Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion chapter_01/.rspec
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
--color
--format nested
--format doc
89 changes: 49 additions & 40 deletions chapter_01/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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
Expand Down
13 changes: 9 additions & 4 deletions chapter_01/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -38,14 +42,15 @@ 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

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
Expand Down
28 changes: 14 additions & 14 deletions chapter_01/spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@

it "should get a user" do
user = User.find_by_name("paul")
user["name"].should == "paul"
user["email"].should == "[email protected]"
user["bio"].should == "rubyist"
expect(user["name"]).to eq "paul"
expect(user["email"]).to eq "[email protected]"
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
Expand All @@ -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
58 changes: 31 additions & 27 deletions chapter_01/spec/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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 == "[email protected]"
expect(last_response.status).to eq 200
attributes = get_attributes(last_response)
expect(attributes["email"]).to eq "[email protected]"
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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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