Class: Reuters::Client::Base
- Inherits:
-
Object
- Object
- Reuters::Client::Base
- 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
Instance Method Summary collapse
-
#after_request {|req| ... } ⇒ Object
Yields a block that is called after a successful request to Reuters.
-
#before_request {|req, type| ... } ⇒ Object
Yields a block that is called before a request is sent to Savon.
-
#client ⇒ Object
Retrieves a new instance of a Savon client that has been correctly configured for sending requests to the Reuter’s API.
-
#initialize ⇒ Base
constructor
Initialize the base client class and set a token that can be retrieved upon request.
-
#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.
-
#request(type, message, attribs = {}, auth = true) ⇒ Object
Send a correctly formatted request to the Reuter’s API.
Constructor Details
#initialize ⇒ Base
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
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.
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
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.
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 |
#client ⇒ Object
Retrieves a new instance of a Savon client that has been correctly configured for sending requests to the Reuter’s API.
30 31 32 |
# File 'lib/reuters/client/base.rb', line 30 def client Savon.client end |
#request(type, message, attribs = {}, auth = true) ⇒ Object
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.
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, , attribs = {}, auth = true) content = { attributes: attribs.merge('xmlns' => @namespace.endpoint), message: before_request.call(, type) } content[:soap_header] = header if auth data = client.call(type, content).body Reuters::Response.new after_request.call(data[data.keys.first]) end |