Class: ApiStruct::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/api_struct/client.rb

Constant Summary collapse

DEFAULT_HEADERS =
{
  'Accept': 'application/json',
  'Content-Type': 'application/json'
}
URL_OPTION_REGEXP =
/\/:([a-z_]+)/.freeze
HTTP_METHODS =
%i[get post patch put delete].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



37
38
39
40
41
# File 'lib/api_struct/client.rb', line 37

def initialize
  api_settings_exist
  client_headers = headers || DEFAULT_HEADERS
  @client = HTTP::Client.new(headers: client_headers)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



9
10
11
# File 'lib/api_struct/client.rb', line 9

def client
  @client
end

Class Method Details

.method_missing(method_name, *args, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/api_struct/client.rb', line 11

def self.method_missing(method_name, *args, &block)
  endpoints = Settings.config.endpoints
  return super unless endpoints.keys.include?(method_name)

  define_method(:api_root) { endpoints[method_name][:root] }
  define_method(:default_params) { endpoints[method_name][:params] || {} }
  define_method(:default_path) { first_arg(args) }

  define_method(:headers) do
    endpoints[method_name][:headers]
  end
end