countonce 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.travis.yml +6 -0
- data/Gemfile +7 -0
- data/README.md +129 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/countonce.gemspec +29 -0
- data/lib/countonce.rb +104 -0
- data/lib/version.rb +3 -0
- metadata +83 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 85bf33fb4d07145b50872e1c42e5f1cb2ef88c7730e783c6aa11fe6c17d36b68
|
4
|
+
data.tar.gz: feaaf06258daa3ca090ad58e781be320428c6e41aa312a3ba6df11c78b40778a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6a8d2ea49999cae75a31bf4327194175133e656086f386eeb3ce692c3fc2dafab998f2d85ccb41f19e300c8db9f13df608fee6fe31a2191a817e6474ab2ea156
|
7
|
+
data.tar.gz: 033fa55deb979134dda4e15f0df61b396a9d52c5d429cc8ef22d8f6988b3218e0fa005068c0d72265125b5db9498dc9a1a7ea3414b19eefc330cbcbff93a5366
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
# CountOnce-ruby
|
2
|
+
|
3
|
+
Wrapper for the CountOnce API
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'countonce'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install countonce
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### Ping
|
24
|
+
```ruby
|
25
|
+
require "countonce"
|
26
|
+
|
27
|
+
co_client = CountOnce.new({
|
28
|
+
account_id: "<account_id>",
|
29
|
+
auth_token: "<your api auth token>"
|
30
|
+
})
|
31
|
+
|
32
|
+
begin
|
33
|
+
|
34
|
+
response = co_client.ping({
|
35
|
+
key: "<key>",
|
36
|
+
attributes: {
|
37
|
+
account_id: "<attribute value>",
|
38
|
+
action: "<attribute value>"
|
39
|
+
},
|
40
|
+
unique_value: "<unique id or hash>",
|
41
|
+
revenue: 0.00
|
42
|
+
})
|
43
|
+
puts response.json
|
44
|
+
|
45
|
+
rescue StandardError => e
|
46
|
+
puts e.message
|
47
|
+
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
51
|
+
>The '.then' function only works on Windows for now. Hence, the following example is Windows only.
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
require "countonce"
|
55
|
+
|
56
|
+
co_client = CountOnce.new({
|
57
|
+
account_id: "<account_id>",
|
58
|
+
auth_token: "<your api auth token>"
|
59
|
+
})
|
60
|
+
|
61
|
+
begin
|
62
|
+
|
63
|
+
co_client.async.ping({
|
64
|
+
key: "<key>",
|
65
|
+
attributes: {
|
66
|
+
account_id: "<attribute value>",
|
67
|
+
action: "<attribute value>"
|
68
|
+
},
|
69
|
+
unique_value: "<unique id or hash>",
|
70
|
+
revenue: 0.00
|
71
|
+
}).then {|response| puts response.value.json}
|
72
|
+
|
73
|
+
rescue StandardError => e
|
74
|
+
puts e.message
|
75
|
+
|
76
|
+
end
|
77
|
+
```
|
78
|
+
|
79
|
+
### Query
|
80
|
+
```ruby
|
81
|
+
require "countonce"
|
82
|
+
|
83
|
+
co_client = CountOnce.new({
|
84
|
+
account_id: "<account_id>",
|
85
|
+
auth_token: "<your api auth token>"
|
86
|
+
})
|
87
|
+
|
88
|
+
query_options = {
|
89
|
+
metric: "daily"
|
90
|
+
}
|
91
|
+
|
92
|
+
begin
|
93
|
+
|
94
|
+
data = co_client.getUniques("<key>", query_options)
|
95
|
+
p data.json
|
96
|
+
|
97
|
+
rescue StandardError => e
|
98
|
+
puts e.message
|
99
|
+
|
100
|
+
end
|
101
|
+
```
|
102
|
+
|
103
|
+
>Same case as for the ping example. You can use the ```then``` function but only with Windows.
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
require "countonce"
|
107
|
+
|
108
|
+
co_client = CountOnce.new({
|
109
|
+
account_id: "<account_id>",
|
110
|
+
auth_token: "<your api auth token>"
|
111
|
+
})
|
112
|
+
|
113
|
+
query_options = {
|
114
|
+
metric: "daily"
|
115
|
+
}
|
116
|
+
|
117
|
+
begin
|
118
|
+
|
119
|
+
co_client.async.getUniques("<key>", query_options).then {|data|
|
120
|
+
p data.value.json
|
121
|
+
}
|
122
|
+
|
123
|
+
rescue StandardError => e
|
124
|
+
puts e.message
|
125
|
+
|
126
|
+
end
|
127
|
+
```
|
128
|
+
|
129
|
+
For more information on async, please refer to the concurrent-ruby's documentation.
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "countonce/ruby"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/countonce.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require_relative 'lib/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "countonce"
|
5
|
+
spec.version = CountOnce::VERSION
|
6
|
+
spec.authors = ["CountOnce"]
|
7
|
+
spec.email = ["info@countonce.com"]
|
8
|
+
spec.licenses = ['MIT']
|
9
|
+
|
10
|
+
spec.summary = %q{Wrapper for the CountOnce API}
|
11
|
+
spec.homepage = "https://www.countonce.com"
|
12
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
13
|
+
|
14
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
15
|
+
|
16
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
17
|
+
spec.metadata["source_code_uri"] = "https://github.com/countonce/countonce-ruby"
|
18
|
+
|
19
|
+
# Specify which files should be added to the gem when it is released.
|
20
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
21
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
22
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
23
|
+
end
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
|
26
|
+
# dependencies
|
27
|
+
spec.add_dependency "httparty", "~> 0.18.0"
|
28
|
+
spec.add_dependency "concurrent-ruby", "~> 1.1.6"
|
29
|
+
end
|
data/lib/countonce.rb
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
require "httparty"
|
2
|
+
require "concurrent-ruby"
|
3
|
+
|
4
|
+
PingResult = Struct.new(:json)
|
5
|
+
QueryResult = Struct.new(:json)
|
6
|
+
|
7
|
+
class CountOnce
|
8
|
+
include Concurrent::Async
|
9
|
+
|
10
|
+
DEFAULT_DOMAIN = "countapi.com"
|
11
|
+
DEFAULT_SCHEMA = "https"
|
12
|
+
|
13
|
+
attr_accessor :account_id
|
14
|
+
attr_accessor :auth_token
|
15
|
+
attr_accessor :url
|
16
|
+
|
17
|
+
def initialize(credentials = {})
|
18
|
+
@account_id = credentials[:account_id]
|
19
|
+
@auth_token = credentials[:auth_token]
|
20
|
+
|
21
|
+
api_protocol = ENV.key?("_API_PROTOCOL") ? ENV["_API_PROTOCOL"] : DEFAULT_SCHEMA
|
22
|
+
api_domain = ENV.key?("_API_DOMAIN") ? ENV["_API_DOMAIN"] : DEFAULT_DOMAIN
|
23
|
+
|
24
|
+
@url = "#{api_protocol}://#{credentials[:account_id]}.#{api_domain}"
|
25
|
+
end
|
26
|
+
|
27
|
+
def ping(ping_options = {})
|
28
|
+
url_params = {}
|
29
|
+
url_params["key"] = ping_options[:key] || ""
|
30
|
+
url_params["unique_value"] = ping_options[:unique_value] || ""
|
31
|
+
|
32
|
+
headers = {}
|
33
|
+
headers["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8"
|
34
|
+
headers["Authorization"] = "Bearer #{@auth_token}" if @auth_token
|
35
|
+
|
36
|
+
if ping_options[:attributes]
|
37
|
+
ping_options[:attributes].each {|key, value| url_params["attributes[#{key}]"] = value}
|
38
|
+
end
|
39
|
+
|
40
|
+
url_params["revenue"] = ping_options[:revenue] if ping_options[:revenue]
|
41
|
+
url_params["timezone"] = ping_options[:timezone] if ping_options[:timezone]
|
42
|
+
|
43
|
+
response = HTTParty.post(
|
44
|
+
@url + "/ping",
|
45
|
+
:body => url_params,
|
46
|
+
:headers => headers,
|
47
|
+
:verify => false
|
48
|
+
)
|
49
|
+
|
50
|
+
PingResult.new(response.parsed_response)
|
51
|
+
end
|
52
|
+
|
53
|
+
def query(key_name, query_type, query_options = {}, iterator = nil)
|
54
|
+
url_params = {}
|
55
|
+
url_params["iterator"] = iterator if iterator
|
56
|
+
|
57
|
+
headers = {}
|
58
|
+
headers["Authorization"] = "Bearer #{@auth_token}" if @auth_token
|
59
|
+
|
60
|
+
if query_options[:filter]
|
61
|
+
query_options[:filter].each {|key, value| url_params["filter[#{key}]"] = value}
|
62
|
+
end
|
63
|
+
|
64
|
+
if query_options[:include]
|
65
|
+
if query_options[:include].is_a? Array
|
66
|
+
query_options[:include] = query_options[:include].join(",")
|
67
|
+
end
|
68
|
+
|
69
|
+
url_params["include"] = query_options[:include]
|
70
|
+
end
|
71
|
+
|
72
|
+
url_params["start_date"] = query_options[:start_date] if query_options[:start_date]
|
73
|
+
url_params["end_date"] = query_options[:start_date] if query_options[:end_date]
|
74
|
+
url_params["range"] = query_options[:start_date] if query_options[:range]
|
75
|
+
url_params["prev_start_date"] = query_options[:start_date] if query_options[:prev_start_date]
|
76
|
+
url_params["prev_end_date"] = query_options[:start_date] if query_options[:prev_end_date]
|
77
|
+
url_params["prev_range"] = query_options[:start_date] if query_options[:prev_date]
|
78
|
+
|
79
|
+
response = HTTParty.get(
|
80
|
+
"#{@url}/#{query_type}/#{key_name}/#{query_options[:metric] || 'daily'}",
|
81
|
+
:query => url_params,
|
82
|
+
:headers => headers,
|
83
|
+
:verify => false
|
84
|
+
)
|
85
|
+
|
86
|
+
return QueryResult.new(response.parsed_response)
|
87
|
+
end
|
88
|
+
|
89
|
+
def getUniques(key_name, query_options = {}, iterator = nil)
|
90
|
+
self.query(key_name, 'uniques', query_options, iterator)
|
91
|
+
end
|
92
|
+
|
93
|
+
def getIncrements(key_name, query_options = {}, iterator = nil)
|
94
|
+
self.query(key_name, 'increments', query_options, iterator)
|
95
|
+
end
|
96
|
+
|
97
|
+
def getRevenue(key_name, query_options = {}, iterator = nil)
|
98
|
+
self.query(key_name, 'revenue', query_options, iterator)
|
99
|
+
end
|
100
|
+
|
101
|
+
def getCombined(key_name, query_options = {}, iterator = nil)
|
102
|
+
self.query(key_name, 'combined', query_options, iterator)
|
103
|
+
end
|
104
|
+
end
|
data/lib/version.rb
ADDED
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: countonce
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- CountOnce
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-06-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: httparty
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.18.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.18.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: concurrent-ruby
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.1.6
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.1.6
|
41
|
+
description:
|
42
|
+
email:
|
43
|
+
- info@countonce.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- ".travis.yml"
|
50
|
+
- Gemfile
|
51
|
+
- README.md
|
52
|
+
- bin/console
|
53
|
+
- bin/setup
|
54
|
+
- countonce.gemspec
|
55
|
+
- lib/countonce.rb
|
56
|
+
- lib/version.rb
|
57
|
+
homepage: https://www.countonce.com
|
58
|
+
licenses:
|
59
|
+
- MIT
|
60
|
+
metadata:
|
61
|
+
allowed_push_host: https://rubygems.org
|
62
|
+
homepage_uri: https://www.countonce.com
|
63
|
+
source_code_uri: https://github.com/countonce/countonce-ruby
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: 2.3.0
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
requirements: []
|
79
|
+
rubygems_version: 3.1.4
|
80
|
+
signing_key:
|
81
|
+
specification_version: 4
|
82
|
+
summary: Wrapper for the CountOnce API
|
83
|
+
test_files: []
|