Skip to content

Instantly share code, notes, and snippets.

@twoism
Created May 8, 2009 19:36
Show Gist options
  • Select an option

  • Save twoism/108962 to your computer and use it in GitHub Desktop.

Select an option

Save twoism/108962 to your computer and use it in GitHub Desktop.
# Sample usage:
# class Collection
# include Typhoeus
# remote_defaults :on_success => lambda {|response| JSON.parse(response.body)},
# :on_failure => lambda {|response| puts "error code: #{response.code}"},
# :base_uri => "http://localhost:9292",
# :is_resource => true
# end
module Typhoeus
module ClassMethods
def build_resource_methods
resource_name = self.to_s.tableize
{
:all => {
:path => "/#{resource_name}.json",
:method => :get
},
:create => {
:path => "/#{resource_name}.json",
:method => :post
},
:show => {
:path => "/#{resource_name}/:id.json",
:method => :get
},
:update => {
:path => "/#{resource_name}/:id.json",
:method => :post
},
:destroy => {
:path => "/#{resource_name}/:id.json",
:method => :delete
}
}.each do |m,args|
define_remote_method m, args
end
end
# overrides Typhoeus::ClassMethods::remote_defaults
def remote_defaults(options)
@remote_defaults = options
build_resource_methods unless options[:is_resource].nil?
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment