Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/hosemonkey/ext/hash.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_xml_string(xml) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/hosemonkey/ext/hash.rb', line 41

def from_xml_string(xml)
  begin
    result = Nokogiri::XML(xml) do |config|
      config.strict.noent
    end
    return { result.root.name => xml_node_to_hash(result.root)}
  rescue Exception => e
    raise "Could not parse xml: #{e}"
  end
end

.xml_node_to_hash(node) ⇒ Object



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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/hosemonkey/ext/hash.rb', line 53

def 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 != {}
      attributes = {}
      node.attributes.keys.each do |key|
        attributes[node.attributes[key].name] = node.attributes[key].value
      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
            return result unless attributes
            result_hash[child.name] = result
          end
        elsif result_hash[child.name]

          if result_hash[child.name].is_a?(Object::Array)
             result_hash[child.name] << result
          else
             result_hash[child.name] = [result_hash[child.name]] << result
          end
        else
          result_hash[child.name] = result
        end
      end
      if attributes
         #add code to remove non-data attributes e.g. xml schema, namespace here
         #if there is a collision then node content supersets attributes
         result_hash = attributes.merge(result_hash)
      end
      return result_hash
    else
      return attributes
    end
  else
    return node.content.to_s
  end
end

Instance Method Details

#include_only(*args) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/hosemonkey/ext/hash.rb', line 31

def include_only(*args)
  self.each do |key,value|
    unless args.include?(key)
      raise "Unknown Argument"
    end
  end
end

#include_only?(*args) ⇒ Boolean

Returns:



22
23
24
25
26
27
28
29
# File 'lib/hosemonkey/ext/hash.rb', line 22

def include_only?(*args)
  self.each do |key,value|
    unless args.include?(key)
      return false
    end
  end
  return true
end

#stringify_keysObject



2
3
4
5
6
7
# File 'lib/hosemonkey/ext/hash.rb', line 2

def stringify_keys
  keys.each do |key|
    self[(key.to_s rescue key) || key] = delete(key)
  end
  self
end

#symbolize_keysObject

From rails active_support. Return a new hash with all keys converted to symbols



18
19
20
# File 'lib/hosemonkey/ext/hash.rb', line 18

def symbolize_keys
  dup.symbolize_keys!
end

#symbolize_keys!Object

From rails active_support. Destructively convert all keys to symbols as long as they respond to to_sym



10
11
12
13
14
15
# File 'lib/hosemonkey/ext/hash.rb', line 10

def symbolize_keys!
  keys.each do |key|
    self[(key.to_sym rescue key) || key] = delete(key)
  end
  self
end