Class: Faraday::XML::Encoder

Inherits:
Object
  • Object
show all
Defined in:
lib/faraday/xml/encoder.rb

Overview

Utility fucntion that encodes input as XML.

Doesn’t try to encode input which already are in string form.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(encoder_options = {}) ⇒ Encoder

Returns a new instance of Encoder.



15
16
17
# File 'lib/faraday/xml/encoder.rb', line 15

def initialize(encoder_options = {})
  @encoder_options = encoder_options || {}
end

Class Method Details

.build!(encoder_options = {}) ⇒ Object



9
10
11
12
13
# File 'lib/faraday/xml/encoder.rb', line 9

def self.build!(encoder_options = {})
  encoder = new(encoder_options)
  encoder.encoder!
  encoder
end

Instance Method Details

#_parameter_as_list_xml(array_of_hashes) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/faraday/xml/encoder.rb', line 82

def _parameter_as_list_xml(array_of_hashes)
  xml_markup = build_xml_markup(skip_instruct: true)
  array_of_hashes.each do |value|
    xml_markup << parameters_as_xml(value) # recursive case
  end
  xml_markup.target!
end

#_parameter_as_xml(value) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/faraday/xml/encoder.rb', line 71

def _parameter_as_xml(value)
  case value
  when Hash
    parameters_as_xml(value) # recursive case
  when Array
    _parameter_as_list_xml(value) # recursive case
  else
    value.to_s.encode(xml: :text) # end case
  end
end

#_parameter_as_xml?(value) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
# File 'lib/faraday/xml/encoder.rb', line 64

def _parameter_as_xml?(value)
  case value
  when Hash, Array then true
  else false
  end
end

#build_xml_markup(**options) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/faraday/xml/encoder.rb', line 90

def build_xml_markup(**options)
  # https://github.com/rails/rails/blob/86fd8d0143b1a0578b359f4b86fea94c718139ae/activesupport/lib/active_support/builder.rb
  # https://github.com/rails/rails/blob/86fd8d0143b1a0578b359f4b86fea94c718139ae/activesupport/lib/active_support/core_ext/hash/conversions.rb
  require 'builder'
  options.merge!(@encoder_options)
  options[:indent] = 2 unless options.key?(:indent)
  xml_markup = ::Builder::XmlMarkup.new(**options)
  if !options.delete(:skip_instruct) # rubocop:disable Style/NegatedIf
    xml_markup.instruct! :xml, version: '1.0', encoding: 'UTF-8'
  end
  xml_markup
end

#encode(data) ⇒ Object



19
20
21
# File 'lib/faraday/xml/encoder.rb', line 19

def encode(data)
  encoder.call(data)
end

#encoderObject Also known as: encoder!



23
24
25
26
27
28
29
30
# File 'lib/faraday/xml/encoder.rb', line 23

def encoder
  @encoder ||= nil
  if @encoder.nil?
    @encoder = set_encoder
    @encoder && test_encoder
  end
  @encoder or raise 'Missing dependencies Builder'
end

#parameters_as_xml(parameter_hash) ⇒ Object

rubocop:disable Metrics/MethodLength



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/faraday/xml/encoder.rb', line 48

def parameters_as_xml(parameter_hash) # rubocop:disable Metrics/MethodLength
  xml_markup = build_xml_markup(skip_instruct: true)
  parameter_hash.each_pair do |key, value|
    key = key.to_s
    if _parameter_as_xml?(value)
      xml_markup.tag!(key) do
        xml = _parameter_as_xml(value)
        xml_markup << xml
      end
    else
      xml_markup.tag!(key, _parameter_as_xml(value))
    end
  end
  xml_markup.target!
end

#set_encoderObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/faraday/xml/encoder.rb', line 37

def set_encoder
  @encoder ||= # rubocop:disable Naming/MemoizedInstanceVariableName
    begin
      require 'builder'
      lambda do |parameter_hash|
        parameters_as_xml(parameter_hash)
      end
    rescue LoadError # rubocop:disable Lint/SuppressedException
    end
end

#test_encoderObject



33
34
35
# File 'lib/faraday/xml/encoder.rb', line 33

def test_encoder
  encode({ success: true })
end