Module: Prestashop::Api::Converter
- Defined in:
- lib/prestashop/api/converter.rb
Class Method Summary collapse
-
.build(resource, model, hash) ⇒ Object
Build XML from given params and hash.
-
.build_nodes(ml, source, mkey = nil) ⇒ Object
Build XML nodes based from given source parameter, skipping
attrorvalkeys if source is Hash. -
.cdata(ml, value) ⇒ Object
Create CDATA tag into XML, when is not empty.
-
.parse(xml) ⇒ Object
Parse XML (response from WebService call) to Hash, skipping first <prestashop> node.
-
.parse_error(response) ⇒ Object
Parse XML to Hash, call parse and skip everything to error message.
- .prepare(data) ⇒ Object
-
.xml_node_to_hash(node) ⇒ Object
Parse XML node and covert it to hash.
Class Method Details
.build(resource, model, hash) ⇒ Object
Build XML from given params and hash. This XML is compatible with Prestashop WebService
Converter.build :customers, :customer, { name: 'Steve' } # => <prestashop><customers><customer>....</customer></customers></prestashop>
11 12 13 14 15 16 17 18 19 |
# File 'lib/prestashop/api/converter.rb', line 11 def self.build resource, model, hash Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |ml| ml.prestashop("xmlns:xlink" => "http://www.w3.org/1999/xlink") { ml.send(model.to_s) { build_nodes ml, hash } } end.to_xml(save_with: Nokogiri::XML::Node::SaveOptions::AS_XML | Nokogiri::XML::Node::SaveOptions::NO_DECLARATION).strip end |
.build_nodes(ml, source, mkey = nil) ⇒ Object
Build XML nodes based from given source parameter, skipping attr or val keys if source is Hash
Parameters:
-
ml- Current XML -
source- Source, which will be converted into the XML
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/prestashop/api/converter.rb', line 51 def self.build_nodes ml, source, mkey = nil unless source[:attr] and source[:val] source.each do |key, value| if value.kind_of? Hash if value[:attr] ml.send(key, value[:attr]){ cdata ml, value[:val] build_nodes ml, value } elsif key != :attr ml.send(key){ unless value.kind_of? Hash cdata ml, value end build_nodes ml, value } end elsif value.kind_of? Array value.each do |item| hash = {} hash[key] = item build_nodes ml, hash end elsif key != :val ml.send(key){ cdata ml, value } end end if source.kind_of? Hash end end |
.cdata(ml, value) ⇒ Object
Create CDATA tag into XML, when is not empty
85 86 87 88 89 |
# File 'lib/prestashop/api/converter.rb', line 85 def self.cdata ml, value if value and value != '' ml.cdata value end end |
.parse(xml) ⇒ Object
Parse XML (response from WebService call) to Hash, skipping first <prestashop> node.
Converter.parse('<prestashop><customers><customer><name>Steve</customer></customers></prestashop>')
# => {customers: [ customer: { name: 'Steve' } ]}
26 27 28 29 30 31 32 |
# File 'lib/prestashop/api/converter.rb', line 26 def self.parse xml result = Nokogiri::XML xml raise ParserError unless result.root xml_node_to_hash result.root end |
.parse_error(response) ⇒ Object
Parse XML to Hash, call parse and skip everything to error message
36 37 38 39 40 41 42 43 |
# File 'lib/prestashop/api/converter.rb', line 36 def self.parse_error response parsed = parse(response) if parsed[:errors] and parsed[:errors][:error] and parsed[:errors][:error][:message] parsed[:errors][:error][:message] else raise ParserError end end |
.prepare(data) ⇒ Object
147 148 149 |
# File 'lib/prestashop/api/converter.rb', line 147 def self.prepare data (data.class == String && data.to_i.to_s == data) ? data.to_i : data end |
.xml_node_to_hash(node) ⇒ Object
Parse XML node and covert it to hash
Converter.xml_node_to_hash '<customer id_lang="1">Steve</customer>'
# => { attr: { id_lang: 1 }, val: 'Steve' }
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/prestashop/api/converter.rb', line 96 def self.xml_node_to_hash node # If we are at the root of the document, start the hash if node.element? result_hash = {} if node.attributes != {} if node.attributes node.attributes.each do |key, value| unless value.name == 'href' result_hash[:attr] = {} unless result_hash[:attr] result_hash[:attr][value.name.to_sym] = prepare(value.value) end end end end if node.children.size > 0 node.children.each do |child| result = xml_node_to_hash(child) if child.name == "text" unless child.next_sibling || child.previous_sibling if !result_hash.empty? result_hash[:val] = prepare(result) return result_hash end return prepare(result) end elsif child.name == "#cdata-section" if !result_hash.empty? result_hash[:val] = prepare(result) return result_hash end return prepare(result) elsif result_hash[child.name.to_sym] if result_hash[child.name.to_sym].is_a?(Object::Array) result_hash[child.name.to_sym] << prepare(result) else result_hash[child.name.to_sym] = [result_hash[child.name.to_sym]] << prepare(result) end else result_hash[child.name.to_sym] = prepare(result) end end return result_hash else return result_hash end else return prepare(node.content.to_s) end end |