class FinAppsCore::REST::BaseClient
base client functionality
Attributes
config[R]
Public Class Methods
new(options, logger = nil)
click to toggle source
# File lib/finapps_core/rest/base_client.rb 21 def initialize(options, logger = nil) 22 @config = ::FinAppsCore::REST::Configuration.new options 23 @logger = logger 24 end
Public Instance Methods
connection()
click to toggle source
Returns an initialized Faraday connection object.
@return Faraday::Connection.
# File lib/finapps_core/rest/base_client.rb 29 def connection 30 @connection ||= faraday(config, logger) 31 end
method_missing(method_id, *arguments, &block)
click to toggle source
Defines methods to perform HTTP GET, POST, PUT and DELETE requests. Returns a hash obtained from parsing the JSON object in the response body.
Calls superclass method
# File lib/finapps_core/rest/base_client.rb 55 def method_missing(method_id, *arguments, &block) 56 if %i[get post put delete].include? method_id 57 send_to_connection method_id, arguments 58 else 59 super 60 end 61 end
respond_to_missing?(method_sym, include_private = false)
click to toggle source
Calls superclass method
# File lib/finapps_core/rest/base_client.rb 63 def respond_to_missing?(method_sym, include_private = false) 64 %i[get post put delete].include?(method_sym) ? true : super 65 end
send_request(path, method, params = {}) { |response| ... }
click to toggle source
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.
@param [String] path @param [String] method @param [Hash] params @return [Hash,Array<String>]
# File lib/finapps_core/rest/base_client.rb 42 def send_request(path, method, params = {}) 43 not_blank(path, :path) 44 not_blank(method, :method) 45 46 response, error_messages = execute_request(method, path, params) 47 result = block_given? ? yield(response) : response_body(response) 48 49 [result, error_messages] 50 end
Private Instance Methods
execute_method(method, path, params)
click to toggle source
# File lib/finapps_core/rest/base_client.rb 100 def execute_method(method, path, params) 101 unless %i(get post put delete).include?(method) 102 fail FinAppsCore::UnsupportedHttpMethodError, 103 "Method not supported: #{method}." 104 end 105 106 send(method, path, params) 107 end
execute_request(method, path, params)
click to toggle source
# File lib/finapps_core/rest/base_client.rb 80 def execute_request(method, path, params) 81 [send(method, path, params), []] 82 rescue FinAppsCore::InvalidArgumentsError, 83 FinAppsCore::MissingArgumentsError, 84 Faraday::ConnectionFailed => e 85 [nil, handle_error(e)] 86 rescue Faraday::ClientError => e 87 [nil, handle_client_error(e)] 88 end
handle_client_error(error)
click to toggle source
# File lib/finapps_core/rest/base_client.rb 95 def handle_client_error(error) 96 logger.warn "#{self.class}##{__method__} => #{error.class.name}, #{error}" 97 error.response && error.response[:error_messages] ? error.response[:error_messages] : [error.message] 98 end
handle_error(error)
click to toggle source
# File lib/finapps_core/rest/base_client.rb 90 def handle_error(error) 91 logger.fatal "#{self.class}##{__method__} => #{error}" 92 fail error 93 end
response_body(response)
click to toggle source
# File lib/finapps_core/rest/base_client.rb 69 def response_body(response) 70 response.body if response.respond_to?(:body) && !response.body.to_s.empty? 71 end
send_to_connection(method_id, arguments)
click to toggle source
# File lib/finapps_core/rest/base_client.rb 73 def send_to_connection(method_id, arguments) 74 connection.send(method_id) do |req| 75 req.url arguments.first 76 req.body = arguments[1] unless method_id == :get 77 end 78 end