Module: CdekClient::Util
Instance Method Summary collapse
- #array_wrap(value) ⇒ Object
- #blank?(value) ⇒ Boolean
- #deep_symbolize_keys(data) ⇒ Object
- #hash_to_xml(hash) ⇒ Object
- #hash_value_at_keypath(hash, keypath) ⇒ Object
- #xml_to_hash(xml_io) ⇒ Object
Instance Method Details
#array_wrap(value) ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/cdek_client/util.rb', line 49 def array_wrap(value) if value.is_a? Array return value elsif value.nil? return [] else return [value] end end |
#blank?(value) ⇒ Boolean
59 60 61 62 63 |
# File 'lib/cdek_client/util.rb', line 59 def blank?(value) value.nil? || (value.is_a?(String) && value.strip == '') || ((value.is_a?(Array) || (value.is_a?(Hash))) && value.empty?) end |
#deep_symbolize_keys(data) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/cdek_client/util.rb', line 20 def deep_symbolize_keys(data) case data when Hash result = {} data.each do |key, value| result[key.to_sym] = deep_symbolize_keys value end return result when Array result = [] data.each do |value| result.push deep_symbolize_keys(value) end return result else return data end end |
#hash_to_xml(hash) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/cdek_client/util.rb', line 13 def hash_to_xml(hash) root_key = hash.keys.first Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml| process_hash_to_xml(root_key, hash[root_key], xml) end.to_xml end |
#hash_value_at_keypath(hash, keypath) ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/cdek_client/util.rb', line 39 def hash_value_at_keypath(hash, keypath) if keypath.length == 0 || !hash.is_a?(Hash) return nil elsif keypath.length == 1 return hash[keypath[0]] else return hash_value_at_keypath hash[keypath[0]], keypath[1..-1] end end |
#xml_to_hash(xml_io) ⇒ Object
8 9 10 11 |
# File 'lib/cdek_client/util.rb', line 8 def xml_to_hash(xml_io) result = Nokogiri::XML xml_io return { result.root.name.to_sym => xml_node_to_hash(result.root)} end |