Class: ApiccoSDK::Client

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/apicco-sdk/client.rb

Constant Summary

Constants included from Helpers

Helpers::ACRONYMS_UNDERSCORE_REGEX, Helpers::ACRONYM_REGEX

Instance Method Summary collapse

Methods included from Helpers

#underscore

Constructor Details

#initialize(origin, rel_path: 'api/v1', api: {}, intercept: ->(req) { req }) ⇒ Client

Returns a new instance of Client.



5
6
7
8
9
10
# File 'lib/apicco-sdk/client.rb', line 5

def initialize(origin, rel_path:'api/v1', api:{}, intercept: ->(req) { req })
  @rel_path = rel_path
  @origin = origin
  @intercept = intercept
  @api = discover_api(api)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/apicco-sdk/client.rb', line 12

def method_missing(m, *args, &block)
  method_name = m.to_s
  super unless @api.key?(method_name)

  data = (args[0] || {}).transform_keys!(&:to_s) 

  action, params = @api[method_name]

  required_params = params.inject([]) do |memo, p|
    memo << p[1..-1] if p[0] == "*"
    memo
  end

  missing_params = required_params.select { |p| data[p].nil? }

  fail "#{m} missing params: #{missing_params.join(", ")}" if missing_params.any?
  request(:post, "#{@url}/#{action}", data.to_json)
end

Instance Method Details

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

Returns:

  • (Boolean)


31
32
33
# File 'lib/apicco-sdk/client.rb', line 31

def respond_to_missing?(method_name, include_private = false)
  @api.key?(method_name) || super
end