Skip to content

Instantly share code, notes, and snippets.

@mashiro
Created July 26, 2013 11:59
Show Gist options
  • Select an option

  • Save mashiro/6088331 to your computer and use it in GitHub Desktop.

Select an option

Save mashiro/6088331 to your computer and use it in GitHub Desktop.
module MyApp
module API
class Railtie < Rails::Railtie
class Command
def initialize(user = nil)
@user = user || User.first
end
%i(get post patch put delete head).each do |method|
define_method(method) do |path, parameters = {}, headers_or_env = {}|
app.__send__ method, path, default_params.merge(parameters), default_headers.merge(headers_or_env)
ActiveSupport::JSON.decode app.response.body
end
end
private
def default_params
{
'api_key' => @user.api_key
}
end
def default_headers
{
'Content-Type' => 'application/json',
'Accept' => 'application/json'
}
end
def app
TOPLEVEL_BINDING.eval('app')
end
end
module ClassMethods
def api
@api ||= Command.new
end
end
console do
TOPLEVEL_BINDING.eval('self').extend ClassMethods
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment