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 || DEFAULT_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)


56
57
58
# File 'lib/zoho_hub/connection.rb', line 56

def access_token?
  @access_token.present?
end

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



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

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

  response = with_refresh do
    adapter(&block).get(path, params)
  end
  response.body
end

#log(text) ⇒ Object



64
65
66
67
68
# File 'lib/zoho_hub/connection.rb', line 64

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

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

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



38
39
40
41
42
43
44
45
# File 'lib/zoho_hub/connection.rb', line 38

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

  response = with_refresh do
    adapter(&block).post(path, params)
  end
  response.body
end

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



47
48
49
50
51
52
53
54
# File 'lib/zoho_hub/connection.rb', line 47

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

  response = with_refresh do
    adapter(&block).put(path, params)
  end
  response.body
end

#refresh_token?Boolean

Returns:

  • (Boolean)


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

def refresh_token?
  @refresh_token.present?
end