class Reuters::Client::Base

The base class for the client is not meant to be directly initialized but instead contains common functionality shared by most classes inside the Client.

This class also handles authenticating the user with the Reuter’s API and handling the resulting token provided.

Public Class Methods

new() click to toggle source

Initialize the base client class and set a token that can be retrieved upon request.

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

Public Instance Methods

after_request(&block) click to toggle source

Yields a block that is called after a successful request to Reuters. The object that is returned from this is request is subsequently passed onto this Clients corresponding response class.

@note By default this request hook has no effect.

@yield [req] The raw response object that

has been received and parsed by Savon.
# File lib/reuters/client/base.rb, line 104
def after_request(&block)
  @after_request = block if block
  @after_request || proc { |a| a }
end
before_request(&block) click to toggle source

Yields a block that is called before a request is sent to Savon. The hash that is returned from this block is used as the message part of the Savon request.

@note By default this request hook has no effect.

@yield [req, type] The request object that has been

sent to the client. The operation name is passed
as the second parameter.

@yieldparam req [Reuters::Builder] The request object. @yieldparam type [Symbol] The intended operation to perform.

# File lib/reuters/client/base.rb, line 90
def before_request(&block)
  @before_request = block if block
  @before_request || proc { |a| a }
end
client() click to toggle source

Retrieves a new instance of a Savon client that has been correctly configured for sending requests to the Reuter’s API.

@see savonrb.com/version2/client.html

@return [Object] client to make requests through.

# File lib/reuters/client/base.rb, line 30
def client
  Savon.client options
end
method_missing(op, *args) click to toggle source

Attempts to call an operation for this client through the determined set of operations that have been retrieved from the WSDL.

# File lib/reuters/client/base.rb, line 37
def method_missing(op, *args)
  if client.operations.include?(op)
    request op, *args
  else
    fail NoMethodError, op
  end
end
request(type, message, attribs = {}, auth = true) click to toggle source

Send a correctly formatted request to the Reuter’s API. This method makes an authenticated request to the API, assuming that a token object has been defined by the extending Class.

@note This request method calls the Savon Client call

method.

@see savonrb.com/version2/requests.html

@param [Symbol] type of request to send to Reuters API. @param [Reuters::Builder] message contents to send to the API. @param [Hash] attribs to attach to the request object. @param [Boolean] auth defaults to true if a token is required

for this request

@return [Object] The corresponding Response object.

# File lib/reuters/client/base.rb, line 62
def request(type, message, attribs = {}, auth = true)

  content = {
    attributes: attribs.merge('xmlns' => @namespace.endpoint),
    message: before_request.call(message, type)
  }

  content[:soap_header] = header if auth

  data = client.call(type, content).body

  Reuters::Response.new after_request.call(data[data.keys.first])

end

Private Instance Methods

client_name() click to toggle source
# File lib/reuters/client/base.rb, line 111
def client_name
  self.class.name.demodulize
end
common() click to toggle source
# File lib/reuters/client/base.rb, line 115
def common
  Reuters::Namespaces::Common
end
options() click to toggle source
# File lib/reuters/client/base.rb, line 119
def options
  {
    wsdl: @wsdl.endpoint,
    ssl_version: :SSLv3,
    namespace_identifier: nil,
    ssl_verify_mode: :none,
    log: Reuters.debug,
    pretty_print_xml: true
  }
end