Skip to content

Commit

Permalink
socrata#14 adding stubbed demarshall config option with FAILING test
Browse files Browse the repository at this point in the history
  • Loading branch information
marks committed Sep 10, 2014
1 parent ec29257 commit bb50acd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
15 changes: 10 additions & 5 deletions lib/soda/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Client
# * +:password+ - Your Socrata password (optional, only necessary for modifying data)
# * +:app_token+ - Your Socrata application token (register at http://dev.socrata.com/register)
# * +:ignore_ssl+ - Ignore ssl errors (defaults to false)
# * +:demarshall+ - Convert returned data (http://dev.socrata.com/docs/datatypes/) to native Ruby representations (defaults to false)
#
# Returns a SODA::Client instance.
#
Expand Down Expand Up @@ -165,12 +166,16 @@ def handle_response(response)
if response.body.nil? || response.body.empty?
return nil
elsif response["Content-Type"].include?("application/json")
# Return a bunch of mashes if we're JSON
response = JSON::parse(response.body, :max_nesting => false)
if response.is_a? Array
return response.collect { |r| Hashie::Mash.new(r) }
if @config[:demarshall]
raise "Demarshalling not yet implemented"
else
return Hashie::Mash.new(response)
# Return a bunch of mashes if we're JSON
response = JSON::parse(response.body, :max_nesting => false)
if response.is_a? Array
return response.collect { |r| Hashie::Mash.new(r) }
else
return Hashie::Mash.new(response)
end
end
else
# We don't partically care, just return the raw body
Expand Down
23 changes: 23 additions & 0 deletions test/test_soda.rb
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,27 @@ def resource(name)
assert response.is_a? String
end
end

context "demarshalling earthquake data" do
setup do
@client = SODA::Client.new({:domain => DOMAIN, :app_token => "K6rLY8NBK0Hgm8QQybFmwIUQw", :demarshall => true })
end

should "get the results we expect" do
stub_request(:get, "https://fakehost.socrata.com/resource/earthquakes.json?$where=magnitude > 4&source=pr")
.to_return(resource("earthquakes_source_pr_where_gt_4.response"))

response = @client.get("earthquakes", {"$where" => "magnitude > 4", :source => "pr"})
puts response
assert_equal 1, response.size

quake = response.first
assert_equal "Puerto Rico region", quake.region
assert_equal 4.2, quake.magnitude
assert_equal 17.00, quake.depth

assert quake.region?
end
end

end

0 comments on commit bb50acd

Please sign in to comment.