Module: GrepdataClient::Utils

Defined in:
lib/grepdata_client/utils.rb

Class Method Summary collapse

Class Method Details

.check_attributes(name, options) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/grepdata_client/utils.rb', line 20

def self.check_attributes(name, options)
  params  = options[:params]
  required = options[:required]
  
  missing = []
  required.each_key do |key|
    if not params[key].is_a? required[key]
      message = "#{name} missing required attribute #{key.to_s} of type #{required[key].name}"
      missing.push message
      puts "Warning: #{message}"
    end
  end
  raise "Error: #{name} missing required attributes" if missing.length > 0
end

.date_formatObject



3
4
5
# File 'lib/grepdata_client/utils.rb', line 3

def self.date_format
  "%Y%m%d%H%M"
end

.default_expirationObject



7
8
9
# File 'lib/grepdata_client/utils.rb', line 7

def self.default_expiration
  (Time.now.utc + (24*60*60)).strftime Utils.date_format
end

.format_params(action, params) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/grepdata_client/utils.rb', line 42

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
end

.generate_key(api_key, options) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/grepdata_client/utils.rb', line 11

def self.generate_key(api_key, options)
  identity = "#{options[:datamart]}\n"
  identity += "#{options[:values]}\n" if options[:values].length > 0
  identity += options[: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



35
36
37
38
39
40
# File 'lib/grepdata_client/utils.rb', line 35

def self.preprocess_dates(params, attributes)
  attributes.each do |attr|
    date = DateTime.parse params[attr]
    params[attr] = date.strftime Utils.date_format
  end
end