diff --git a/CHANGELOG b/CHANGELOG index 119ce0cc..6133e539 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,6 @@ +v1.5.4 + - Fix a problem with fetching counts with the http_method_override: true option supplied. + v1.5.3 - Add support for http_method_override (true/false) option to client, which when supplied results in a POST with appropriate X-HTTP-Method-Override header. diff --git a/VERSION b/VERSION index 8af85beb..94fe62c2 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.5.3 +1.5.4 diff --git a/lib/spark_api/faraday_middleware.rb b/lib/spark_api/faraday_middleware.rb index fd057614..17436d2c 100644 --- a/lib/spark_api/faraday_middleware.rb +++ b/lib/spark_api/faraday_middleware.rb @@ -29,7 +29,11 @@ def on_complete(env) if paging.nil? results = response else - q = CGI.parse(env[:url].query) + q = if http_method_override_request?(env) + CGI.parse(env[:request_body]) + else + CGI.parse(env[:url].query) + end if q.key?("_pagination") && q["_pagination"].first == "count" results = paging['TotalRows'] else @@ -87,7 +91,13 @@ def decompress_body(env) env[:body] end - + + private + + def http_method_override_request?(env) + env[:request_headers]["X-HTTP-Method-Override"] == "GET" + end + end Faraday::Response.register_middleware :spark_api => FaradayMiddleware diff --git a/spec/support/stub_api_requests.rb b/spec/support/stub_api_requests.rb index 857aba33..7dea277c 100644 --- a/spec/support/stub_api_requests.rb +++ b/spec/support/stub_api_requests.rb @@ -41,11 +41,14 @@ def stub_api_request(meth, service_path, stub_fixture, body, opts, &block) def with_args(service_path, opts, body=nil) if body.is_a?(Hash) - body = { :D => body } unless body.empty? - elsif !body.nil? + body = { :D => body } unless body.empty? + body_str = body.nil? ? body : MultiJson.dump(body) + elsif body =~ /\.json\z/ body = MultiJson.load(fixture(body).read) + body_str = body.nil? ? body : MultiJson.dump(body) + else + body_str = body end - body_str = body.nil? ? body : MultiJson.dump(body) params = {:ApiUser => "foobar", :AuthToken => "c401736bf3d3f754f07c04e460e09573"}.merge(opts) query = {:ApiSig => get_signature(service_path, params, body_str) } diff --git a/spec/unit/spark_api/models/listing_spec.rb b/spec/unit/spark_api/models/listing_spec.rb index c5386de8..6f731ece 100644 --- a/spec/unit/spark_api/models/listing_spec.rb +++ b/spec/unit/spark_api/models/listing_spec.rb @@ -181,6 +181,12 @@ count = Listing.count() expect(count).to eq(2001) end + + on_get_it "should return the count even if http_method_override: true is passed" do + stub_api_post('/listings', '_pagination=count', 'count.json') + count = Listing.count(http_method_override: true) + expect(count).to eq(2001) + end end context "/listings/", :support do