Class: SoapClient

Inherits:
Object
  • Object
show all
Defined in:
lib/clearconnect/soap_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(username, password, session) ⇒ SoapClient

Returns a new instance of SoapClient.



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/clearconnect/soap_client.rb', line 2

def initialize(username, password, session)
  wsdl = HTTParty.get(ClearConnect.configuration.endpoints[:wsdl], :format => 'xml').parsed_response
  @client = Savon::Client.new(
    wsdl: wsdl, 
    log_level: :info
  )
  @username = username
  @password = password
  @session = session
  @default_params = {
    :username => username,
    :password => password,
    :resultType => ClearConnect.configuration.format.to_s
  }
end

Instance Method Details

#generic_request(message) ⇒ Object

Encodes request, packages into required node, sends, decodes response



19
20
21
22
23
24
25
# File 'lib/clearconnect/soap_client.rb', line 19

def generic_request(message)
  request = "<requestXmlString>#{xml_encode(message)}</requestXmlString>"
  response = @client.call(:tss_request, message: request)
  # :tss_request_response => {:tss_request_return => [ugly value to be decoded]}
  # response.body
  JSON.parse(response.body[:tss_request_response][:tss_request_return])[0]
end

#post_request(query) ⇒ Object

Takes a query of format { :key => ‘value’ } where :key is the properly formatted parameter and ‘value’ is the string value Parameters can be nested like so: { :tempRecords => { :tempRecord => [ { :key1 = ‘value1’, :key2 => ‘value2’ }, { :key1 => ‘value3’ } ] } } will produce <tempRecords><tempRecord><key1>value1</key1><key2>value2</key2></tempRecord><tempRecord><key1>value3</key1></tempRecord></tempRecords>



30
31
32
33
34
# File 'lib/clearconnect/soap_client.rb', line 30

def post_request(query)
  params = @default_params.merge(query)
  post_data = XmlSimple.xml_out(params, RootName: 'clearviewRequest', NoAttr: true)
  generic_request(post_data)
end

#xml_decode(xml_string) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/clearconnect/soap_client.rb', line 54

def xml_decode(xml_string)
  string = xml_string
  characters = [
    { replacement: '&', find: '&amp;' },
    { replacement: '"', find: '&quot;' },
    { replacement: '<', find: '&lt;' },
    { replacement: '>', find: '&gt;' },
    { replacement: "'", find: '&apos;' }
  ]
  characters.each do |set|
    new_string = string.gsub(set[:find], set[:replacement])
    if not new_string.nil?
      string = new_string
    end
  end
  return string 
end

#xml_encode(xml_string) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/clearconnect/soap_client.rb', line 36

def xml_encode(xml_string)
  string = xml_string
  characters = [
    { find: '&', replacement: '&amp;' },
    { find: '"', replacement: '&quot;' },
    { find: '<', replacement: '&lt;' },
    { find: '>', replacement: '&gt;' },
    { find: "'", replacement: '&apos;' }
  ]
  characters.each do |set|
    new_string = string.gsub(set[:find], set[:replacement])
    if not new_string.nil?
      string = new_string
    end
  end
  return string 
end