Class: Bitrix24CloudApi::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

resource_path, resource_url

Constructor Details

#initialize(attrs = {}) ⇒ Client

Returns a new instance of Client.



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

def initialize(attrs = {})
  puts attrs[:extension]
  @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.



6
7
8
# File 'lib/bitrix24_cloud_api/client.rb', line 6

def access_token
  @access_token
end

#client_idObject (readonly)

Returns the value of attribute client_id.



6
7
8
# File 'lib/bitrix24_cloud_api/client.rb', line 6

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



6
7
8
# File 'lib/bitrix24_cloud_api/client.rb', line 6

def client_secret
  @client_secret
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



6
7
8
# File 'lib/bitrix24_cloud_api/client.rb', line 6

def endpoint
  @endpoint
end

#extensionObject (readonly)

Returns the value of attribute extension.



6
7
8
# File 'lib/bitrix24_cloud_api/client.rb', line 6

def extension
  @extension
end

#oauth2clientObject (readonly)

Returns the value of attribute oauth2client.



6
7
8
# File 'lib/bitrix24_cloud_api/client.rb', line 6

def oauth2client
  @oauth2client
end

#redirect_uriObject (readonly)

Returns the value of attribute redirect_uri.



6
7
8
# File 'lib/bitrix24_cloud_api/client.rb', line 6

def redirect_uri
  @redirect_uri
end

#scopeObject (readonly)

Returns the value of attribute scope.



6
7
8
# File 'lib/bitrix24_cloud_api/client.rb', line 6

def scope
  @scope
end

Instance Method Details

#api_endpointObject



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

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

#authorize_urlObject



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

def authorize_url
  return nil unless oauth2client

  oauth2client.auth_code.authorize_url(:redirect_uri => redirect_uri)
end

#check_response(response) ⇒ Object



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

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

#contactsObject



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

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

#dealsObject



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

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

#get_access_token(code) ⇒ Object



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

def get_access_token(code)
  return nil unless oauth2client

  auth_token_query = {}
  auth_token_query[:client_id] = client_id
  auth_token_query[:client_secret] = client_secret
  auth_token_query[:grant_type] = "authorization_code"
  auth_token_query[:scope] = scope
  auth_token_query[:redirect_uri] = redirect_uri
  auth_token_path = oauth2client.options[:token_url] + "?#{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



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

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

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



76
77
78
79
80
# File 'lib/bitrix24_cloud_api/client.rb', line 76

def make_get_request(path, params = {})
  params.merge!(auth: access_token)
  response = HTTParty.get(path, query: params)
  check_response(response)
end

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



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

def make_post_request(path, params = {})
  params.merge!(auth: access_token)
  response = HTTParty.post(path, query: params)
  check_response(response)
end

#refresh_token(refresh_token_hash) ⇒ Object



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

def refresh_token(refresh_token_hash)
  return nil unless oauth2client

  auth_token_query = {}
  auth_token_query[:client_id] = client_id
  auth_token_query[:client_secret] = client_secret
  auth_token_query[:grant_type] = 'refresh_token'
  auth_token_query[:refresh_token] = refresh_token_hash
  auth_token_path = "/oauth/token?#{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