Class: Adyen::API::XMLQuerier

Inherits:
Object
  • Object
show all
Defined in:
lib/adyen/api/xml_querier.rb

Overview

A simple wrapper around the raw response body returned by Adyen. It abstracts away the differences between REXML and Nokogiri, ensuring that this library will always work.

At load time, it will first check if Nokogiri is available, otherwise REXML is used. This means that if you want to use Nokogiri and have it installed as a gem, you will have to make sure rubygems is loaded and the gem has been activated. Or assign the backend to use.

Defined Under Namespace

Classes: NokogiriBackend, REXMLBackend

Constant Summary collapse

NS =

The namespaces used by Adyen.

{
  'soap'      => 'http://schemas.xmlsoap.org/soap/envelope/',
  'payment'   => 'http://payment.services.adyen.com',
  'recurring' => 'http://recurring.services.adyen.com',
  'common'    => 'http://common.services.adyen.com'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, backend) ⇒ XMLQuerier

Returns a new instance of XMLQuerier.

Parameters:

  • data (Nokogiri::XML::NodeSet)

    The XML data to wrap.



96
97
98
# File 'lib/adyen/api/xml_querier.rb', line 96

def initialize(node, backend)
  @node, @backend = node, backend
end

Instance Attribute Details

#backendObject (readonly)

Returns the value of attribute backend.



93
94
95
# File 'lib/adyen/api/xml_querier.rb', line 93

def backend
  @backend
end

Class Method Details

.default_backendObject

Returns A backend to handle XML parsing.

Returns:

  • A backend to handle XML parsing.



63
64
65
66
67
68
69
# File 'lib/adyen/api/xml_querier.rb', line 63

def self.default_backend
  @default_backend ||= begin
    NokogiriBackend.new
  rescue LoadError
    REXMLBackend.new
  end
end

.html(data, backend = nil) ⇒ Object

Creates an XML querier for an HTML document



78
79
80
81
# File 'lib/adyen/api/xml_querier.rb', line 78

def self.html(data, backend = nil)
  backend ||= default_backend
  self.new(backend.document_for_html(string_from(data)), backend)
end

.string_from(data) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/adyen/api/xml_querier.rb', line 83

def self.string_from(data)
  if data.is_a?(String)
    data
  elsif data.responds_to?(:body)
    data.body.to_s
  else 
    data.to_s
  end
end

.xml(data, backend = nil) ⇒ Object

Creates an XML querier for an XML document



72
73
74
75
# File 'lib/adyen/api/xml_querier.rb', line 72

def self.xml(data, backend = nil)
  backend ||= default_backend
  self.new(backend.document_for_xml(string_from(data)), backend)
end

Instance Method Details

#childrenArray, Nokogiri::XML::NodeSet

Returns The children of this node.

Returns:

  • (Array, Nokogiri::XML::NodeSet)

    The children of this node.



116
117
118
# File 'lib/adyen/api/xml_querier.rb', line 116

def children
  @node.first.children
end

#empty?Boolean

Returns whether or not this node is empty.

Returns:

  • (Boolean)

    Returns whether or not this node is empty.



121
122
123
# File 'lib/adyen/api/xml_querier.rb', line 121

def empty?
  @node.empty?
end

#map {|XMLQuerier| ... } ⇒ Array

Returns The list of nodes wrapped in XMLQuerier instances.

Yields:

  • (XMLQuerier)

    A member of this node set, ready to be queried.

Returns:

  • (Array)

    The list of nodes wrapped in XMLQuerier instances.



132
133
134
# File 'lib/adyen/api/xml_querier.rb', line 132

def map(&block)
  @node.map { |n| self.class.new(n, backend) }.map(&block)
end

#text(query) ⇒ String

Returns The contents of the text node indicated by the given query.

Parameters:

  • query (String)

    The xpath query to perform.

Returns:

  • (String)

    The contents of the text node indicated by the given query.



111
112
113
# File 'lib/adyen/api/xml_querier.rb', line 111

def text(query)
  xpath("#{query}/text()").to_s.strip
end

#to_sString

Returns A string representation of this node.

Returns:

  • (String)

    A string representation of this node.



126
127
128
# File 'lib/adyen/api/xml_querier.rb', line 126

def to_s
  backend.stringify_nodeset(@node)
end

#xpath(query) {|XMLQuerier| ... } ⇒ XMLQuerier

Returns A new XMLQuerier scoped to the given query. Or, if a block is given, the result of calling the block.

Parameters:

  • query (String)

    The xpath query to perform.

Yields:

  • (XMLQuerier)

    A new XMLQuerier scoped to the given query.

Returns:

  • (XMLQuerier)

    A new XMLQuerier scoped to the given query. Or, if a block is given, the result of calling the block.



104
105
106
107
# File 'lib/adyen/api/xml_querier.rb', line 104

def xpath(query)
  result = self.class.new(backend.perform_xpath(query, @node), backend)
  block_given? ? yield(result) : result
end