Skip to content

Commit

Permalink
Adds https to the mix
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Neighman committed Jul 28, 2008
1 parent e1bcc42 commit c8870e3
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 43 deletions.
1 change: 1 addition & 0 deletions merb-rest-client/lib/merb-rest-client.rb
@@ -1,5 +1,6 @@
require 'dm-core'
require 'net/http'
require 'net/https'
require 'uri'

require File.join(File.dirname(__FILE__), "merb_rest_adapter")
59 changes: 26 additions & 33 deletions merb-rest-client/lib/merb_rest_adapter.rb
Expand Up @@ -39,26 +39,6 @@ def update(attributes, query)
end
updated
end

# def update(attributes, query)
# updated = 0
# resources = read_many(query)
# resources.each do |resource|
# key = resource.class.key(self.name).map do |property|
# resource.instance_variable_get(property.instance_variable_name)
# end
# result = http_put("/#{self.escaped_db_name}/#{key}", resource.to_json)
# if result["ok"]
# key = resource.class.key(self.name)
# resource.instance_variable_set(
# key.first.instance_variable_name, result["id"])
# resource.instance_variable_set(
# "@rev", result["rev"])
# updated += 1
# end
# end
# updated
# end

def read_one(query)
response = api_get(resource_name(query).to_s, api_query_parameters(query))
Expand All @@ -82,6 +62,17 @@ def read_many(query)
end
end

def delete(query)
deleted = 0
resources = read_many(query)
storage_name = resource_name(query).to_s
resources.each do |resource|
result = api_delete(storage_name, api_query_parameters(query))
deleted +=1 if successful?(result)
end
deleted > 0
end

protected
def api_get(path, options = {})
abstract_request(
Expand Down Expand Up @@ -134,6 +125,8 @@ def abstract_request(options)
req = klass.new(path)
req.basic_auth @uri.user, @uri.password
req.set_form_data(data, seperator)
req.use_ssl = true if @uri.scheme == "https"

res = Net::HTTP.new(@uri.host, @uri.port).start{|http| http.request(req)}
case res
when Net::HTTPSuccess, Net::HTTPRedirection
Expand Down Expand Up @@ -162,7 +155,7 @@ def field_parameters(fields)
def condition_parameters(conditions)
out = {}
conditions.each do |operator, prop, value|
out.merge!("#{prop.name}.#{operator}" => value)
out.merge!("#{prop.name}.#{operator}" => value.to_s)
end
out
end
Expand Down Expand Up @@ -204,20 +197,20 @@ def normalize_uri(uri_or_options)
uri_or_options = Addressable::URI.parse(uri_or_options)
end
if Addressable::URI === uri_or_options
uri_or_options.scheme = "http"
uri_or_options.scheme = "http" if uri_or_options.scheme == "merb_rest"
return uri_or_options.normalize
end

user = uri_or_options.fetch(:username)
password = uri_or_options.fetch(:password)
host = uri_or_options.fetch(:host, "")
port = uri_or_options.fetch(:port)
database = uri_or_options.fetch(:database)
scheme = uri_or_options.fetch(:scheme, "http")
@format = uri_or_options.fetch(:format, :json)
query = uri_or_options.to_a.map { |pair| pair.join('=') }.join('&')
query = nil if query == ""

opts = uri_or_options.dup
opts.delete(:adapter)
user = opts.delete(:username)
password = opts.delete(:password)
host = opts.delete(:host) || ""
database = opts.delete(:database) || ""
scheme = opts.delete(:scheme) || "http"
port = opts.delete(:port) || scheme == "https" ? 443 : 80
@format = opts.delete(:format) || :json
query = opts.to_a.map { |pair| pair.join('=') }.join('&')
query = nil if query.blank?
return Addressable::URI.new(
scheme, user, password, host, port, database, query, nil
)
Expand Down
84 changes: 74 additions & 10 deletions merb-rest-client/spec/merb_rest_adapter_spec.rb
Expand Up @@ -27,7 +27,8 @@ def self.default_repository_name

property :id, Serial
property :title, String
property :body, Text, :lazy => false
property :body, Text
property :created_at, DateTime

belongs_to :post
end
Expand All @@ -44,11 +45,37 @@ def self.default_repository_name



it "should handle ssl"
it "should setup an connection"
it "should setup a connection with basic auth"
it "should handle date/time"
it "should handle date"
it "should handle ssl" do
@response = mock("response")
@request = Net::HTTP::Get.new("http://example.com")
DataMapper.setup(:merb_rest_ssl, :adapter => "merb_rest",
:host => "example.com",
:scheme => "https",
:port => 443,
:username => "hassox",
:password => "password",
:format => :json)
adapter = repository(:merb_rest_ssl).adapter
Net::HTTP::Get.should_receive(:new).and_return(@request)
@request.should_receive(:use_ssl=).with(true)
Net::HTTP.should_receive(:new).and_return(@response)
@response.should_receive(:start).and_return(@response)
@response.stub!(:error!).and_return(@response)
@response.stub!(:body).and_return(JSON.generate([{:id => 3, :title => "blah"}]))

repository(:merb_rest_ssl){Comment.all.each{}}
end


it "should setup a connection with basic auth" do
req = Net::HTTP::Get.new("http://example.com")
req.should_receive(:basic_auth)
Net::HTTP::Get.should_receive(:new).and_return(req)
Net::HTTP.should_receive(:new).and_return(mock("response", :null_object => true, :body => JSON.generate([{:id => 3, :title => "blah"}])))
Post.all.each{}
end



describe "create" do
before(:each) do
Expand Down Expand Up @@ -119,6 +146,27 @@ def self.default_repository_name
posts[1].body.should == "another body"
end

it "should handle date/time" do
d = DateTime.now
@adapter.should_receive(:api_get) do |location, params|
location.should == "comments"
params["created_at.eql"].should == d.to_s
@response
end
Comment.all(:created_at => d).each{}
end


it "should handle date" do
d = Date.today
@adapter.should_receive(:api_get) do |location, params|
location.should == "comments"
params["created_at.eql"].should == d.to_s
@response
end
Comment.all(:created_at => d).each{}
end

describe "read many with conditions" do

it "should use a get with conditional parameters" do
Expand Down Expand Up @@ -178,7 +226,7 @@ def self.default_repository_name
it{@adapter.should respond_to(:read_one)}

it "should send a get request to a specific Post Resource" do
@adapter.should_receive(:api_get).with("posts", "id.eql" => 3,
@adapter.should_receive(:api_get).with("posts", "id.eql" => "3",
"fields" => ["id", "title", "body"],
"order" => ["id.asc"],
"limit" => 1
Expand Down Expand Up @@ -255,9 +303,25 @@ def self.default_repository_name
end

it{@adapter.should respond_to(:delete)}
it "should send a delete request to a specific resource"
it "should send a delete request to the general resource with parameters"
it "should delete all records"

it "should send a delete request to a specific resource" do
@adapter.should_receive(:api_delete) do |location, attributes|
location.should == "posts"
attributes["id.eql"].should == "16"
@response
end
@post.destroy
end

it "return false if the item is deleted" do
@response.should_receive(:code).and_return("500")
@post.destroy.should be_false
end

it "should return true if the item is deleted" do
@response.should_receive(:code).and_return("200")
@post.destroy.should be_true
end
end

describe "formats" do
Expand Down

0 comments on commit c8870e3

Please sign in to comment.