Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add support for custom_<method> methods

  • Loading branch information...
commit 2c0c1718471a626cb63b667b2f7232ddde75cd06 1 parent 37aef13
@remiprev authored
Showing with 102 additions and 1 deletion.
  1. +20 −1 README.md
  2. +50 −0 lib/her/model/http.rb
  3. +32 −0 spec/model/http_spec.rb
View
21 README.md
@@ -186,7 +186,26 @@ 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`, `post`, `put` or `delete` (which maps the returned data to either a collection or a resource).
+You can easily define custom requests for your models using `custom_get`, `custom_post`, etc.
+
+```ruby
+class User
+ include Her::Model
+ custom_get :popular, :unpopular
+ custom_post :from_default
+end
+
+User.popular # => [#<User id=1>, #<User id=2>]
+# GET /users/popular
+
+User.unpopular # => [#<User id=3>, #<User id=4>]
+# GET /users/unpopular
+
+User.from_default(:name => "Maeby Fünke") # => #<User id=5>
+# POST /users/from_default?name=Maeby+Fünke
+```
+
+You can also use `get`, `post`, `put` or `delete` (which maps the returned data to either a collection or a resource).
```ruby
class User
View
50 lib/her/model/http.rb
@@ -206,6 +206,56 @@ def delete_resource(path, attrs={}) # {{{
new(parsed_data[:data])
end
end # }}}
+
+ # Define custom GET requests
+ def custom_get(*paths) # {{{
+ metaclass = (class << self; self; end)
+ paths.each do |path|
+ metaclass.send(:define_method, path.to_sym) do |*attrs|
+ get(path, attrs.first || Hash.new)
+ end
+ end
+ end # }}}
+
+ # Define custom POST requests
+ def custom_post(*paths) # {{{
+ metaclass = (class << self; self; end)
+ paths.each do |path|
+ metaclass.send(:define_method, path.to_sym) do |*attrs|
+ post(path, attrs.first || Hash.new)
+ end
+ end
+ end # }}}
+
+ # Define custom PUT requests
+ def custom_put(*paths) # {{{
+ metaclass = (class << self; self; end)
+ paths.each do |path|
+ metaclass.send(:define_method, path.to_sym) do |*attrs|
+ put(path, attrs.first || Hash.new)
+ end
+ end
+ end # }}}
+
+ # Define custom PATCH requests
+ def custom_patch(*paths) # {{{
+ metaclass = (class << self; self; end)
+ paths.each do |path|
+ metaclass.send(:define_method, path.to_sym) do |*attrs|
+ patch(path, attrs.first || Hash.new)
+ end
+ end
+ end # }}}
+
+ # Define custom DELETE requests
+ def custom_delete(*paths) # {{{
+ metaclass = (class << self; self; end)
+ paths.each do |path|
+ metaclass.send(:define_method, path.to_sym) do |*attrs|
+ delete(path, attrs.first || Hash.new)
+ end
+ end
+ end # }}}
end
end
end
View
32 spec/model/http_spec.rb
@@ -172,4 +172,36 @@ class User
end
end # }}}
end
+
+ context "setting custom requests" do
+ before do # {{{
+ @api = Her::API.new
+ @api.setup :base_uri => "https://api.example.com"
+ FakeWeb.register_uri(:get, "https://api.example.com/users/popular", :body => { :data => [{ :id => 1 }, { :id => 2 }] }.to_json)
+ FakeWeb.register_uri(:post, "https://api.example.com/users/from_default", :body => { :data => { :id => 4 } }.to_json)
+
+ class User
+ include Her::Model
+ end
+ User.custom_get :popular, :foobar
+ User.custom_post :from_default
+ end # }}}
+
+ it "handles custom methods" do # {{{
+ User.respond_to?(:popular).should be_true
+ User.respond_to?(:foobar).should be_true
+ User.respond_to?(:from_default).should be_true
+ end # }}}
+
+ it "handles custom GET requests" do # {{{
+ @users = User.popular
+ @users.length.should == 2
+ @users.first.id.should == 1
+ end # }}}
+
+ it "handles custom POST requests" do # {{{
+ @user = User.from_default(:name => "Tobias Fünke")
+ @user.id.should be_true
+ end # }}}
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.