Class: FinAppsCore::REST::BaseClient
- Inherits:
-
Object
- Object
- FinAppsCore::REST::BaseClient
- Includes:
- Connection, Utils::Loggeable
- Defined in:
- lib/finapps_core/rest/base_client.rb
Overview
base client functionality
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
-
#connection ⇒ Object
Returns an initialized Faraday connection object.
-
#initialize(options, logger = nil) ⇒ BaseClient
constructor
A new instance of BaseClient.
-
#method_missing(method_id, *arguments, &block) ⇒ Object
Defines methods to perform HTTP GET, POST, PUT and DELETE requests.
- #respond_to_missing?(method_sym, include_private = false) ⇒ Boolean
-
#send_request(path, method, params = {}) ⇒ Hash, Array<String>
Performs HTTP GET, POST, UPDATE and DELETE requests.
Methods included from Connection
Methods included from Utils::Loggeable
Constructor Details
#initialize(options, logger = nil) ⇒ BaseClient
Returns a new instance of BaseClient.
13 14 15 16 |
# File 'lib/finapps_core/rest/base_client.rb', line 13 def initialize(, logger=nil) @config = FinAppsCore::REST::Configuration.new @logger = logger end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_id, *arguments, &block) ⇒ Object
Defines methods to perform HTTP GET, POST, PUT and DELETE requests. Returns a hash obtained from parsing the JSON object in the response body.
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/finapps_core/rest/base_client.rb', line 50 def method_missing(method_id, *arguments, &block) if %i(get post put delete).include? method_id connection.send(method_id) do |req| req.url arguments.first req.body = arguments[1] unless method_id == :get end else super end end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
11 12 13 |
# File 'lib/finapps_core/rest/base_client.rb', line 11 def config @config end |
Instance Method Details
#connection ⇒ Object
Returns an initialized Faraday connection object.
21 22 23 |
# File 'lib/finapps_core/rest/base_client.rb', line 21 def connection @connection ||= faraday(config, logger) end |
#respond_to_missing?(method_sym, include_private = false) ⇒ Boolean
61 62 63 |
# File 'lib/finapps_core/rest/base_client.rb', line 61 def respond_to_missing?(method_sym, include_private=false) [:get, :post, :put, :delete].include?(method_sym) ? true : super end |
#send_request(path, method, params = {}) ⇒ Hash, Array<String>
Performs HTTP GET, POST, UPDATE and DELETE requests. You shouldn’t need to use this method directly, but it can be useful for debugging. Returns a hash obtained from parsing the JSON object in the response body.
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/finapps_core/rest/base_client.rb', line 33 def send_request(path, method, params={}) raise FinAppsCore::MissingArgumentsError.new 'Missing argument: path.' if path.blank? raise FinAppsCore::MissingArgumentsError.new 'Missing argument: method.' if method.blank? response, = execute_request(path, method, params) result = if empty?(response) nil else block_given? ? yield(response) : response.body end [result, ] end |