class Bitrix24CloudApi::Client

Constants

B24_OAUTH_ENDPOINT

Attributes

access_token[RW]
client_id[R]
client_secret[R]
endpoint[R]
extension[R]
oauth2client[R]
redirect_uri[R]
scope[R]

Public Class Methods

new(attrs = {}) click to toggle source
# 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

Public Instance Methods

api_endpoint() click to toggle source
# File lib/bitrix24_cloud_api/client.rb, line 24
def api_endpoint
  "https://#{endpoint}/rest/"
end
authorize_url() click to toggle source
# 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) click to toggle source
# File lib/bitrix24_cloud_api/client.rb, line 84
def check_response(response)
  if response.success?
    response.parsed_response
  else
    response.parsed_response.merge(code: response.code)
  end
end
contacts() click to toggle source
# File lib/bitrix24_cloud_api/client.rb, line 100
def contacts
  Bitrix24CloudApi::CRM::Contact.list(self)
end
deals() click to toggle source
# File lib/bitrix24_cloud_api/client.rb, line 92
def deals
  Bitrix24CloudApi::CRM::Deal.list(self)
end
get_access_token(code) click to toggle source
# 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
leads() click to toggle source
# File lib/bitrix24_cloud_api/client.rb, line 96
def leads
  Bitrix24CloudApi::CRM::Lead.list(self)
end
make_get_request(path, params = {}) click to toggle source
# 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)
  check_response(response)
end
make_post_request(path, params = {}) click to toggle source
# File lib/bitrix24_cloud_api/client.rb, line 79
def make_post_request(path, params = {})
  response = HTTParty.post(path, body: params, query: {auth: access_token})
  check_response(response)
end
refresh_token(refresh_token) click to toggle source
# 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