module Pipl::Utils
Public Class Methods
alnum_chars(s)
click to toggle source
# File lib/pipl/utils.rb, line 26 def alnum_chars(s) s.gsub(/[^\p{Alnum}]/, '') end
alpha_chars(s)
click to toggle source
# File lib/pipl/utils.rb, line 22 def alpha_chars(s) s.gsub(/[^\p{Alpha}]/, '') end
date_to_str(d)
click to toggle source
# File lib/pipl/utils.rb, line 14 def date_to_str(d) d.strftime(DATE_FORMAT) end
extract_rate_limits(headers={})
click to toggle source
# File lib/pipl/utils.rb, line 47 def extract_rate_limits(headers={}) res = {} res[:qps_allotted] = headers['X-QPS-Allotted'].to_i if headers.key? 'X-QPS-Allotted' res[:qps_current] = headers['X-QPS-Current'].to_i if headers.key? 'X-QPS-Current' res[:qps_live_allotted] = headers['X-QPS-Live-Allotted'].to_i if headers.key? 'X-QPS-Live-Allotted' res[:qps_live_current] = headers['X-QPS-Live-Current'].to_i if headers.key? 'X-QPS-Live-Current' res[:qps_demo_allotted] = headers['X-QPS-Demo-Allotted'].to_i if headers.key? 'X-QPS-Demo-Allotted' res[:qps_demo_current] = headers['X-QPS-Demo-Current'].to_i if headers.key? 'X-QPS-Demo-Current' res[:quota_allotted] = headers['X-APIKey-Quota-Allotted'].to_i if headers.key? 'X-APIKey-Quota-Allotted' res[:quota_current] = headers['X-APIKey-Quota-Current'].to_i if headers.key? 'X-APIKey-Quota-Current' res[:quota_reset] = DateTime.strptime(headers['X-Quota-Reset'], '%A, %B %d, %Y %I:%M:%S %p %Z') if headers.key? 'X-Quota-Reset' res[:demo_usage_allotted] = headers['X-Demo-Usage-Allotted'].to_i if headers.key? 'X-Demo-Usage-Allotted' res[:demo_usage_current] = headers['X-Demo-Usage-Current'].to_i if headers.key? 'X-Demo-Usage-Current' res[:demo_usage_expiry] = DateTime.strptime(headers['X-Demo-Usage-Expiry'], '%A, %B %d, %Y %I:%M:%S %p %Z') if headers.key? 'X-Demo-Usage-Expiry' res end
is_valid_url?(url)
click to toggle source
# File lib/pipl/utils.rb, line 18 def is_valid_url?(url) not ((url =~ URI::ABS_URI).nil?) end
str_to_date(s)
click to toggle source
# File lib/pipl/utils.rb, line 10 def str_to_date(s) Date.strptime(s, DATE_FORMAT) end
titleize(s)
click to toggle source
# File lib/pipl/utils.rb, line 30 def titleize(s) s.gsub(/\w+/) { |x| x.capitalize } end
to_utf8(obj)
click to toggle source
# File lib/pipl/utils.rb, line 34 def to_utf8(obj) if obj.respond_to?(:encode) begin obj.encode('UTF-8') rescue Exception puts "Could not convert #{obj} to UTF-8" raise end else obj end end