class WirecardSepa::Utils::Template

Attributes

request[R]

Public Class Methods

new(request) click to toggle source
# File lib/wirecard_sepa/utils/template.rb, line 8
def initialize(request)
  @request = request
end

Public Instance Methods

to_xml() click to toggle source
# File lib/wirecard_sepa/utils/template.rb, line 12
def to_xml
  xml_template = File.open template_path, "r:UTF-8", &:read
  xml_template.gsub(/{{\w+}}/, request_params)#.tap { byebug }
end

Private Instance Methods

custom_fields_xml() click to toggle source
# File lib/wirecard_sepa/utils/template.rb, line 40
def custom_fields_xml
  # TODO: Refactor me :>
  custom_fields = request.params[:custom_fields] || Hash.new
  return '' if custom_fields.empty?
  fields_xml = custom_fields.map do |k,v|
    "  <custom-field field-name=\"#{k}\" field-value=\"#{v}\"/>\n"
  end.join.to_s
  '<custom-fields>' "\n" +
  "  #{fields_xml}" +
  '  </custom-fields>'
end
params_without_custom_fields() click to toggle source
# File lib/wirecard_sepa/utils/template.rb, line 25
def params_without_custom_fields
  request.params.reject { |k,_| k == :custom_fields }
end
request_params() click to toggle source
# File lib/wirecard_sepa/utils/template.rb, line 19
def request_params
  params_without_custom_fields.each_with_object({}) do |(k,v), h|
    h["{{#{k.upcase}}}"] = REXML::Text.new(v.to_s)
  end.merge('{{CUSTOM_FIELDS}}' => custom_fields_xml)
end
template_file() click to toggle source
# File lib/wirecard_sepa/utils/template.rb, line 33
def template_file
  request.class.name.
    gsub(/(.)([A-Z])/, '\1_\2').
    gsub('::_', '/').
    downcase + '.xml'
end
template_path() click to toggle source
# File lib/wirecard_sepa/utils/template.rb, line 29
def template_path
  File.expand_path "../../../templates/#{template_file}", __FILE__
end