Class: FinAppsCore::REST::BaseClient

Inherits:
Object
  • Object
show all
Includes:
Connection, Utils::Loggeable, Utils::Validatable
Defined in:
lib/finapps_core/rest/base_client.rb

Overview

base client functionality

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Connection

connection_options, faraday

Methods included from Utils::Validatable

#nil_or_empty?, #not_blank

Methods included from Utils::Loggeable

#logger

Constructor Details

#initialize(options, logger = nil) ⇒ BaseClient

Returns a new instance of BaseClient.



21
22
23
24
# File 'lib/finapps_core/rest/base_client.rb', line 21

def initialize(options, logger = nil)
  @config = ::FinAppsCore::REST::Configuration.new options
  @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.



58
59
60
61
62
63
64
# File 'lib/finapps_core/rest/base_client.rb', line 58

def method_missing(method_id, *arguments, &block)
  if %i[get post put delete].include? method_id
    send_to_connection method_id, arguments
  else
    super
  end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



19
20
21
# File 'lib/finapps_core/rest/base_client.rb', line 19

def config
  @config
end

Instance Method Details

#connectionObject

Returns an initialized Faraday connection object.

Returns:

  • Faraday::Connection.



29
30
31
# File 'lib/finapps_core/rest/base_client.rb', line 29

def connection
  @connection ||= faraday(config, logger)
end

#respond_to_missing?(method_sym, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/finapps_core/rest/base_client.rb', line 66

def respond_to_missing?(method_sym, include_private = false)
  %i[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.

Parameters:

  • path (String)
  • method (String)
  • params (Hash) (defaults to: {})

Returns:

  • (Hash, Array<String>)


41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/finapps_core/rest/base_client.rb', line 41

def send_request(path, method, params = {})
  not_blank(path, :path)
  not_blank(method, :method)

  response, error_messages = execute_request(path, method, params)
  result = if empty?(response)
             nil
           else
             block_given? ? yield(response) : response.body
           end

  [result, error_messages]
end