Class: Racli::CLI
- Inherits:
-
Object
- Object
- Racli::CLI
- Defined in:
- lib/racli/cli.rb
Instance Method Summary collapse
- #add_alias(alias_name, method = 'GET', path = '/', headers = {}) ⇒ Object
- #add_handler(handler_klass) ⇒ Object
- #call(method:, path:, params:, headers: @default_headers) ⇒ Object
- #default_headers(headers) ⇒ Object
-
#initialize(config:, rcfile:) ⇒ CLI
constructor
A new instance of CLI.
Constructor Details
#initialize(config:, rcfile:) ⇒ CLI
Returns a new instance of CLI.
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/racli/cli.rb', line 11 def initialize(config:, rcfile:) config_ruby_string = File.read(config) eval_string = "::Racli::Rack.new { #{config_ruby_string} }.to_app" @rackapp = TOPLEVEL_BINDING.eval eval_string, '(racli)', 0 @default_handler = Racli::Handlers::DefaultHandler.new(self) @handlers = [] @aliases = {} @default_headers = {} eval(File.read(rcfile)) if File.exist?(rcfile) end |
Instance Method Details
#add_alias(alias_name, method = 'GET', path = '/', headers = {}) ⇒ Object
51 52 53 |
# File 'lib/racli/cli.rb', line 51 def add_alias(alias_name, method = 'GET', path = '/', headers = {}) @aliases[alias_name] = { method: method, path: path, headers: headers } end |
#add_handler(handler_klass) ⇒ Object
46 47 48 49 |
# File 'lib/racli/cli.rb', line 46 def add_handler(handler_klass) handler = handler_klass.new(self) @handlers.push handler end |
#call(method:, path:, params:, headers: @default_headers) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/racli/cli.rb', line 23 def call(method:, path:, params:, headers: @default_headers) if @aliases.include?(method.to_sym) alias_setting = @aliases[method.to_sym] method = alias_setting[:method] path = alias_setting[:path] headers.merge!(alias_setting[:headers]) end request_params = request_params( method: method, path: path, params: params, headers: headers ) status, headers, body = @rackapp.call(request_params) handle_response( status: status, headers: headers, body: body, original_args: { method: method, path: path, params: params } ) end |
#default_headers(headers) ⇒ Object
55 56 57 |
# File 'lib/racli/cli.rb', line 55 def default_headers(headers) @default_headers = headers end |