Class: Reuters::Client::Base

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

Overview

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.

Direct Known Subclasses

Fundamentals, Search::Base, Token

Instance Method Summary collapse

Constructor Details

#initializeBase

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



17
18
19
20
21
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(op, *args) ⇒ Object

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



37
38
39
40
41
42
43
# 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

Instance Method Details

#after_request {|req| ... } ⇒ Object

Note:

By default this request hook has no effect.

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.

Yields:

  • (req)

    The raw response object that has been received and parsed by Savon.



104
105
106
107
# 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 {|req, type| ... } ⇒ Object

Note:

By default this request hook has no effect.

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.

Yields:

  • (req, type)

    The request object that has been sent to the client. The operation name is passed as the second parameter.

Yield Parameters:

  • req (Reuters::Builder)

    The request object.

  • type (Symbol)

    The intended operation to perform.



90
91
92
93
# File 'lib/reuters/client/base.rb', line 90

def before_request(&block)
  @before_request = block if block
  @before_request || proc { |a| a }
end

#clientObject

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

Returns:

  • (Object)

    client to make requests through.

See Also:



30
31
32
# File 'lib/reuters/client/base.rb', line 30

def client
  Savon.client options
end

#request(type, message, attribs = {}, auth = true) ⇒ Object

Note:

This request method calls the Savon Client #call method.

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.

Parameters:

  • type (Symbol)

    of request to send to Reuters API.

  • message (Reuters::Builder)

    contents to send to the API.

  • attribs (Hash) (defaults to: {})

    to attach to the request object.

  • auth (Boolean) (defaults to: true)

    defaults to true if a token is required for this request

Returns:

  • (Object)

    The corresponding Response object.

See Also:



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 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