Class: Sem4rSoap::SoapService

Inherits:
Object
  • Object
show all
Defined in:
lib/sem4r_soap/soap_service.rb

Direct Known Subclasses

SoapServiceV13, SoapServiceV2010

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSoapService

Returns a new instance of SoapService.



29
30
31
# File 'lib/sem4r_soap/soap_service.rb', line 29

def initialize
  @soap_namespaces = {}
end

Class Method Details

._soap_call(api_version, method, options = {}) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/sem4r_soap/soap_service.rb', line 100

def self._soap_call(api_version, method, options = {})
  options.assert_valid_keys(:mutate)
  mutate = options.delete :mutate
  if mutate.nil? or mutate
    smutate = "credentials.mutable?"
  else
    smutate = "true"
  end
  ruby_str =<<-EOFS
      define_method :#{method.to_sym} do |*args|
        credentials = args.shift
        if #{smutate}
          soap_body = send("_#{method}", *args)
          helper_call('#{api_version}', credentials, soap_body)
        else
          raise "mutate methods '#{method}' cannot be called on read_only profile"
        end
      end
  EOFS
  eval ruby_str

  ruby_str =<<-EOFS
      define_method :#{(method.to_s + "_raw").to_sym} do |*args|
        credentials = args.shift
        soap_message = args.shift
        if #{smutate}
          helper_call_raw('#{api_version}', credentials, soap_message)
        else
          raise "mutate methods '#{method}' cannot be called on read_only profile"
        end
      end
  EOFS
  eval ruby_str
end

Instance Method Details

#build_soap_message(soap_header, soap_body) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/sem4r_soap/soap_service.rb', line 43

def build_soap_message(soap_header, soap_body)
  soap_message = '<?xml version="1.0" encoding="utf-8" ?>'
  soap_message +=<<-EOFS
  <env:Envelope
     xmlns:xsd="http://www.w3.org/2001/XMLSchema"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
  EOFS

  @soap_namespaces.each do |name, value|
    soap_message += " #{name}=\"#{value}\""
  end
  soap_message += ">"

  soap_message += soap_header
  soap_message += "<env:Body>"
  soap_message += soap_body
  soap_message += "</env:Body>"

  soap_message += "</env:Envelope>"
  soap_message
end

#helper_call(api_version, credentials, soap_body) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/sem4r_soap/soap_service.rb', line 66

def helper_call(api_version, credentials, soap_body)
  soap_action = ""

  case api_version
    when "v13"
      match_data = soap_body.match(/<(\w+)/m)
      if match_data
        soap_action = match_data[1]
      else
        raise "Soapaction not found in #{soap_body}"
      end
    when "v2010"
    else
      raise "unknown api version #{api_version}"
  end

  soap_xml = build_soap_message(build_soap_header(credentials), soap_body)
  if credentials.sandbox?
    _send(@sandbox_service_url, soap_action, soap_xml)
  else
    _send(@production_service_url, soap_action, soap_xml)
  end
end

#helper_call_raw(credentials, xml_message) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/sem4r_soap/soap_service.rb', line 90

def helper_call_raw(credentials, xml_message)
  soap_message = SoapMessageV2010.new(@connector, credentials)
  soap_message.init( @header_namespace, @service_namespace )
  if credentials.sandbox?
    soap_message.send_raw(@sandbox_service_url, xml_message)
  else
    soap_message.send_raw(@production_service_url, xml_message)
  end
end

#init(header_namespace, service_namespace) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/sem4r_soap/soap_service.rb', line 33

def init(header_namespace, service_namespace)
  @soap_namespaces = {}

  @header_namespace = header_namespace
  @soap_namespaces['xmlns'] = header_namespace if header_namespace

  @service_namespace = service_namespace
  @soap_namespaces['xmlns:s'] = service_namespace if service_namespace
end