Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Resource nested hash support #42

Merged
merged 2 commits into from

2 participants

@artempartos

I use Weary for my client. In some my API i use ransack and often i have a nested hash in params. For example i have rest people index api, where three hash keys.

get :people, "/api/people.json" do |resource|
      resource.optional :q, :per_page, :page
end

Here page and per_page integer option, and q - ransack search nested hash.
for example q[description_cont] or q[year_eq].
But with Weary::Resource i have error (test illustrate it)

 TypeError:
       Can't convert Hash into String.

It's famous addresable issue sporkmonger/addressable#36 which fixed in addresable 2.2.7 and broken in addresable 2.3.
I decide fix this bug here, because addresable maintainer doss't want do it (sporkmonger/addressable#84) and
minor version of Weary is higher than 2.2.

@mwunsch mwunsch merged commit ef7ac66 into mwunsch:master

1 check passed

Details default The Travis CI build passed
@artempartos artempartos deleted the artempartos:feature/nested_hash_params branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 9 additions and 3 deletions.
  1. +2 −3 lib/weary/request.rb
  2. +7 −0 spec/weary/resource_spec.rb
View
5 lib/weary/request.rb
@@ -69,13 +69,12 @@ def method=(verb)
def params(parameters=nil)
if !parameters.nil?
+ @body = query_params_from_hash(parameters)
if ["POST", "PUT"].include? method
- @body = query_params_from_hash(parameters)
body StringIO.new(@body)
use Weary::Middleware::ContentType
else
- uri.query_values = parameters
- @body = uri.query
+ uri.query = @body
end
end
@body
View
7 spec/weary/resource_spec.rb
@@ -246,5 +246,12 @@
req = resource.request :user_id => "markwunsch"
req.params.should eql "user_id=markwunsch"
end
+
+ it "passes nested hash parameters into the request body" do
+ resource = described_class.new "GET", "http://github.com/api/v2/json/repos/show/mwunsch/weary"
+ resource.optional :search
+ req = resource.request :search => {:description => 'weary'}
+ req.params.should eql "search%5Bdescription%5D=weary"
+ end
end
end
Something went wrong with that request. Please try again.