Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Squashed commit of the following:

commit e974712
Author: Daniel Neighman <has.sox@gmail.com>
Date:   Tue Jul 29 00:05:11 2008 +1000

    Adds code and specs for read_one :)
  • Loading branch information...
commit 86aff7e41e83f74903770288b1e9b2da469f9ece 1 parent b83cc30
@hassox hassox authored
View
28 merb-rest-client/lib/merb_rest_adapter.rb
@@ -38,21 +38,17 @@ def create(resources)
end
def read_one(query)
- query.limit = 1
- read_many(query).first
+ response = api_get(resource_name(query).to_s, api_query_parameters(query))
+ fields = query.fields.map{|f| f.name.to_s}
+ result = parse_results(response.body).first
+ value_array = fields.map{|f| result[f]}
+ query.model.load(value_array, query)
end
def read_many(query)
resource = resource_name(query)
Collection.new(query) do |collection|
- parameters = condition_parameters(query.conditions)
- parameters.merge!(order_parameters(query.order)) unless query.order.blank?
- parameters.merge!("fields" => query.fields.map{|f| f.name.to_s}) unless query.fields.blank?
- parameters.merge!("limit" => query.limit) if query.limit
- parameters.merge!("offset" => query.offset) if query.offset && query.offset > 1
- parameters.merge!("unique" => query.unique?) if query.unique?
-
- result = api_get(resource_name(query).to_s, parameters)
+ result = api_get(resource_name(query).to_s, api_query_parameters(query))
values_array =[]
fields = query.fields.map{|f| f.name.to_s}
results_array = parse_results(result.body).map do |result|
@@ -123,7 +119,17 @@ def abstract_request(options)
else
res.error!
end
- end
+ end
+
+ def api_query_parameters(query)
+ parameters = condition_parameters(query.conditions)
+ parameters.merge!(order_parameters(query.order)) unless query.order.blank?
+ parameters.merge!("fields" => query.fields.map{|f| f.name.to_s}) unless query.fields.blank?
+ parameters.merge!("limit" => query.limit) if query.limit
+ parameters.merge!("offset" => query.offset) if query.offset && query.offset > 1
+ parameters.merge!("unique" => query.unique?) if query.unique?
+ parameters
+ end
def field_parameters(fields)
out = []
View
52 merb-rest-client/spec/merb_rest_adapter_spec.rb
@@ -46,9 +46,7 @@ def self.default_repository_name
end
end
- before(:each) do
- end
it "should handle ssl"
it "should setup an connection"
@@ -67,13 +65,12 @@ def self.default_repository_name
it "should return 0 if a post does not save"
end
- describe "read_many" do
+ describe "read_many" do
before(:all) do
@hash = [{"title" => "title", "body" => "body", "id" => 3},{"title" => "another title", "body" => "another body", "id" => 42}]
@json = JSON.generate(@hash)
-
end
-
+
before(:each) do
@response = mock("response")
@adapter.stub!(:abstract_request).and_return(@response)
@@ -156,16 +153,49 @@ def self.default_repository_name
end
end
- describe "load many with associations" do
-
- end
-
end
describe "read_one" do
+
+ before(:all) do
+ @hash = [{"title" => "title", "body" => "body", "id" => 3},{"title" => "another title", "body" => "another body", "id" => 42}]
+ @json = JSON.generate(@hash)
+ end
+
+ before(:each) do
+ @response = mock("response")
+ @adapter.stub!(:abstract_request).and_return(@response)
+ @response.stub!(:body).and_return(@json)
+ end
+
it{@adapter.should respond_to(:read_one)}
- it "should send a get request to a specific Post Resource"
- it "should get the post"
+
+ it "should send a get request to a specific Post Resource" do
+ @adapter.should_receive(:api_get).with("posts", "id.eql" => 3,
+ "fields" => ["id", "title", "body"],
+ "order" => ["id.asc"],
+ "limit" => 1
+ ).and_return(@response)
+
+ Post.get(3).should_not be_nil
+ end
+
+ it "should send a get the first post" do
+ post = Post.first
+ post.should_not be_nil
+ post.id.should == 3
+ post.title.should == "title"
+ post.body.should == "body"
+ end
+
+ it "should get the provided post" do
+ @response.stub!(:body).and_return(JSON.generate([{"title" => "another title", "body" => "another body", "id" => 42}]))
+ post = Post.get(42)
+ post.should_not be_nil
+ post.id.should == 42
+ post.title.should == "another title"
+ post.body.should == "another body"
+ end
end
describe "update" do
Please sign in to comment.
Something went wrong with that request. Please try again.