Module: DaFace::Utilities
- Included in:
- Api::Adapter, Api::PushSubscription, Datasift::Interaction, Datasift::Link, Datasift::Parser, Datasift::Twitter, Twitter::Parser, Twitter::Tweet, Twitter::User
- Defined in:
- lib/da_face/utilities.rb
Instance Method Summary collapse
- #parse_json(data) ⇒ Object
- #parse_timestamp(timestamp = nil) ⇒ Object
- #parse_uri(url) ⇒ Object
-
#symbolize_keys(keys, hash) ⇒ Object
Creates a new hash with all keys as symbols, can be any level of depth.
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 =nil return nil unless return Time.at() if .kind_of? Fixnum return Time.at() if .kind_of? Float begin return Time.parse() if .kind_of? String rescue ArgumentError => error return end return 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 |