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.



35
36
37
# File 'lib/stamps/request.rb', line 35

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



58
59
60
# File 'lib/stamps/request.rb', line 58

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

#get_authenticator_tokenObject

Make Authentication request for the user



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

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
28
29
# 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
    globals.open_timeout self.open_timeout
    globals.read_timeout self.read_timeout
  end

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

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