class Layer::Client::REST

Attributes

app_id[R]
block[R]
token[R]

Public Class Methods

authenticate(app_id, nonce = nil) { |nonce| ... } click to toggle source
# File lib/layer/client/rest.rb, line 5
def self.authenticate(app_id, nonce = nil)
  client = Layer::Client.new

  nonce ||= client.post('/nonces')['nonce']

  identity_token = yield(nonce)

  response = client.post('/sessions', { identity_token: identity_token, app_id: app_id })
  response['session_token']
end
new(app_id = Layer::Client.app_id, &block) click to toggle source
# File lib/layer/client/rest.rb, line 18
def initialize(app_id = Layer::Client.app_id, &block)
  @app_id = self.class.normalize_id(app_id)
  @block = block
  authenticate
end

Private Instance Methods

authenticate(nonce = nil) click to toggle source
# File lib/layer/client/rest.rb, line 37
def authenticate(nonce = nil)
  @token = self.class.authenticate(app_id, nonce, &block)
end
request(method, url, payload = {}, headers = {}) click to toggle source
Calls superclass method Layer::Client#request
# File lib/layer/client/rest.rb, line 26
def request(method, url, payload = {}, headers = {})
  url = "https://api.layer.com#{url}" unless url.start_with?('https://api.layer.com')
  headers['Accept'] ||= 'application/vnd.layer+json; version=1.0'
  headers['Authorization'] ||= "Layer session-token=\"#{token}\""

  super
rescue Layer::Exceptions::AuthenticationRequired => exception
  authenticate(exception.response_json['data']['nonce'])
  retry
end