Class: GaroonCat::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/garoon-cat/request.rb

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Request

Returns a new instance of Request.



6
7
8
# File 'lib/garoon-cat/request.rb', line 6

def initialize(params)
  @params = params
end

Instance Method Details

#bodyObject



77
78
79
80
81
# File 'lib/garoon-cat/request.rb', line 77

def body
  body = REXML::Element.new('soap:Body')
  body.add_element(body_action)
  body
end

#body_actionObject



71
72
73
74
75
# File 'lib/garoon-cat/request.rb', line 71

def body_action
  action = REXML::Element.new(@params.dig(:header, :action).to_s)
  action.add_element(body_action_parameters)
  action
end

#body_action_parametersObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/garoon-cat/request.rb', line 52

def body_action_parameters
  parameters = REXML::Element.new('parameters')
  target = @params.dig(:body, :parameters)
  case target
  when Hash
    target.each do |key, v1|
      case v1
      when String
        parameters.add_element(key.to_s).add_text(v1.to_s)
      when Array
        v1.each do |v2|
          parameters.add_element(key.to_s).add_text(v2.to_s)
        end
      end
    end
  end
  parameters
end

#docObject



91
92
93
94
95
96
# File 'lib/garoon-cat/request.rb', line 91

def doc
  doc = REXML::Document.new
  doc << REXML::XMLDecl.new('1.0', 'UTF-8')
  doc.add_element(envelope)
  doc
end

#envelopeObject



83
84
85
86
87
88
89
# File 'lib/garoon-cat/request.rb', line 83

def envelope
  envelope = REXML::Element.new('soap:Envelope')
  envelope.add_namespace('soap', 'http://www.w3.org/2003/05/soap-envelope')
  envelope.add_element(header)
  envelope.add_element(body)
  envelope
end

#headerObject



42
43
44
45
46
47
48
49
# File 'lib/garoon-cat/request.rb', line 42

def header
  header = REXML::Element.new('soap:Header')
  header.add_element(header_action)
  header.add_element(header_security)
  header.add_element(header_timestamp)
  header.add_element(header_locale)
  header
end

#header_actionObject



10
11
12
13
14
# File 'lib/garoon-cat/request.rb', line 10

def header_action
  action = REXML::Element.new('Action')
  action.add_text(@params.dig(:header, :action).to_s)
  action
end

#header_localeObject



36
37
38
39
40
# File 'lib/garoon-cat/request.rb', line 36

def header_locale
  locale = REXML::Element.new('locale')
  locale.add_text(@params.dig(:header, :locale))
  locale
end

#header_securityObject



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/garoon-cat/request.rb', line 16

def header_security
  security = REXML::Element.new('Security')
  username = @params.dig(:header, :security, :username_token, :username)
  password = @params.dig(:header, :security, :username_token, :password)
  if username && password
    username_token = REXML::Element.new('UsernameToken')
    username_token.add_element('Username').add_text(username)
    username_token.add_element('Password').add_text(password)
    security.add_element(username_token)
  end
  security
end

#header_timestampObject



29
30
31
32
33
34
# File 'lib/garoon-cat/request.rb', line 29

def header_timestamp
  timestamp = REXML::Element.new('Timestamp')
  timestamp.add_element('Created').add_text(@params.dig(:header, :timestamp, :created).iso8601)
  timestamp.add_element('Expires').add_text(@params.dig(:header, :timestamp, :expires).iso8601)
  timestamp
end

#to_sObject



98
99
100
# File 'lib/garoon-cat/request.rb', line 98

def to_s
  doc.to_s
end