Permalink
Browse files

Add support for `get`, `post`, etc. wrapper methods

  • Loading branch information...
1 parent a7ae25f commit 37aef13fe39d558e5daeae07d9a181a1f4021355 @remiprev committed Apr 22, 2012
Showing with 88 additions and 5 deletions.
  1. +19 −5 README.md
  2. +60 −0 lib/her/model/http.rb
  3. +9 −0 spec/model/http_spec.rb
View
@@ -186,29 +186,32 @@ In the future, adding hooks to all models will be possible, as well as defining
## Custom requests
-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.)
+You can easily add custom methods for your models. You can either use `get`, `post`, `put` or `delete` (which maps the returned data to either a collection or a resource).
```ruby
class User
include Her::Model
end
-User.get_collection(:popular) # => [#<User id=1>, #<User id=2>]
+User.get(:popular) # => [#<User id=1>, #<User id=2>]
# GET /users/popular
+
+User.get(:single_best) # => #<User id=1>
+# GET /users/single_best
```
-You can also use a full request path:
+Also, `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) can also be used. Other HTTP methods are supported (`post_raw`, `put_resource`, etc.).
```ruby
class User
include Her::Model
def self.popular
- get_collection("/users/popular")
+ get_collection(:popular)
end
def self.total
- get_raw("/users/stats") do |parsed_data|
+ get_raw(:stats) do |parsed_data|
parsed_data[:data][:total_users]
end
end
@@ -218,6 +221,17 @@ User.popular # => [#<User id=1>, #<User id=2>]
User.total # => 42
```
+You can also use full request paths (with strings instead of symbols).
+
+```ruby
+class User
+ include Her::Model
+end
+
+User.get("/users/popular") # => [#<User id=1>, #<User id=2>]
+# GET /users/popular
+```
+
## Multiple APIs
It is possible to use different APIs for different models. Instead of calling `Her::API.setup`, you can create instances of `Her::API`:
View
@@ -37,6 +37,18 @@ def request(attrs={}, &block) # {{{
yield @her_api.request(attrs)
end # }}}
+ # Make a GET request and return either a collection or a resource
+ def get(path, attrs={}) # {{{
+ path = "#{@her_collection_path}/#{path}" if path.is_a?(Symbol)
+ get_raw(path, attrs) do |parsed_data|
+ if parsed_data[:data].is_a?(Array)
+ new_collection(parsed_data)
+ else
+ new(parsed_data[:data])
+ end
+ end
+ end # }}}
+
# 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)
@@ -59,6 +71,18 @@ def get_resource(path, attrs={}) # {{{
end
end # }}}
+ # Make a POST request and return either a collection or a resource
+ def post(path, attrs={}) # {{{
+ path = "#{@her_collection_path}/#{path}" if path.is_a?(Symbol)
+ post_raw(path, attrs) do |parsed_data|
+ if parsed_data[:data].is_a?(Array)
+ new_collection(parsed_data)
+ else
+ new(parsed_data[:data])
+ end
+ 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)
@@ -81,6 +105,18 @@ def post_resource(path, attrs={}) # {{{
end
end # }}}
+ # Make a PUT request and return either a collection or a resource
+ def put(path, attrs={}) # {{{
+ path = "#{@her_collection_path}/#{path}" if path.is_a?(Symbol)
+ put_raw(path, attrs) do |parsed_data|
+ if parsed_data[:data].is_a?(Array)
+ new_collection(parsed_data)
+ else
+ new(parsed_data[:data])
+ end
+ 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)
@@ -103,6 +139,18 @@ def put_resource(path, attrs={}) # {{{
end
end # }}}
+ # Make a PATCH request and return either a collection or a resource
+ def patch(path, attrs={}) # {{{
+ path = "#{@her_collection_path}/#{path}" if path.is_a?(Symbol)
+ patch_raw(path, attrs) do |parsed_data|
+ if parsed_data[:data].is_a?(Array)
+ new_collection(parsed_data)
+ else
+ new(parsed_data[:data])
+ end
+ 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)
@@ -125,6 +173,18 @@ def patch_resource(path, attrs={}) # {{{
end
end # }}}
+ # Make a DELETE request and return either a collection or a resource
+ def delete(path, attrs={}) # {{{
+ path = "#{@her_collection_path}/#{path}" if path.is_a?(Symbol)
+ delete_raw(path, attrs) do |parsed_data|
+ if parsed_data[:data].is_a?(Array)
+ new_collection(parsed_data)
+ else
+ new(parsed_data[:data])
+ end
+ 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)
View
@@ -99,6 +99,15 @@ class User
User.uses_api @api
end # }}}
+ it "handle GET wrapper method" do # {{{
+ @users = User.get(:popular)
+ @users.length.should == 2
+ @users.first.id.should == 1
+
+ @user = User.get(:"1")
+ @user.id.should == 1
+ end # }}}
+
it "handle raw GET" do # {{{
User.get_raw("/users") do |parsed_data|
parsed_data[:data].should == [{ :id => 1 }]

0 comments on commit 37aef13

Please sign in to comment.