class QuickbloxApi::BaseClient

Public Class Methods

new(opts = {}) click to toggle source
# File lib/quickblox_api/base_client.rb, line 5
def initialize(opts = {})
  raise(ArgumentError, 'secrets not specified') unless opts[:secrets].is_a?(Hash)
  opts.each do |name, value|
    instance_variable_set("@#{name}", value)
    self.class.send(:attr_reader, name)
  end
end

Public Instance Methods

query(type, path, body = {}) click to toggle source
# File lib/quickblox_api/base_client.rb, line 13
def query(type, path, body = {})
  _query(type, path, body.merge(token: get_token))
end

Private Instance Methods

_query(type, path, body) click to toggle source
# File lib/quickblox_api/base_client.rb, line 19
def _query(type, path, body)
  response = connection.send(type) do |request|
    request.url path
    request.headers['Content-Type'] = 'application/json'
    request.body = body.to_json.to_s
  end
  { status: response.status, body: safe_parse(response.body) }
end
connection() click to toggle source
# File lib/quickblox_api/base_client.rb, line 36
def connection
  @connection ||= Faraday.new(url: 'http://' + config[:server]) do |connection|
    connection.request  :url_encoded
    connection.adapter  Faraday.default_adapter
  end
end
get_token() click to toggle source
# File lib/quickblox_api/base_client.rb, line 28
def get_token
  response = _query(:post, '/session.json', sign(get_token_params))
  unless response[:status] == 201
    raise "Can't get authentication token\n #{JSON.pretty_generate(response)}"
  end
  response[:body][:session][:token]
end
get_token_params() click to toggle source
# File lib/quickblox_api/base_client.rb, line 48
def get_token_params
  {
    application_id: config[:application_id],
    auth_key: config[:auth_key],
    timestamp: Time.now.to_i,
    nonce: rand(10**10)
  }
end
sign(parameters) click to toggle source
# File lib/quickblox_api/base_client.rb, line 43
def sign(parameters)
  signature = HMAC::SHA1.hexdigest(config[:auth_secret], Encoder.encode!(parameters))
  parameters.merge!(signature: signature)
end