Class: CountOnce

Inherits:
Object
  • Object
show all
Includes:
Concurrent::Async
Defined in:
lib/version.rb,
lib/countonce.rb

Constant Summary collapse

VERSION =
"0.1.0"
DEFAULT_DOMAIN =
"countapi.com"
DEFAULT_SCHEMA =
"https"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(credentials = {}) ⇒ CountOnce

Returns a new instance of CountOnce.



17
18
19
20
21
22
23
24
25
# File 'lib/countonce.rb', line 17

def initialize(credentials = {})
  @account_id = credentials[:account_id]
  @auth_token = credentials[:auth_token]

  api_protocol = ENV.key?("_API_PROTOCOL") ? ENV["_API_PROTOCOL"] : DEFAULT_SCHEMA
  api_domain = ENV.key?("_API_DOMAIN") ? ENV["_API_DOMAIN"] : DEFAULT_DOMAIN
  
  @url = "#{api_protocol}://#{credentials[:account_id]}.#{api_domain}"
end

Instance Attribute Details

#account_idObject

Returns the value of attribute account_id.



13
14
15
# File 'lib/countonce.rb', line 13

def 
  @account_id
end

#auth_tokenObject

Returns the value of attribute auth_token.



14
15
16
# File 'lib/countonce.rb', line 14

def auth_token
  @auth_token
end

#urlObject

Returns the value of attribute url.



15
16
17
# File 'lib/countonce.rb', line 15

def url
  @url
end

Instance Method Details

#getCombined(key_name, query_options = {}, iterator = nil) ⇒ Object



101
102
103
# File 'lib/countonce.rb', line 101

def getCombined(key_name, query_options = {}, iterator = nil)
  self.query(key_name, 'combined', query_options, iterator)
end

#getIncrements(key_name, query_options = {}, iterator = nil) ⇒ Object



93
94
95
# File 'lib/countonce.rb', line 93

def getIncrements(key_name, query_options = {}, iterator = nil)
  self.query(key_name, 'increments', query_options, iterator)
end

#getRevenue(key_name, query_options = {}, iterator = nil) ⇒ Object



97
98
99
# File 'lib/countonce.rb', line 97

def getRevenue(key_name, query_options = {}, iterator = nil)
  self.query(key_name, 'revenue', query_options, iterator)
end

#getUniques(key_name, query_options = {}, iterator = nil) ⇒ Object



89
90
91
# File 'lib/countonce.rb', line 89

def getUniques(key_name, query_options = {}, iterator = nil)
  self.query(key_name, 'uniques', query_options, iterator)
end

#ping(ping_options = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/countonce.rb', line 27

def ping(ping_options = {})
  url_params = {}
  url_params["key"] = ping_options[:key] || ""
  url_params["unique_value"] = ping_options[:unique_value] || ""
  
  headers = {}
  headers["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8" 
  headers["Authorization"] = "Bearer #{@auth_token}" if @auth_token

  if ping_options[:attributes]
    ping_options[:attributes].each {|key, value| url_params["attributes[#{key}]"] = value}
  end

  url_params["revenue"] = ping_options[:revenue] if ping_options[:revenue]
  url_params["timezone"] = ping_options[:timezone] if ping_options[:timezone]

  response = HTTParty.post(
    @url + "/ping",
    :body => url_params,
    :headers => headers,
    :verify => false
  )

  PingResult.new(response.parsed_response)
end

#query(key_name, query_type, query_options = {}, iterator = nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/countonce.rb', line 53

def query(key_name, query_type, query_options = {}, iterator = nil)
  url_params = {}
  url_params["iterator"] = iterator if iterator

  headers = {}
  headers["Authorization"] = "Bearer #{@auth_token}" if @auth_token

  if query_options[:filter]
    query_options[:filter].each {|key, value| url_params["filter[#{key}]"] = value}
  end

  if query_options[:include]
    if query_options[:include].is_a? Array
      query_options[:include] = query_options[:include].join(",")
    end
    
    url_params["include"] = query_options[:include]
  end

  url_params["start_date"] = query_options[:start_date] if query_options[:start_date]
  url_params["end_date"] = query_options[:start_date] if query_options[:end_date]
  url_params["range"] = query_options[:start_date] if query_options[:range]
  url_params["prev_start_date"] = query_options[:start_date] if query_options[:prev_start_date]
  url_params["prev_end_date"] = query_options[:start_date] if query_options[:prev_end_date]
  url_params["prev_range"] = query_options[:start_date] if query_options[:prev_date]

  response = HTTParty.get(
    "#{@url}/#{query_type}/#{key_name}/#{query_options[:metric] || 'daily'}",
    :query => url_params,
    :headers => headers,
    :verify => false
  )

  return QueryResult.new(response.parsed_response)
end