Skip to content

Commit

Permalink
Adds xml format and repository storage name to the parsing. eg {'post…
Browse files Browse the repository at this point in the history
…s' => [{title => '..'}]} etc
  • Loading branch information
Daniel Neighman committed Aug 12, 2008
1 parent cabe866 commit c4eaa39
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
6 changes: 4 additions & 2 deletions merb-rest-client/lib/merb_rest_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def update(attributes, query)
def read_one(query)
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
result = parse_results(response.body)[resource_name(query).to_s].first
value_array = fields.map{|f| result[f]}
query.model.load(value_array, query)
end
Expand All @@ -53,7 +53,7 @@ def read_many(query)
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|
results_array = parse_results(result.body)[resource_name(query)].map do |result|
fields.map{|f| result[f]}
end
results_array.each do |result|
Expand Down Expand Up @@ -185,6 +185,8 @@ def parse_results(data)
case @format
when :json
data.blank? ? {} : JSON.parse(data)
when :xml
data.blank? ? {} : Merb::Rest::Formats::Xml.decode(data)
end
end

Expand Down
38 changes: 20 additions & 18 deletions merb-rest-client/spec/merb_rest_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def self.default_repository_name
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"}]))
@response.stub!(:body).and_return(JSON.generate("posts" => [{:id => 3, :title => "blah"}], "comments" => [{:id => 3, :title => "blah"}]))

repository(:merb_rest_ssl){Comment.all.each{}}
end
Expand All @@ -71,7 +71,7 @@ def self.default_repository_name
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"}])))
Net::HTTP.should_receive(:new).and_return(mock("response", :null_object => true, :body => JSON.generate({"posts" => [{:id => 3, :title => "blah"}]})))
Post.all.each{}
end

Expand Down Expand Up @@ -101,7 +101,9 @@ def self.default_repository_name

describe "read_many" do
before(:all) do
@hash = [{"title" => "title", "body" => "body", "id" => 3},{"title" => "another title", "body" => "another body", "id" => 42}]
@hash = {"posts" => [{"title" => "title", "body" => "body", "id" => 3},{"title" => "another title", "body" => "another body", "id" => 42}],
"comments" => [{"title" => "title", "body" => "body", "id" => 3},{"title" => "another title", "body" => "another body", "id" => 42}]
}
@json = JSON.generate(@hash)
end

Expand Down Expand Up @@ -213,7 +215,7 @@ def self.default_repository_name
describe "read_one" do

before(:all) do
@hash = [{"title" => "title", "body" => "body", "id" => 3},{"title" => "another title", "body" => "another body", "id" => 42}]
@hash = {:posts => [{"title" => "title", "body" => "body", "id" => 3},{"title" => "another title", "body" => "another body", "id" => 42}]}
@json = JSON.generate(@hash)
end

Expand Down Expand Up @@ -244,7 +246,7 @@ def self.default_repository_name
end

it "should get the provided post" do
@response.stub!(:body).and_return(JSON.generate([{"title" => "another title", "body" => "another body", "id" => 42}]))
@response.stub!(:body).and_return(JSON.generate({:posts => [{"title" => "another title", "body" => "another body", "id" => 42}]}))
post = Post.get(42)
post.should_not be_nil
post.id.should == 42
Expand Down Expand Up @@ -324,19 +326,19 @@ def self.default_repository_name
end
end

describe "formats" do
describe "json" do
before do
@post = Post.new(:title => "title", :body => "body", :id => 3)
@post_json = @post.to_json
end

it "should parse the json of an object" do
result = @adapter.send(:parse_results, @post_json)
result.should == {"title" => "title", "body" => "body", "id" => 3}
end
end
end
# describe "formats" do
# describe "json" do
# before do
# @post = Post.new(:title => "title", :body => "body", :id => 3)
# @post_json = @post.to_json
# end
#
# it "should parse the json of an object" do
# result = @adapter.send(:parse_results, @post_json)
# result.should == {:posts => [{"title" => "title", "body" => "body", "id" => 3}]}
# end
# end
# end

describe "api methods" do
before do
Expand Down

0 comments on commit c4eaa39

Please sign in to comment.