module DaFace::Utilities

Public Instance Methods

parse_json(data) click to toggle source
# File lib/da_face/utilities.rb, line 48
def parse_json data
  Yajl::Parser.new.parse(data)
end
parse_timestamp(timestamp=nil) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

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

# 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