Class: FinAppsCore::REST::BaseClient

Inherits:
Object
  • Object
show all
Includes:
Connection, Utils::Loggeable
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

faraday

Methods included from Utils::Loggeable

#logger

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(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.



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

#configObject (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

#connectionObject

Returns an initialized Faraday connection object.

Returns:

  • Faraday::Connection.



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

Returns:

  • (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.

Parameters:

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

Returns:

  • (Hash, Array<String>)


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, error_messages = execute_request(path, method, params)
  result = if empty?(response)
             nil
           else
             block_given? ? yield(response) : response.body
           end

  [result, error_messages]
end