Module: DaFace::Utilities

Instance Method Summary collapse

Instance Method Details

#parse_json(data) ⇒ Object



48
49
50
# File 'lib/da_face/utilities.rb', line 48

def parse_json data
  Yajl::Parser.new.parse(data)
end

#parse_timestamp(timestamp = nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/da_face/utilities.rb', line 36

def parse_timestamp timestamp=nil
  return nil unless timestamp
  return Time.at(timestamp) if timestamp.kind_of? Fixnum
  return Time.at(timestamp) if timestamp.kind_of? Float
  begin
    return Time.parse(timestamp) if timestamp.kind_of? String
  rescue ArgumentError => error
    return timestamp
  end
  return timestamp
end

#parse_uri(url) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/da_face/utilities.rb', line 28

def parse_uri url
  begin
    URI(URI.encode(url)) if url
  rescue URI::InvalidURIError
    return url
  end
end

#symbolize_keys(keys, hash) ⇒ Object

Creates a new hash with all keys as symbols, can be any level of depth



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/da_face/utilities.rb', line 6

def symbolize_keys keys, hash
  new_hash = {}
  keys.each do |key|
    if hash[key].kind_of? Hash
      new_hash[key.to_sym] = symbolize_keys(hash[key].keys, hash[key])
    elsif hash[key].kind_of? Array
      new_hash[key.to_sym] = []
      hash[key].each do |item|
        if item.kind_of? Hash
          new_hash[key.to_sym] << symbolize_keys(item.keys, item)
        else
          new_hash[key.to_sym] << item
        end
      end
    else
      new_hash[key.to_sym] = hash[key]
    end
  end

  return new_hash
end