Skip to content

Commit

Permalink
Add support for symbols in custom requests
Browse files Browse the repository at this point in the history
  • Loading branch information
remi committed Apr 22, 2012
1 parent a366819 commit a7ae25f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,17 @@ In the future, adding hooks to all models will be possible, as well as defining

You can easily add custom methods for your models. You can either use `get_collection` (which maps the returned data to a collection of resources), `get_resource` (which maps the returned data to a single resource) or `get_raw` (which yields the parsed data return from the HTTP request). Other HTTP methods are supported (`post_raw`, `put_resource`, etc.)

```ruby
class User
include Her::Model
end

User.get_collection(:popular) # => [#<User id=1>, #<User id=2>]
# GET /users/popular
```

You can also use a full request path:

```ruby
class User
include Her::Model
Expand All @@ -203,8 +214,8 @@ class User
end
end

User.popular # => [#<User id=1>, #<User id=2>]
User.total # => 42
User.popular # => [#<User id=1>, #<User id=2>]
User.total # => 42
```

## Multiple APIs
Expand Down
15 changes: 15 additions & 0 deletions lib/her/model/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,94 +39,109 @@ def request(attrs={}, &block) # {{{

# Make a GET request and return the parsed JSON response (not mapped to objects)
def get_raw(path, attrs={}, &block) # {{{
path = "#{@her_collection_path}/#{path}" if path.is_a?(Symbol)
request(attrs.merge(:_method => :get, :_path => path), &block)
end # }}}

# Make a GET request and return a collection of resources
def get_collection(path, attrs={}) # {{{
path = "#{@her_collection_path}/#{path}" if path.is_a?(Symbol)
get_raw(path, attrs) do |parsed_data|
new_collection(parsed_data)
end
end # }}}

# Make a GET request and return a collection of resources
def get_resource(path, attrs={}) # {{{
path = "#{@her_collection_path}/#{path}" if path.is_a?(Symbol)
get_raw(path, attrs) do |parsed_data|
new(parsed_data[:data])
end
end # }}}

# Make a POST request and return the parsed JSON response (not mapped to objects)
def post_raw(path, attrs={}, &block) # {{{
path = "#{@her_collection_path}/#{path}" if path.is_a?(Symbol)
request(attrs.merge(:_method => :post, :_path => path), &block)
end # }}}

# Make a POST request and return a collection of resources
def post_collection(path, attrs={}) # {{{
path = "#{@her_collection_path}/#{path}" if path.is_a?(Symbol)
post_raw(path, attrs) do |parsed_data|
new_collection(parsed_data)
end
end # }}}

# Make a POST request and return a collection of resources
def post_resource(path, attrs={}) # {{{
path = "#{@her_collection_path}/#{path}" if path.is_a?(Symbol)
post_raw(path, attrs) do |parsed_data|
new(parsed_data[:data])
end
end # }}}

# Make a PUT request and return the parsed JSON response (not mapped to objects)
def put_raw(path, attrs={}, &block) # {{{
path = "#{@her_collection_path}/#{path}" if path.is_a?(Symbol)
request(attrs.merge(:_method => :put, :_path => path), &block)
end # }}}

# Make a PUT request and return a collection of resources
def put_collection(path, attrs={}) # {{{
path = "#{@her_collection_path}/#{path}" if path.is_a?(Symbol)
put_raw(path, attrs) do |parsed_data|
new_collection(parsed_data)
end
end # }}}

# Make a PUT request and return a collection of resources
def put_resource(path, attrs={}) # {{{
path = "#{@her_collection_path}/#{path}" if path.is_a?(Symbol)
put_raw(path, attrs) do |parsed_data|
new(parsed_data[:data])
end
end # }}}

# Make a PATCH request and return the parsed JSON response (not mapped to objects)
def patch_raw(path, attrs={}, &block) # {{{
path = "#{@her_collection_path}/#{path}" if path.is_a?(Symbol)
request(attrs.merge(:_method => :patch, :_path => path), &block)
end # }}}

# Make a PATCH request and return a collection of resources
def patch_collection(path, attrs={}) # {{{
path = "#{@her_collection_path}/#{path}" if path.is_a?(Symbol)
patch_raw(path, attrs) do |parsed_data|
new_collection(parsed_data)
end
end # }}}

# Make a PATCH request and return a collection of resources
def patch_resource(path, attrs={}) # {{{
path = "#{@her_collection_path}/#{path}" if path.is_a?(Symbol)
patch_raw(path, attrs) do |parsed_data|
new(parsed_data[:data])
end
end # }}}

# Make a DELETE request and return the parsed JSON response (not mapped to objects)
def delete_raw(path, attrs={}, &block) # {{{
path = "#{@her_collection_path}/#{path}" if path.is_a?(Symbol)
request(attrs.merge(:_method => :delete, :_path => path), &block)
end # }}}

# Make a DELETE request and return a collection of resources
def delete_collection(path, attrs={}) # {{{
path = "#{@her_collection_path}/#{path}" if path.is_a?(Symbol)
delete_raw(path, attrs) do |parsed_data|
new_collection(parsed_data)
end
end # }}}

# Make a DELETE request and return a collection of resources
def delete_resource(path, attrs={}) # {{{
path = "#{@her_collection_path}/#{path}" if path.is_a?(Symbol)
delete_raw(path, attrs) do |parsed_data|
new(parsed_data[:data])
end
Expand Down
17 changes: 17 additions & 0 deletions spec/model/http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,22 @@ class User
@user = User.get_resource("/users/1")
@user.id.should == 1
end # }}}

it "handle GET collection through a symbol" do # {{{
@users = User.get_collection(:popular)
@users.length.should == 2
@users.first.id.should == 1
end # }}}

it "handle GET resource through a symbol" do # {{{
@user = User.get_resource(:"1")
@user.id.should == 1
end # }}}

it "handle raw GET through a symbol" do # {{{
User.get_raw(:popular) do |parsed_data|
parsed_data[:data].should == [{ :id => 1 }, { :id => 2 }]
end
end # }}}
end
end

0 comments on commit a7ae25f

Please sign in to comment.