Class: Soap::Webservice::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/soap/webservice/request.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Request

Returns a new instance of Request.



15
16
17
18
19
# File 'lib/soap/webservice/request.rb', line 15

def initialize(attributes = {})
  @attributes = attributes
  validate_header_attrs!
  validate_body_attrs!
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



13
14
15
# File 'lib/soap/webservice/request.rb', line 13

def attributes
  @attributes
end

Class Method Details

.body_attributesObject



10
# File 'lib/soap/webservice/request.rb', line 10

def body_attributes; {} end

.header_attributesObject



8
# File 'lib/soap/webservice/request.rb', line 8

def header_attributes; {} end

Instance Method Details

#body_paramsObject



43
44
45
46
47
48
49
50
# File 'lib/soap/webservice/request.rb', line 43

def body_params
  return {} if self.class.body_attributes.nil?
  self.class.body_attributes.each.with_object({}) do |attr, attrs|
    name = Soap.to_snakecase(attr[:name]).to_sym
    value = attributes[name]
    attrs[name] = value if value
  end
end

#header_paramsObject



52
53
54
55
56
57
58
59
# File 'lib/soap/webservice/request.rb', line 52

def header_params
  return {} if self.class.header_attributes.nil?
  self.class.header_attributes.each.with_object({}) do |attr, attrs|
    name = Soap.to_snakecase(attr[:name]).to_sym
    value = attributes[name]
    attrs[name] = value if value
  end
end

#to_messageObject



61
62
63
64
65
66
# File 'lib/soap/webservice/request.rb', line 61

def to_message
  {
    soap_header: header_params,
    message: body_params
  }
end

#to_xmlObject



68
69
70
71
72
73
# File 'lib/soap/webservice/request.rb', line 68

def to_xml
  Gyoku.xml(
    to_message,
    key_converter: lambda { |key| Soap.to_camelcase(key) }
  )
end

#validate_body_attrs!Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/soap/webservice/request.rb', line 32

def validate_body_attrs!
  return if required_body.empty?
  if !required_body.empty? && attributes.empty?
    raise "Required attributes are missing: #{required_body}"
  end

  if !(required_body - attributes.keys).empty?
    raise "Attribute #{required_body - attributes.keys} must be present"
  end
end

#validate_header_attrs!Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/soap/webservice/request.rb', line 21

def validate_header_attrs!
  return if required_header.empty?
  if !required_header.empty? && attributes.empty?
    raise "Required attributes are missing: #{required_header}"
  end

  if !(required_header - attributes.keys).empty?
    raise "Attribute #{required_header - attributes.keys} must be present"
  end
end