From 836b3f3925eebecdddc6784818ddfb8d40a59423 Mon Sep 17 00:00:00 2001 From: Cody Skidmore Date: Tue, 19 May 2015 12:57:03 -0400 Subject: [PATCH] Updated gem & Ruby versions. Fixed Rspec syntax to remove deprecation errors. --- .gitignore | 19 +++++++ chapter_01/.rspec | 2 +- chapter_01/Gemfile.lock | 89 ++++++++++++++++++--------------- chapter_01/client.rb | 13 +++-- chapter_01/spec/client_spec.rb | 28 +++++------ chapter_01/spec/service_spec.rb | 58 +++++++++++---------- 6 files changed, 123 insertions(+), 86 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e1e72c3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,19 @@ +# Created by .ignore support plugin (hsz.mobi) +**/.idea +# See https://help.github.com/articles/ignoring-files for more about ignoring files. +# +# If you find yourself ignoring temporary files generated by your text editor +# or operating system, you probably want to add a global ignore instead: +# git config --global core.excludesfile '~/.gitignore_globasl' + +# Ignore bundler config. +**/.bundle + +# Ignore the default SQLite database. +**/db/*.sqlite3 +**/db/*.sqlite3-journal + +# Ignore all logfiles and tempfiles. +**/log/* +**!/log/.keep +**/tmp \ No newline at end of file diff --git a/chapter_01/.rspec b/chapter_01/.rspec index 0eb654b..9d52c56 100644 --- a/chapter_01/.rspec +++ b/chapter_01/.rspec @@ -1,2 +1,2 @@ --color ---format nested +--format doc diff --git a/chapter_01/Gemfile.lock b/chapter_01/Gemfile.lock index 99375d3..f483195 100644 --- a/chapter_01/Gemfile.lock +++ b/chapter_01/Gemfile.lock @@ -1,48 +1,57 @@ GEM remote: http://rubygems.org/ specs: - activemodel (3.2.6) - activesupport (= 3.2.6) - builder (~> 3.0.0) - activerecord (3.2.6) - activemodel (= 3.2.6) - activesupport (= 3.2.6) - arel (~> 3.0.2) - tzinfo (~> 0.3.29) - activesupport (3.2.6) - i18n (~> 0.6) - multi_json (~> 1.0) - arel (3.0.2) - builder (3.0.0) - diff-lcs (1.1.3) - ffi (1.0.11) - i18n (0.6.0) - json (1.7.3) - mime-types (1.19) - multi_json (1.3.6) - rack (1.4.1) - rack-protection (1.2.0) + activemodel (4.2.1) + activesupport (= 4.2.1) + builder (~> 3.1) + activerecord (4.2.1) + activemodel (= 4.2.1) + activesupport (= 4.2.1) + arel (~> 6.0) + activesupport (4.2.1) + i18n (~> 0.7) + json (~> 1.7, >= 1.7.7) + minitest (~> 5.1) + thread_safe (~> 0.3, >= 0.3.4) + tzinfo (~> 1.1) + arel (6.0.0) + builder (3.2.2) + diff-lcs (1.2.5) + ethon (0.7.3) + ffi (>= 1.3.0) + ffi (1.9.8) + i18n (0.7.0) + json (1.8.2) + minitest (5.6.1) + rack (1.6.1) + rack-protection (1.5.3) rack - rack-test (0.6.1) + rack-test (0.6.3) rack (>= 1.0) - rspec (2.10.0) - rspec-core (~> 2.10.0) - rspec-expectations (~> 2.10.0) - rspec-mocks (~> 2.10.0) - rspec-core (2.10.1) - rspec-expectations (2.10.0) - diff-lcs (~> 1.1.3) - rspec-mocks (2.10.1) - sinatra (1.3.2) - rack (~> 1.3, >= 1.3.6) - rack-protection (~> 1.2) - tilt (~> 1.3, >= 1.3.3) - sqlite3 (1.3.6) - tilt (1.3.3) - typhoeus (0.4.2) - ffi (~> 1.0) - mime-types (~> 1.18) - tzinfo (0.3.33) + rspec (3.2.0) + rspec-core (~> 3.2.0) + rspec-expectations (~> 3.2.0) + rspec-mocks (~> 3.2.0) + rspec-core (3.2.3) + rspec-support (~> 3.2.0) + rspec-expectations (3.2.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.2.0) + rspec-mocks (3.2.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.2.0) + rspec-support (3.2.2) + sinatra (1.4.6) + rack (~> 1.4) + rack-protection (~> 1.4) + tilt (>= 1.3, < 3) + sqlite3 (1.3.10) + thread_safe (0.3.5) + tilt (2.0.1) + typhoeus (0.7.1) + ethon (>= 0.7.1) + tzinfo (1.2.2) + thread_safe (~> 0.1) PLATFORMS ruby diff --git a/chapter_01/client.rb b/chapter_01/client.rb index e5c7c5d..e4d93a5 100644 --- a/chapter_01/client.rb +++ b/chapter_01/client.rb @@ -4,10 +4,14 @@ class User class << self; attr_accessor :base_uri end + def self.get_attributes(json_response) + JSON.parse(json_response.body) + end + def self.find_by_name(name) response = Typhoeus::Request.get("#{base_uri}/api/v1/users/#{name}") if response.code == 200 - JSON.parse(response.body)["user"] + get_attributes(response) elsif response.code == 404 nil else @@ -18,7 +22,7 @@ def self.find_by_name(name) def self.create attributes response = Typhoeus::Request.post("#{base_uri}/api/v1/users", :body => attributes.to_json ) if response.code == 200 - JSON.parse(response.body)['user'] + get_attributes(response) elsif response.code == 400 nil else @@ -29,7 +33,7 @@ def self.create attributes def self.update(name, attributes) response = Typhoeus::Request.put("#{base_uri}/api/v1/users/#{name}", :body => attributes.to_json) if response.code == 200 - JSON.parse(response.body)['user'] + get_attributes(response) elsif response.code == 400 || response.code == 404 nil else @@ -38,6 +42,7 @@ def self.update(name, attributes) end def self.destroy(name) + pp "#{base_uri}/api/v1/users/#{name}" response = Typhoeus::Request.delete("#{base_uri}/api/v1/users/#{name}") response.success? # response.code == 200 end @@ -45,7 +50,7 @@ def self.destroy(name) def self.login(name, password) response = Typhoeus::Request.post("#{base_uri}/api/v1/users/#{name}/sessions", :body => {:password => password}.to_json) if response.success? # response.code == 200 - JSON.parse(response.body)["user"] + get_attributes(response) elsif response.code == 400 nil else diff --git a/chapter_01/spec/client_spec.rb b/chapter_01/spec/client_spec.rb index 55fe127..8790799 100644 --- a/chapter_01/spec/client_spec.rb +++ b/chapter_01/spec/client_spec.rb @@ -27,13 +27,13 @@ it "should get a user" do user = User.find_by_name("paul") - user["name"].should == "paul" - user["email"].should == "paul@pauldix.net" - user["bio"].should == "rubyist" + expect(user["name"]).to eq "paul" + expect(user["email"]).to eq "paul@pauldix.net" + expect(user["bio"]).to eq "rubyist" end it "should return nil for a user not found" do - User.find_by_name("gosling").should be_nil + expect(User.find_by_name("gosling")).to eq nil end it "should create a user" do @@ -43,29 +43,29 @@ :name => random_name, :email => random_email, :password => 'whatev') - user['name'].should == random_name - user['email'].should == random_email - User.find_by_name(random_name).should == user + expect(user["name"]).to eq random_name + expect(user["email"]).to eq random_email + expect(User.find_by_name(random_name)).to eq user end it "should update a user" do user = User.update("paul", :bio => "rubyist and author") - user["name"].should == "paul" - user["bio"].should == "rubyist and author" - User.find_by_name('paul').should == user + expect(user["name"]).to eq "paul" + expect(user["bio"]).to eq "rubyist and author" + expect(User.find_by_name('paul')).to eq user end it "should destroy a user" do - User.destroy("bryan").should == true - User.find_by_name("bryan").should be_nil + expect(User.destroy("bryan")).to eq true + expect(User.find_by_name("bryan")).to eq nil end it "should verify login credentials" do user = User.login("paul", "strongpass") - user["name"].should == "paul" + expect(user["name"]).to eq "paul" end it "should return nil with invalid credentials" do - User.login("paul", "wrongpassword").should be_nil + expect(User.login("paul", "wrongpassword")).to eq nil end end \ No newline at end of file diff --git a/chapter_01/spec/service_spec.rb b/chapter_01/spec/service_spec.rb index 492cd00..1c1821e 100644 --- a/chapter_01/spec/service_spec.rb +++ b/chapter_01/spec/service_spec.rb @@ -20,6 +20,10 @@ def app User.delete_all end + def get_attributes(json_response) + JSON.parse(json_response.body) + end + describe "GET on /api/v1/users/:id" do before(:each) do User.create( @@ -31,35 +35,35 @@ def app it "should return a user by name" do get '/api/v1/users/paul' - last_response.should be_ok - attributes = JSON.parse(last_response.body)["user"] - attributes["name"].should == "paul" + expect(last_response.status).to eq 200 + attributes = get_attributes(last_response) + expect(attributes["name"]).to eq "paul" end it "should return a user with an email" do get '/api/v1/users/paul' - last_response.should be_ok - attributes = JSON.parse(last_response.body)["user"] - attributes["email"].should == "paul@pauldix.net" + expect(last_response.status).to eq 200 + attributes = get_attributes(last_response) + expect(attributes["email"]).to eq "paul@pauldix.net" end it "should not return a user's password" do get '/api/v1/users/paul' - last_response.should be_ok - attributes = JSON.parse(last_response.body)["user"] - attributes.should_not have_key("password") + expect(last_response.status).to eq 200 + attributes = get_attributes(last_response) + expect(attributes).to_not have_key(:password) end it "should return a user with a bio" do get '/api/v1/users/paul' - last_response.should be_ok - attributes = JSON.parse(last_response.body)["user"] - attributes["bio"].should == "rubyist" + expect(last_response.status).to eq 200 + attributes = get_attributes(last_response) + expect(attributes["bio"]).to eq "rubyist" end it "should return a 404 for a user that doesn't exist" do get '/api/v1/users/foo' - last_response.status.should == 404 + expect(last_response.status).to eq 404 end end @@ -70,12 +74,12 @@ def app :email => "no spam", :password => "whatever", :bio => "southern bell"}.to_json - last_response.should be_ok + expect(last_response.status).to eq 200 get '/api/v1/users/trotter' - attributes = JSON.parse(last_response.body)["user"] - attributes["name"].should == "trotter" - attributes["email"].should == "no spam" - attributes["bio"].should == "southern bell" + attributes = get_attributes(last_response) + expect(attributes["name"]).to eq "trotter" + expect(attributes["email"]).to eq "no spam" + expect(attributes["bio"]).to eq "southern bell" end end @@ -88,10 +92,10 @@ def app :bio => "rspec master") put '/api/v1/users/bryan', { :bio => "testing freak"}.to_json - last_response.should be_ok + expect(last_response.status).to eq 200 get '/api/v1/users/bryan' - attributes = JSON.parse(last_response.body)["user"] - attributes["bio"].should == "testing freak" + attributes = get_attributes(last_response) + expect(attributes["bio"]).to eq "testing freak" end end @@ -103,9 +107,9 @@ def app :password => "whatever", :bio => "williamsburg hipster") delete '/api/v1/users/francis' - last_response.should be_ok + expect(last_response.status).to eq 200 get '/api/v1/users/francis' - last_response.status.should == 404 + expect(last_response.status).to eq 404 end end @@ -120,15 +124,15 @@ def app it "should return the user object on valid credentials" do post '/api/v1/users/josh/sessions', { :password => "nyc.rb rules"}.to_json - last_response.should be_ok - attributes = JSON.parse(last_response.body)["user"] - attributes["name"].should == "josh" + expect(last_response.status).to eq 200 + attributes = get_attributes(last_response) + expect(attributes["name"]).to eq "josh" end it "should fail on invalid credentials" do post '/api/v1/users/josh/sessions', { :password => "wrong"}.to_json - last_response.status.should == 400 + expect(last_response.status).to eq 400 end end end