Class: Bitrix24CloudApi::Client

Inherits:
Base
  • Object
show all
Defined in:
lib/bitrix24_cloud_api/client.rb

Constant Summary collapse

B24_OAUTH_ENDPOINT =
'https://oauth.bitrix.info/oauth/token'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

resource_path, resource_url, to_query

Constructor Details

#initialize(attrs = {}) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/bitrix24_cloud_api/client.rb', line 11

def initialize(attrs = {})
  @extension = attrs[:extension] || "json"
  @endpoint = attrs[:endpoint]
  @access_token = attrs[:access_token]
  @client_id = attrs[:client_id]
  @client_secret = attrs[:client_secret]
  @redirect_uri = attrs[:redirect_uri]
  @scope = attrs[:scope]
  if @client_id && @client_secret
    @oauth2client = OAuth2::Client.new(@client_id, @client_secret, :site => api_endpoint)
  end
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



8
9
10
# File 'lib/bitrix24_cloud_api/client.rb', line 8

def access_token
  @access_token
end

#client_idObject (readonly)

Returns the value of attribute client_id.



8
9
10
# File 'lib/bitrix24_cloud_api/client.rb', line 8

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



8
9
10
# File 'lib/bitrix24_cloud_api/client.rb', line 8

def client_secret
  @client_secret
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



8
9
10
# File 'lib/bitrix24_cloud_api/client.rb', line 8

def endpoint
  @endpoint
end

#extensionObject (readonly)

Returns the value of attribute extension.



8
9
10
# File 'lib/bitrix24_cloud_api/client.rb', line 8

def extension
  @extension
end

#oauth2clientObject (readonly)

Returns the value of attribute oauth2client.



8
9
10
# File 'lib/bitrix24_cloud_api/client.rb', line 8

def oauth2client
  @oauth2client
end

#redirect_uriObject (readonly)

Returns the value of attribute redirect_uri.



8
9
10
# File 'lib/bitrix24_cloud_api/client.rb', line 8

def redirect_uri
  @redirect_uri
end

#scopeObject (readonly)

Returns the value of attribute scope.



8
9
10
# File 'lib/bitrix24_cloud_api/client.rb', line 8

def scope
  @scope
end

Instance Method Details

#api_endpointObject



24
25
26
# File 'lib/bitrix24_cloud_api/client.rb', line 24

def api_endpoint
  "https://#{endpoint}/rest/"
end

#authorize_urlObject



28
29
30
31
32
# File 'lib/bitrix24_cloud_api/client.rb', line 28

def authorize_url
  return nil unless oauth2client

  oauth2client.auth_code.authorize_url(:redirect_uri => redirect_uri, state: true)
end

#check_response(response) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/bitrix24_cloud_api/client.rb', line 89

def check_response(response)
  if response.success?
    response.parsed_response
  else
    response.parsed_response.merge(code: response.code)
  end
end

#contactsObject



105
106
107
# File 'lib/bitrix24_cloud_api/client.rb', line 105

def contacts
  Bitrix24CloudApi::CRM::Contact.list(self)
end

#dealsObject



97
98
99
# File 'lib/bitrix24_cloud_api/client.rb', line 97

def deals
  Bitrix24CloudApi::CRM::Deal.list(self)
end

#get_access_token(code) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/bitrix24_cloud_api/client.rb', line 34

def get_access_token(code)
  return nil unless oauth2client

  auth_token_query = { client_id: client_id,
                       client_secret: client_secret,
                       grant_type: 'authorization_code',
                       code: code }
  auth_token_path = "#{B24_OAUTH_ENDPOINT}?#{to_query(auth_token_query)}"
  oauth2client.options[:token_url] = auth_token_path
  begin
    token = oauth2client.auth_code.get_token(code, :redirect_uri => redirect_uri)
    token.params.merge({ access_token: token.token,
                         refresh_token: token.refresh_token,
                         expires_at: token.expires_at })
  rescue OAuth2::Error
    return nil
  end
end

#leadsObject



101
102
103
# File 'lib/bitrix24_cloud_api/client.rb', line 101

def leads
  Bitrix24CloudApi::CRM::Lead.list(self)
end

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



73
74
75
76
77
78
79
# File 'lib/bitrix24_cloud_api/client.rb', line 73

def make_get_request(path, params = {})
  params.merge!(auth: access_token)
  response = HTTParty.get(path,
                          query: params,
                          query_string_normalizer: ->(query) { Bitrix24CloudApi::HashConversions.to_params(query) })
  check_response(response)
end

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



81
82
83
84
85
86
87
# File 'lib/bitrix24_cloud_api/client.rb', line 81

def make_post_request(path, params = {})
  response = HTTParty.post(path,
                           body: params,
                           query: { auth: access_token },
                           query_string_normalizer: ->(query) { Bitrix24CloudApi::HashConversions.to_params(query) })
  check_response(response)
end

#refresh_token(refresh_token) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/bitrix24_cloud_api/client.rb', line 53

def refresh_token(refresh_token)
  return nil unless oauth2client

  auth_token_query = { client_id: client_id,
                       client_secret: client_secret,
                       grant_type: 'refresh_token',
                       refresh_token: refresh_token }
  auth_token_path = "#{B24_OAUTH_ENDPOINT}?#{to_query(auth_token_query)}"
  oauth2client.options[:token_url] = auth_token_path

  begin
    token = oauth2client.get_token(auth_token_query)
    token.params.merge({ access_token: token.token,
                         refresh_token: token.refresh_token,
                         expires_at: token.expires_at })
  rescue OAuth2::Error
    return false
  end
end