class Reuters::Client::Token

Retrieves a new Token from the Reuter’s API using the credentials that have been globally configured.

@note A retrieved token is stored as a static variable, attached to

this class. It will only be retrieved once, and then again up expiry.

@example Retrieving a new token

Reuters::Credentials.configure do |c|
  c.username = 'username'
  c.password = 'pass'
  c.app_id = 'MyAppID'
end
authentication = Reuters::Client::Token.new
authentication.token #=> "raj2ja89djf98aj3jjoadjowajdoiaj"

Public Class Methods

new() click to toggle source

Override {Base} constructor and avoid setting a token as it will crash the client.

# File lib/reuters/client/token.rb, line 52
def initialize
  @wsdl = Reuters::Wsdls.const_get client_name
  @namespace = Reuters::Namespaces.const_get client_name
end

Public Instance Methods

authenticate() click to toggle source

Authenticates with Reuters and automatically attempts to retieve a new token from the Reuter’s API.

@return [Token] an initialized instance of {Token}

# File lib/reuters/client/token.rb, line 73
def authenticate
  @@response = create_service_token_1(message, {}, false)
end
header() click to toggle source
# File lib/reuters/client/token.rb, line 77
def header
  response = current_response
  Reuters::Builder.new do |body|
    body.authorization.application_id = app_id
    body.authorization.token = response.token
    body.authorization({ 'xmlns' => common.endpoint }, false)
  end
end
message() click to toggle source

Build the authentication message that will be sent to Reuters.

@return [Hash] the contents of the body of the message.

# File lib/reuters/client/token.rb, line 60
def message
  Reuters::Builder.new do |body|
    body.application_id = app_id
    body.username = username
    body.password = password
    body.application_id({ xmlns: common.endpoint }, false)
  end
end

Private Instance Methods

credentials() click to toggle source
# File lib/reuters/client/token.rb, line 88
def credentials
  Reuters::Credentials
end
current_response() click to toggle source
# File lib/reuters/client/token.rb, line 92
def current_response
  if @@response && Time.parse(@@response.expiration) > Time.now
    @@response
  else
    fail 'Token has expired.'
  end
rescue
  authenticate
end