Module: GrepdataClient::Utils
- Defined in:
- lib/grepdata_client/utils.rb
Class Method Summary collapse
- .cache_buster ⇒ Object
- .check_attributes(name, options) ⇒ Object
- .date_format ⇒ Object
- .default_expiration ⇒ Object
- .format_params(action, params) ⇒ Object
- .generate_key(api_key, options) ⇒ Object
- .preprocess_dates(params, attributes) ⇒ Object
Class Method Details
.cache_buster ⇒ Object
7 8 9 |
# File 'lib/grepdata_client/utils.rb', line 7 def self.cache_buster Time.now.getutc.to_i.to_s end |
.check_attributes(name, options) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/grepdata_client/utils.rb', line 24 def self.check_attributes(name, ) params = [:params] required = [:required] missing = [] required.each_key do |key| if not params[key].is_a? required[key] = "#{name} missing required attribute #{key.to_s} of type #{required[key].name}" missing.push puts "Warning: #{message}" end end raise "Error: #{name} missing required attributes" if missing.length > 0 end |
.date_format ⇒ Object
3 4 5 |
# File 'lib/grepdata_client/utils.rb', line 3 def self.date_format "%Y%m%d%H%M" end |
.default_expiration ⇒ Object
11 12 13 |
# File 'lib/grepdata_client/utils.rb', line 11 def self.default_expiration (Time.now.utc + (24*60*60)).strftime Utils.date_format end |
.format_params(action, params) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/grepdata_client/utils.rb', line 46 def self.format_params(action, params) result = {} result[:endpoint] = params[:endpoint] if params[:endpoint] result[:datamart] = params[:datamart] if params[:datamart] result[:metrics] = params[:metrics].join(',') if params[:metrics] result[:dimensions] = params[:dimensions].join(',') if params[:dimensions] result[:filters] = params[:filters].to_json if params[:filters] result[:time_interval] = params[:time_interval] if params[:time_interval] if action == "funneling" steps = [] params[:steps].each do |step| step[:start_date] = params[:start_date] step[:end_date] = params[:end_date] steps.push step end result[:steps] = { :steps => steps }.to_json result[:funnel_dimension] = params[:funnel_dimension] else result[:start_date] = params[:start_date] result[:end_date] = params[:end_date] end result[:api_key] = params[:api_key] if params[:api_key] result[:token] = params[:token] if params[:token] result[:signature] = params[:signature] if params[:signature] result[:restricted] = params[:restricted] if params[:restricted] result[:expiration] = params[:expiration] if params[:expiration] result end |
.generate_key(api_key, options) ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/grepdata_client/utils.rb', line 15 def self.generate_key(api_key, ) identity = "#{options[:datamart]}\n" identity += "#{options[:values]}\n" if [:values].length > 0 identity += [:expiration] #hash identity using HMAC-SHA1. return as base64 encoded string Base64.encode64(OpenSSL::HMAC.digest('sha1', api_key, identity)).chomp end |
.preprocess_dates(params, attributes) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/grepdata_client/utils.rb', line 39 def self.preprocess_dates(params, attributes) attributes.each do |attr| date = DateTime.parse params[attr] params[attr] = date.strftime Utils.date_format end end |