Skip to content

Commit ec60d86

Browse files
authored
API-7806: fix a problem with fetching counts with the http_method_override: true option supplied. (#175)
* API-7806: fix bug with http_method_override requests that request pagination * API-7806: version bump
1 parent 73c890b commit ec60d86

File tree

5 files changed

+28
-6
lines changed

5 files changed

+28
-6
lines changed

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
v1.5.4
2+
- Fix a problem with fetching counts with the http_method_override: true option supplied.
3+
14
v1.5.3
25
- 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.
36

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.5.3
1+
1.5.4

lib/spark_api/faraday_middleware.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ def on_complete(env)
2929
if paging.nil?
3030
results = response
3131
else
32-
q = CGI.parse(env[:url].query)
32+
q = if http_method_override_request?(env)
33+
CGI.parse(env[:request_body])
34+
else
35+
CGI.parse(env[:url].query)
36+
end
3337
if q.key?("_pagination") && q["_pagination"].first == "count"
3438
results = paging['TotalRows']
3539
else
@@ -87,7 +91,13 @@ def decompress_body(env)
8791

8892
env[:body]
8993
end
90-
94+
95+
private
96+
97+
def http_method_override_request?(env)
98+
env[:request_headers]["X-HTTP-Method-Override"] == "GET"
99+
end
100+
91101
end
92102

93103
Faraday::Response.register_middleware :spark_api => FaradayMiddleware

spec/support/stub_api_requests.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,14 @@ def stub_api_request(meth, service_path, stub_fixture, body, opts, &block)
4141

4242
def with_args(service_path, opts, body=nil)
4343
if body.is_a?(Hash)
44-
body = { :D => body } unless body.empty?
45-
elsif !body.nil?
44+
body = { :D => body } unless body.empty?
45+
body_str = body.nil? ? body : MultiJson.dump(body)
46+
elsif body =~ /\.json\z/
4647
body = MultiJson.load(fixture(body).read)
48+
body_str = body.nil? ? body : MultiJson.dump(body)
49+
else
50+
body_str = body
4751
end
48-
body_str = body.nil? ? body : MultiJson.dump(body)
4952

5053
params = {:ApiUser => "foobar", :AuthToken => "c401736bf3d3f754f07c04e460e09573"}.merge(opts)
5154
query = {:ApiSig => get_signature(service_path, params, body_str) }

spec/unit/spark_api/models/listing_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,12 @@
181181
count = Listing.count()
182182
expect(count).to eq(2001)
183183
end
184+
185+
on_get_it "should return the count even if http_method_override: true is passed" do
186+
stub_api_post('/listings', '_pagination=count', 'count.json')
187+
count = Listing.count(http_method_override: true)
188+
expect(count).to eq(2001)
189+
end
184190
end
185191

186192
context "/listings/<listing_id>", :support do

0 commit comments

Comments
 (0)