class QuickBlox::Session

Attributes

_id[RW]
application_id[RW]
created_at[RW]
device_id[RW]
id[RW]
nonce[RW]
token[RW]
ts[RW]
updated_at[RW]
user_id[RW]

Public Class Methods

new() click to toggle source
# File lib/quick_blox/session.rb, line 20
def initialize
  raise QuickBlox::Exceptions::MissingConfiguration unless QuickBlox.configuration

  timestamp = Time.now.in_time_zone('UTC').to_i
  nonce = timestamp-425346
  signature = HMAC::SHA1.hexdigest(QuickBlox.configuration.auth_secret, "application_id=#{ QuickBlox.configuration.application_id }&auth_key=#{ QuickBlox.configuration.auth_key }&nonce=#{ nonce }&timestamp=#{ timestamp }")

  RestClient::Request.execute(
      method: :post,
      url: "#{ QuickBlox.configuration.host }/session.json",
      payload: {
          application_id: QuickBlox.configuration.application_id,
          auth_key: QuickBlox.configuration.auth_key,
          signature: signature,
          timestamp: timestamp,
          nonce: nonce
      }.to_json,
      headers: {
          'Content-Type': 'application/json',
          'QuickBlox-REST-API-Version': QuickBlox.configuration.api_version,
      }
  ){ |response, request, result|

    response = JSON.parse(response)

    case result.code.to_i
      when 201
        response['session'].each do |k, v|
          self.instance_variable_set "@#{k}", v
        end
      when 422
        raise QuickBlox::Exceptions::Response, response['errors']
    end
  }
end

Public Instance Methods

destroy() click to toggle source
# File lib/quick_blox/session.rb, line 79
def destroy
  RestClient::Request.execute(
      method: :delete,
      url: "#{ QuickBlox.configuration.host }/session.json",
      headers: {
          'Content-Type': 'application/json',
          'QuickBlox-REST-API-Version': QuickBlox.configuration.api_version,
          'QB-Token': self.token
      }
  ){ |response, request, result|
    case result.code.to_i
      when 200

      else
        response = JSON.parse(response)
        raise QuickBlox::Exceptions::Response, response['errors']
    end
  }
end
info() click to toggle source
# File lib/quick_blox/session.rb, line 56
def info
  RestClient::Request.execute(
      method: :get,
      url: "#{ QuickBlox.configuration.host }/session.json",
      headers: {
          'Content-Type': 'application/json',
          'QuickBlox-REST-API-Version': QuickBlox.configuration.api_version,
          'QB-Token': self.token
      }
  ){ |response, request, result|
    response = JSON.parse(response)
    case result.code.to_i
      when 201
        response['session'].each do |k, v|
          self.instance_variable_set "@#{k}", v
        end
      when 422
        raise QuickBlox::Exceptions::Response, response['errors']
    end
  }
  self
end