Class: Reuters::Client::Token

Inherits:
Base
  • Object
show all
Defined in:
lib/reuters/client/token.rb

Overview

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.

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

Examples:

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"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#after_request, #before_request, #client, #method_missing, #request

Constructor Details

#initializeToken

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



52
53
54
55
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Reuters::Client::Base

Instance Attribute Details

#app_idString, Nil (readonly)

The application id that will be used to authenticate with the Reuter’s api.

Returns:

  • (String, Nil)

    the application id, or nil if one has not been configured.



46
# File 'lib/reuters/client/token.rb', line 46

delegate :token, :expiration, to: :current_response

#expirationInteger, Nil (readonly)

The timestamp at which the associated token will expire.

Returns:

  • (Integer, Nil)

    the expiry time of the token, or nil if not set.



# File 'lib/reuters/client/token.rb', line 23

#passwordString, Nil (readonly)

The password that will be used to authenticate with the Reuter’s api.

Returns:

  • (String, Nil)

    the password, or nil if one has not been configured.



# File 'lib/reuters/client/token.rb', line 34

#tokenString, Nil (readonly)

The token that has been retrieved from the Reuter’s API.

Returns:

  • (String, Nil)

    the token contents, or nil if not set.



# File 'lib/reuters/client/token.rb', line 19

#usernameString, Nil (readonly)

The username that will be used to authenticate with the Reuter’s api.

Returns:

  • (String, Nil)

    the username, or nil if one has not been configured.



# File 'lib/reuters/client/token.rb', line 28

Instance Method Details

#authenticateToken

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

Returns:



73
74
75
# File 'lib/reuters/client/token.rb', line 73

def authenticate
  @@response = create_service_token_1(message, {}, false)
end

#headerObject



77
78
79
80
81
82
83
84
# 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

#messageHash

Build the authentication message that will be sent to Reuters.

Returns:

  • (Hash)

    the contents of the body of the message.



60
61
62
63
64
65
66
67
# 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