Module: RTurk::XMLUtilities

Included in:
Hit, Parser
Defined in:
lib/rturk/xml_utilities.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) map_content(xml_obj, hash)



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rturk/xml_utilities.rb', line 16

def map_content(xml_obj, hash)
  hash.each_pair do |k,v|
    val = xml_obj.xpath(v).inner_text.strip
    if val.match(/^[0-9]+$/)
      val = val.to_i
    elsif val.match(/^[0-9\.]+$/)
      val = val.to_f
    elsif val.match(/^[0-9]{4}-[0-9]{2}-[0-9]{2}T.+/)
      val =  Time.parse(val)
    end
    if self.respond_to?("#{k.to_s}=")
      self.send("#{k.to_s}=", val)
    else
      self.instance_variable_set("@#{k}", val)
    end
  end
end

- (Object) normalize_nested_params(hash)

Takes a Rails-like nested param hash and normalizes it.



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rturk/xml_utilities.rb', line 35

def normalize_nested_params(hash)
  new_hash = {}
  hash.each do |k,v|
    inner_hash = new_hash
    keys = k.split(/[\[\]]/).reject{|s| s.nil? || s.empty? }
    keys[0...keys.size-1].each do |key|
      inner_hash[key] ||= {}
      inner_hash = inner_hash[key]
    end
    inner_hash[keys.last] = v
  end
  new_hash
end

- (Object) xml_to_hash(noko_xml)



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/rturk/xml_utilities.rb', line 3

def xml_to_hash(noko_xml)
  hash = {}
  noko_xml.children.each do |child|
    next if child.blank?
    if child.text?
      return child.text
    else
      hash[child.name] = xml_to_hash(child)
    end
  end
  hash
end