Class: ZohoHub::Connection

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

Constant Summary collapse

DEFAULT_DOMAIN =
'https://www.zohoapis.eu'
BASE_PATH =
'/crm/v2/'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token:, api_domain: DEFAULT_DOMAIN, expires_in: 3600, refresh_token: nil) ⇒ Connection

Returns a new instance of Connection.



22
23
24
25
26
27
# File 'lib/zoho_hub/connection.rb', line 22

def initialize(access_token:, api_domain: DEFAULT_DOMAIN, expires_in: 3600, refresh_token: nil)
  @access_token = access_token
  @expires_in = expires_in
  @api_domain = api_domain
  @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/zoho_hub/connection.rb', line 12

def access_token
  @access_token
end

#api_domainObject

Returns the value of attribute api_domain.



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

def api_domain
  @api_domain
end

#debugObject

Returns the value of attribute debug.



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

def debug
  @debug
end

#expires_inObject

Returns the value of attribute expires_in.



12
13
14
# File 'lib/zoho_hub/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/zoho_hub/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/zoho_hub/connection.rb', line 12

def refresh_token
  @refresh_token
end

Instance Method Details

#access_token?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/zoho_hub/connection.rb', line 50

def access_token?
  @access_token.present?
end

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



29
30
31
32
33
34
# File 'lib/zoho_hub/connection.rb', line 29

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

  response = with_refresh { adapter.get(path, params) }
  response.body
end

#log(text) ⇒ Object



58
59
60
61
62
# File 'lib/zoho_hub/connection.rb', line 58

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

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

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



36
37
38
39
40
41
# File 'lib/zoho_hub/connection.rb', line 36

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

  response = with_refresh { adapter.post(path, params) }
  response.body
end

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



43
44
45
46
47
48
# File 'lib/zoho_hub/connection.rb', line 43

def put(path, params = {})
  log "PUT #{path} with #{params}"

  response = with_refresh { adapter.put(path, params) }
  response.body
end

#refresh_token?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/zoho_hub/connection.rb', line 54

def refresh_token?
  @refresh_token.present?
end