Created
July 26, 2013 11:59
-
-
Save mashiro/6088331 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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