Module: Stamps::Request

Included in:
API
Defined in:
lib/stamps/request.rb

Overview

Defines HTTP request methods

Instance Method Summary collapse

Instance Method Details

#authenticator_tokenObject

Get the Authenticator token. By using an Authenticator, the integration can be sure that the conversation between the integration and the SWS server is kept in sync and no messages have been lost.



33
34
35
# File 'lib/stamps/request.rb', line 33

def authenticator_token
  @authenticator ||= self.get_authenticator_token
end

#formatted_soap_action(web_method) ⇒ Object

Concatenates namespace and web method in a way the API can understand



56
57
58
# File 'lib/stamps/request.rb', line 56

def formatted_soap_action(web_method)
  [self.namespace, web_method.to_s].compact.join('/')
end

#get_authenticator_tokenObject

Make Authentication request for the user



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/stamps/request.rb', line 39

def get_authenticator_token
  response_hash = self.request('AuthenticateUser',
    Stamps::Mapping::AuthenticateUser.new(
      :credentials => {
        :integration_id => self.integration_id,
        :username       => self.username,
        :password       => self.password
    })
  )
  if response_hash[:authenticate_user_response] != nil
    response_hash[:authenticate_user_response][:authenticator]
  else
    raise Stamps::InvalidIntegrationID.new(response_hash[:errors][0])
  end
end

#request(web_method, params, raw = false) ⇒ Object

Perform an HTTP request



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/stamps/request.rb', line 11

def request(web_method, params, raw=false)
  client = Savon.client do |globals|
    globals.endpoint self.endpoint
    globals.namespace self.namespace
    globals.namespaces("xmlns:tns" => self.namespace)
    globals.log false
    globals.logger Logger.new(STDOUT)
    globals.raise_errors false
    globals.headers({ "SoapAction" => formatted_soap_action(web_method) })
    globals.element_form_default :qualified
    globals.namespace_identifier :tns
  end

  response = client.call(web_method, :message => params.to_hash)

  Stamps::Response.new(response).to_hash
end