Class: Fintoc::BaseClient

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/fintoc/base_client.rb

Direct Known Subclasses

V1::Client, V2::Client

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#flatten, #pick, #pluralize, #snake_to_pascal

Constructor Details

#initialize(api_key, jws_private_key: nil) ⇒ BaseClient



15
16
17
18
19
20
21
22
23
# File 'lib/fintoc/base_client.rb', line 15

def initialize(api_key, jws_private_key: nil)
  @api_key = api_key
  @user_agent = "fintoc-ruby/#{Fintoc::VERSION}"
  @headers = { Authorization: "Bearer #{@api_key}", 'User-Agent': @user_agent }
  @link_headers = nil
  @link_header_pattern = '<(?<url>.*)>;\s*rel="(?<rel>.*)"'
  @default_params = {}
  @jws = jws_private_key ? Fintoc::JWS.new(jws_private_key) : nil
end

Instance Attribute Details

#default_paramsObject

Returns the value of attribute default_params.



13
14
15
# File 'lib/fintoc/base_client.rb', line 13

def default_params
  @default_params
end

Instance Method Details

#delete(version: :v1) ⇒ Object



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

def delete(version: :v1)
  request('delete', version: version)
end

#fetch_nextObject



56
57
58
59
60
61
62
63
64
# File 'lib/fintoc/base_client.rb', line 56

def fetch_next
  next_ = link_headers['next']
  Enumerator.new do |yielder|
    while next_
      yielder << get.call(next_)
      next_ = link_headers['next']
    end
  end
end

#get(version: :v1) ⇒ Object



25
26
27
# File 'lib/fintoc/base_client.rb', line 25

def get(version: :v1)
  request('get', version: version)
end

#patch(version: :v1, use_jws: false) ⇒ Object



37
38
39
# File 'lib/fintoc/base_client.rb', line 37

def patch(version: :v1, use_jws: false)
  request('patch', version: version, use_jws: use_jws)
end

#post(version: :v1, use_jws: false) ⇒ Object



33
34
35
# File 'lib/fintoc/base_client.rb', line 33

def post(version: :v1, use_jws: false)
  request('post', version: version, use_jws: use_jws)
end

#request(method, version: :v1, use_jws: false) ⇒ Object



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

def request(method, version: :v1, use_jws: false)
  proc do |resource, **kwargs|
    parameters = params(method, **kwargs)
    response = make_request(method, resource, parameters, version: version, use_jws: use_jws)
    content = JSON.parse(response.body, symbolize_names: true)

    if response.status.client_error? || response.status.server_error?
      raise_custom_error(content[:error])
    end

    @link_headers = response.headers.get('link')
    content
  end
end

#to_sObject



66
67
68
69
70
71
# File 'lib/fintoc/base_client.rb', line 66

def to_s
  visible_chars = 8
  visible_key = @api_key.slice(0, visible_chars)
  hidden_part = '*' * (@api_key.size - visible_chars)
  "#{self.class.name}(🔑=#{visible_key + hidden_part})"
end