Class: CapitalOnTap::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/capital_on_tap/connection.rb

Constant Summary collapse

BASE_PATH =
'/api/v1/'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token:, expires_in: 1200, refresh_token: nil) ⇒ Connection

Returns a new instance of Connection.



20
21
22
23
24
# File 'lib/capital_on_tap/connection.rb', line 20

def initialize(access_token:, expires_in: 1200, refresh_token: nil)
  @access_token = access_token
  @expires_in = expires_in
  @refresh_token ||= refresh_token # do not overwrite if it's already set
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



12
13
14
# File 'lib/capital_on_tap/connection.rb', line 12

def access_token
  @access_token
end

#debugObject

Returns the value of attribute debug.



12
13
14
# File 'lib/capital_on_tap/connection.rb', line 12

def debug
  @debug
end

#expires_inObject

Returns the value of attribute expires_in.



12
13
14
# File 'lib/capital_on_tap/connection.rb', line 12

def expires_in
  @expires_in
end

#on_refresh_cbObject

This is a block to be run when the token is refreshed. This way you can do whatever you want with the new parameters returned by the refresh method.



16
17
18
# File 'lib/capital_on_tap/connection.rb', line 16

def on_refresh_cb
  @on_refresh_cb
end

#refresh_tokenObject

Returns the value of attribute refresh_token.



12
13
14
# File 'lib/capital_on_tap/connection.rb', line 12

def refresh_token
  @refresh_token
end

Instance Method Details

#get(path, params = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/capital_on_tap/connection.rb', line 26

def get(path, params = {})
  log "GET #{path} with #{params}"

  http_response = with_refresh { adapter.get(path, params) }

  log "Response: #{http_response.body}"

  Response.new(http_response)
end

#log(text) ⇒ Object



46
47
48
49
50
# File 'lib/capital_on_tap/connection.rb', line 46

def log(text)
  return unless CapitalOnTap.configuration.debug?

  puts Rainbow("[CapitalOnTap] #{text}").magenta.bright
end

#post(path, params = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/capital_on_tap/connection.rb', line 36

def post(path, params = {})
  log "POST #{path} with #{params}"

  http_response = with_refresh { adapter.post(path, params.to_json) }

  log "Response: #{http_response.body}"

  Response.new(http_response)
end