Skip to content

Instantly share code, notes, and snippets.

@drochetti
Created September 18, 2014 15:48
Show Gist options
  • Save drochetti/0c38d9b98863a2c3cfad to your computer and use it in GitHub Desktop.
Save drochetti/0c38d9b98863a2c3cfad to your computer and use it in GitHub Desktop.
ruby rest client poc
module Blowout
module Api
class BaseResource < Hashie::Mash
# include RestCore
# include Blowout::Api
# include Blowout::Api::Client
def self.set_client(client)
@client = client
end
def self.client
@client || Blowout::Api::Client.new
end
def self.find(id, *cb)
client.get "#{base_path}/#{id}"
end
def self.fetch(filter={}, *cb)
get "#{base_path}"
end
def create(*cb)
post "#{base_path}"
end
def update(*cb)
put "#{base_path}/#{id}"
end
def destroy(*cb)
delete "#{base_path}/#{id}"
end
protected
def self.resource_name
self.name.split('::').last.downcase
end
def self.base_path
"/#{resource_name}/"
end
end
end
end
@drochetti
Copy link
Author

This is the class that all resource models will inherit from, for example:

class Spider < Blowout::Api::BaseResource

  # non CRUD method
  def schedule when
    post "#{base_path}/#{@id}/schedule", {when: when}
  end

  # getting nested resource 'Job'
  def jobs
    client.jobs.fetch(spider_id: @id)
  end

end

#now I can call
spider = Spider.find(123)
jobs = spider.jobs
scheduled_job = spider.schedule 20.minutes.from_now

@julioprotzek
Copy link

What exactly is not working the way it should?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment