Class: Hash

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

Overview

Override ruby Hash Obj

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#get_url_paramsObject

Attr to be external accessible



4
5
6
# File 'lib/br_open_data/hash.rb', line 4

def get_url_params
  @get_url_params
end

Class Method Details

.from_xml(xml) ⇒ Object



60
61
62
# File 'lib/br_open_data/hash.rb', line 60

def self.from_xml(xml)
  Nori.new.parse xml
end

Instance Method Details

#it_keys_to_get_paramObject

Convert it keys to get params



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/br_open_data/hash.rb', line 18

def it_keys_to_get_param
  self.it_keys_to_sym
  self.get_url_params = '?'

  self.keys.each do |key|
    self.get_url_params = self.get_url_params+'&' unless self.get_url_params.length == 1

    # Nested obj_attrs
    if self[key].is_a?(Hash)
      hash_name = key
      hash_obj = self[key]

      # Hash to GET URL
      param = to_nested_get_param hash_name, hash_obj
    else
      param="#{key}=#{self[key]}"
    end

    self.get_url_params = self.get_url_params+param
  end

  # Remove the last char: &
  self.get_url_params = self.get_url_params[0..self.get_url_params.length-2] if self.get_url_params.index('\\')
  self.get_url_params
end

#it_keys_to_symObject

Convert string keys to symbol keys



7
8
9
10
11
12
13
14
15
# File 'lib/br_open_data/hash.rb', line 7

def it_keys_to_sym
  self.keys.each do |key|
    self[key].it_keys_to_sym if self[key].is_a?(Hash)
    self[key].each{|v| v.it_keys_to_sym if v.is_a?Hash} if self[key].is_a?Array
    self[(key.to_sym rescue key) || key] = self.delete(key)
  end

  return self
end

#to_nested_get_param(hash_name, hash_obj) ⇒ Object

SetUp a hash to hash URL GET



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/br_open_data/hash.rb', line 45

def to_nested_get_param  hash_name, hash_obj
  # initial value
  get_nested_params = ''

  # foreach keys to mount the URL_PARAM
  hash_obj.keys.each do |key|
    key_param = hash_obj[key].to_nested_get_param key, hash_obj[key] if hash_obj[key].is_a?(Hash)
    key_param = "#{hash_name}[#{key}]=#{hash_obj[key]}&" unless hash_obj[key].is_a?(Hash)
    get_nested_params = get_nested_params+key_param
  end

  # return
  get_nested_params
end