Skip to content

Commit

Permalink
Add support for custom_<method> methods
Browse files Browse the repository at this point in the history
  • Loading branch information
remi committed Apr 22, 2012
1 parent 37aef13 commit 2c0c171
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
21 changes: 20 additions & 1 deletion README.md
Expand Up @@ -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
Expand Down
50 changes: 50 additions & 0 deletions lib/her/model/http.rb
Expand Up @@ -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
32 changes: 32 additions & 0 deletions spec/model/http_spec.rb
Expand Up @@ -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

0 comments on commit 2c0c171

Please sign in to comment.