Hi Paul & Trotter,
I just want to let you know that I was able to run the client_spec.rb
using Net::HTTP.
I just want to share the client_spec.rb code with others. Thank you
both for your help. I am looking forward to next few chapters of the
book.
-Catherine
### client_spec.rb
require 'rubygems'
require 'bundler/setup'
require 'net/http'
require 'json'
class User
class << self; attr_accessor :base_uri end
def self.find_by_name(name)
url = URI.parse("#{base_uri}/api/v1/users/#{name}")
req = Net::HTTP::Get.new(url.path)
res = Net::HTTP.start(url.host, url.port) {|http|
http.request(req)
}
if res.code.to_i == 200
JSON.parse(res.body)["user"]
elsif res.code.to_i == 404
nil
else
raise res.body
end
end
def self.create(attributes = {})
url = URI.parse("#{base_uri}/api/v1/users")
req = Net::HTTP::Post.new(url.path)
req.body = attributes.to_json
res = Net::HTTP.new(url.host, url.port).start {|http|
http.request(req) }
case res
when Net::HTTPSuccess, Net::HTTPRedirection
JSON.parse(res.body)["user"]
else
raise res.body
end
end
end