grepdata_client 0.0.1 → 0.0.2
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.
- data/README.md +141 -14
- data/lib/grepdata_client/query.rb +7 -3
- data/lib/grepdata_client/utils.rb +8 -0
- data/lib/grepdata_client/version.rb +1 -1
- data/lib/grepdata_client.rb +70 -7
- metadata +1 -1
data/README.md
CHANGED
|
@@ -1,29 +1,156 @@
|
|
|
1
1
|
# GrepdataClient
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Official Ruby client for Grepdata API
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
gem install grepdata_client
|
|
8
|
+
|
|
9
|
+
or if you use a Gemfile:
|
|
8
10
|
|
|
9
11
|
gem 'grepdata_client'
|
|
12
|
+
|
|
13
|
+
## Publishing Events
|
|
14
|
+
|
|
15
|
+
require 'rubygems'
|
|
16
|
+
require 'grepdata_client'
|
|
17
|
+
|
|
18
|
+
client = GrepdataClient::Client.new(token: "054a9c9ade7dcf325a3aab542ebd73b5", default_endpoint: 'demonstration')
|
|
19
|
+
client.track 'play', data: { age: 18 }
|
|
20
|
+
|
|
21
|
+
## Querying Data
|
|
22
|
+
|
|
23
|
+
require 'rubygems'
|
|
24
|
+
require 'grepdata_client'
|
|
25
|
+
|
|
26
|
+
client = GrepdataClient::Client.new(:api_key => "0ac15f3688987af763c67412066e3378")
|
|
27
|
+
|
|
28
|
+
#Query
|
|
29
|
+
params = {
|
|
30
|
+
:datamart => "user_info",
|
|
31
|
+
:dimensions => %w(country),
|
|
32
|
+
:metrics => %w(Count),
|
|
33
|
+
:filters => { :country => %w(US) },
|
|
34
|
+
:time_interval => "h",
|
|
35
|
+
:start_date => "201306110800",
|
|
36
|
+
:end_date => "201306110900"
|
|
37
|
+
}
|
|
38
|
+
req = client.query params
|
|
39
|
+
puts req.get_result
|
|
40
|
+
|
|
41
|
+
#Funneling
|
|
42
|
+
params = {
|
|
43
|
+
:datamart => 'demonstration',
|
|
44
|
+
:funnel_dimension => 'event',
|
|
45
|
+
:time_interval => 'd',
|
|
46
|
+
:dimensions => %w(event country),
|
|
47
|
+
:metrics => %w(Count),
|
|
48
|
+
:start_date => "201306120000",
|
|
49
|
+
:end_date => "201306190000",
|
|
50
|
+
:steps => [
|
|
51
|
+
{ :name => "step1: play", :value => "play" },
|
|
52
|
+
{ :name => "step2: pause", :value => "pause" },
|
|
53
|
+
{ :name => "step3: seek", :value => "seek" },
|
|
54
|
+
{ :name => "step2: stop", :value => "stop" }
|
|
55
|
+
],
|
|
56
|
+
:filters => { :country => %w(US) },
|
|
57
|
+
:only_totals => false
|
|
58
|
+
}
|
|
59
|
+
req = client.funneling params
|
|
60
|
+
puts req.get_result
|
|
61
|
+
|
|
62
|
+
## Generating access key
|
|
63
|
+
|
|
64
|
+
require 'rubygems'
|
|
65
|
+
require 'grepdata_client'
|
|
66
|
+
|
|
67
|
+
client = GrepdataClient::Client.new(:api_key => "0ac15f3688987af763c67412066e3378")
|
|
68
|
+
|
|
69
|
+
params = {
|
|
70
|
+
:datamart => "user_info",
|
|
71
|
+
:dimensions => %w(country),
|
|
72
|
+
:metrics => %w(Count),
|
|
73
|
+
:filters => { :country => %w(US) },
|
|
74
|
+
:time_interval => "h",
|
|
75
|
+
:start_date => "201306110800",
|
|
76
|
+
:end_date => "201306110900"
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
access_key = client.generate_access_key api_key,
|
|
80
|
+
params: params,
|
|
81
|
+
restricted: ['dimensions', 'filters.country'],
|
|
82
|
+
expiration: '201306220100'
|
|
83
|
+
|
|
84
|
+
puts access_key
|
|
85
|
+
|
|
86
|
+
## Querying with access key
|
|
87
|
+
|
|
88
|
+
require 'rubygems'
|
|
89
|
+
require 'grepdata_client'
|
|
90
|
+
|
|
91
|
+
client = GrepdataClient::Client.new(:token => "054a9c9ade7dcf325a3aab542ebd73b5")
|
|
92
|
+
|
|
93
|
+
#acquired via generate_access_key
|
|
94
|
+
access_key = {
|
|
95
|
+
:signature=>"0xBBKoaUe6RZSLM//6yqzbYelmI=",
|
|
96
|
+
:restricted=>"dimensions,filters.country",
|
|
97
|
+
:expiration=>"201306220100",
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
params = {
|
|
101
|
+
:datamart => "user_info",
|
|
102
|
+
:dimensions => %w(country),
|
|
103
|
+
:metrics => %w(Count),
|
|
104
|
+
:filters => { :country => %w(US) },
|
|
105
|
+
:time_interval => "h",
|
|
106
|
+
:start_date => "201306110800",
|
|
107
|
+
:end_date => "201306110900"
|
|
108
|
+
}
|
|
10
109
|
|
|
11
|
-
|
|
110
|
+
req = client.query_with_token params, access_key
|
|
111
|
+
puts req.get_result
|
|
112
|
+
|
|
113
|
+
#get url of the request
|
|
114
|
+
puts req.get_url
|
|
12
115
|
|
|
13
|
-
|
|
116
|
+
## Running Request in Parallel
|
|
14
117
|
|
|
15
|
-
|
|
118
|
+
require 'rubygems'
|
|
119
|
+
require 'grepdata_client'
|
|
16
120
|
|
|
17
|
-
|
|
121
|
+
#set parallel to true
|
|
122
|
+
client = GrepdataClient::Client.new(
|
|
123
|
+
:default_endpoint => 'demonstration',
|
|
124
|
+
:token => "054a9c9ade7dcf325a3aab542ebd73b5",
|
|
125
|
+
:api_key => "0ac15f3688987af763c67412066e3378",
|
|
126
|
+
:parallel => true)
|
|
127
|
+
|
|
128
|
+
# query or publish data. Requests will be queued up
|
|
129
|
+
...
|
|
130
|
+
|
|
131
|
+
# execute the queued requests and run them in parallel
|
|
132
|
+
client.run_requests
|
|
133
|
+
|
|
134
|
+
We use Typhoeus to handle parallel requests. You can also pass in your own hydra queue
|
|
18
135
|
|
|
19
|
-
|
|
136
|
+
require 'rubygems'
|
|
137
|
+
require 'grepdata_client'
|
|
20
138
|
|
|
21
|
-
|
|
139
|
+
#set parallel to true and pass in hydra queue
|
|
140
|
+
hydra = ::Typhoeus::Hydra.new
|
|
141
|
+
client = GrepdataClient::Client.new(
|
|
142
|
+
:default_endpoint => 'demonstration',
|
|
143
|
+
:token => "054a9c9ade7dcf325a3aab542ebd73b5",
|
|
144
|
+
:api_key => "0ac15f3688987af763c67412066e3378",
|
|
145
|
+
:parallel => true,
|
|
146
|
+
:parallel_manager => hydra)
|
|
147
|
+
|
|
148
|
+
# query or publish data. Requests will be queued up
|
|
149
|
+
...
|
|
150
|
+
|
|
151
|
+
# this will execute the queued requests as well
|
|
152
|
+
hydra.run
|
|
22
153
|
|
|
23
|
-
##
|
|
154
|
+
## Copyright
|
|
24
155
|
|
|
25
|
-
|
|
26
|
-
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
27
|
-
3. Commit your changes (`git commit -am 'Add some feature'`)
|
|
28
|
-
4. Push to the branch (`git push origin my-new-feature`)
|
|
29
|
-
5. Create new Pull Request
|
|
156
|
+
Copyright (c) 2013+ Grepdata. See LICENSE for details.
|
|
@@ -1,20 +1,24 @@
|
|
|
1
1
|
module GrepdataClient
|
|
2
2
|
class DataRequest
|
|
3
|
-
attr_reader :request, :
|
|
3
|
+
attr_reader :request, :base_url, :params, :headers, :action
|
|
4
4
|
|
|
5
5
|
def initialize(action, options)
|
|
6
6
|
@action = action.to_s
|
|
7
|
-
@
|
|
7
|
+
@base_url = "#{options[:url]}/#{@action}"
|
|
8
8
|
@params = options[:params] || {}
|
|
9
9
|
@headers = options[:headers] || {}
|
|
10
10
|
|
|
11
11
|
params = Utils.format_params @action, @params
|
|
12
12
|
|
|
13
|
-
@request = ::Typhoeus::Request.new(@
|
|
13
|
+
@request = ::Typhoeus::Request.new(@base_url, params: params, headers: @headers)
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
def get_result
|
|
17
17
|
return @request.response.body
|
|
18
18
|
end
|
|
19
|
+
|
|
20
|
+
def get_url
|
|
21
|
+
return @request.url
|
|
22
|
+
end
|
|
19
23
|
end
|
|
20
24
|
end
|
|
@@ -4,6 +4,10 @@ module GrepdataClient
|
|
|
4
4
|
"%Y%m%d%H%M"
|
|
5
5
|
end
|
|
6
6
|
|
|
7
|
+
def self.cache_buster
|
|
8
|
+
Time.now.getutc.to_i.to_s
|
|
9
|
+
end
|
|
10
|
+
|
|
7
11
|
def self.default_expiration
|
|
8
12
|
(Time.now.utc + (24*60*60)).strftime Utils.date_format
|
|
9
13
|
end
|
|
@@ -64,6 +68,10 @@ module GrepdataClient
|
|
|
64
68
|
|
|
65
69
|
result[:api_key] = params[:api_key] if params[:api_key]
|
|
66
70
|
result[:token] = params[:token] if params[:token]
|
|
71
|
+
|
|
72
|
+
result[:signature] = params[:signature] if params[:signature]
|
|
73
|
+
result[:restricted] = params[:restricted] if params[:restricted]
|
|
74
|
+
result[:expiration] = params[:expiration] if params[:expiration]
|
|
67
75
|
result
|
|
68
76
|
end
|
|
69
77
|
end
|
data/lib/grepdata_client.rb
CHANGED
|
@@ -11,20 +11,74 @@ require "grepdata_client/utils"
|
|
|
11
11
|
module GrepdataClient
|
|
12
12
|
class Client
|
|
13
13
|
|
|
14
|
-
attr_accessor :api_url, :api_key, :token, :
|
|
14
|
+
attr_accessor :api_url, :api_key, :token, :send_with_headers,
|
|
15
|
+
:default_endpoint, :endpoint_map,
|
|
16
|
+
:parallel, :parallel_manager
|
|
15
17
|
|
|
16
18
|
CONFIG = {
|
|
17
19
|
:api_url => "https://api.grepdata.com/v1",
|
|
20
|
+
:beacon_url => "https://beacon.grepdata.com/v1"
|
|
18
21
|
}
|
|
19
22
|
|
|
20
23
|
def initialize(config)
|
|
21
|
-
@api_key, @token =
|
|
24
|
+
@api_key, @token, @default_endpoint =
|
|
25
|
+
config.values_at(:api_key, :token, :default_endpoint)
|
|
26
|
+
|
|
27
|
+
@send_with_headers = config[:send_with_headers] || false
|
|
28
|
+
@endpoint_map = config[:endpoint_map] || {}
|
|
22
29
|
@api_url = config[:api_url] || CONFIG[:api_url]
|
|
30
|
+
@beacon_url = config[:beacon_url] || CONFIG[:beacon_url]
|
|
31
|
+
|
|
23
32
|
@parallel = config[:parallel] || false
|
|
33
|
+
|
|
24
34
|
if config[:parallel_manager]
|
|
25
35
|
@parallel_manager = config[:parallel_manager]
|
|
26
36
|
end
|
|
27
37
|
end
|
|
38
|
+
|
|
39
|
+
def track(event, options)
|
|
40
|
+
token = options[:token] || @token
|
|
41
|
+
endpoint = @endpoint_map[event.to_sym] || @endpoint_map[event] || @default_endpoint
|
|
42
|
+
data = options[:data]
|
|
43
|
+
|
|
44
|
+
Utils.check_attributes "Request",
|
|
45
|
+
params: { token: token, endpoint: endpoint, data: data},
|
|
46
|
+
required: {
|
|
47
|
+
token: String,
|
|
48
|
+
endpoint: String,
|
|
49
|
+
data: Hash
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
params = {
|
|
53
|
+
event: event,
|
|
54
|
+
q: data.to_json,
|
|
55
|
+
cb: Utils.cache_buster,
|
|
56
|
+
token: token
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
params[:t] = options[:timestamp] if options[:timestamp]
|
|
60
|
+
params[:domain] = options[:domain] if options[:domain]
|
|
61
|
+
params[:ua] = options[:user_agent] if options[:user_agent]
|
|
62
|
+
params[:r] = options[:referer] if options[:referer]
|
|
63
|
+
params[:ip] = options[:ip] if options[:ip]
|
|
64
|
+
params[:visitor] = options[:visitor] if options[:visitor]
|
|
65
|
+
params[:session] = options[:session] if options[:session]
|
|
66
|
+
|
|
67
|
+
url = "#{@beacon_url}/#{endpoint}"
|
|
68
|
+
|
|
69
|
+
request = Typhoeus::Request.new url, params: params, timeout: 5
|
|
70
|
+
|
|
71
|
+
if @parallel
|
|
72
|
+
unless @parallel_manager
|
|
73
|
+
@parallel_manager = ::Typhoeus::Hydra.new
|
|
74
|
+
end
|
|
75
|
+
@parallel_manager.queue request
|
|
76
|
+
else
|
|
77
|
+
request.run
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
request
|
|
81
|
+
end
|
|
28
82
|
|
|
29
83
|
def query(params)
|
|
30
84
|
params[:api_key] = params[:api_key] || @api_key
|
|
@@ -32,7 +86,7 @@ module GrepdataClient
|
|
|
32
86
|
|
|
33
87
|
Utils.preprocess_dates params, [:start_date, :end_date]
|
|
34
88
|
|
|
35
|
-
Utils.check_attributes "
|
|
89
|
+
Utils.check_attributes "Request",
|
|
36
90
|
params: params,
|
|
37
91
|
required: {
|
|
38
92
|
api_key: String,
|
|
@@ -55,7 +109,7 @@ module GrepdataClient
|
|
|
55
109
|
|
|
56
110
|
Utils.preprocess_dates params, [:start_date, :end_date]
|
|
57
111
|
|
|
58
|
-
Utils.check_attributes "
|
|
112
|
+
Utils.check_attributes "Request",
|
|
59
113
|
params: params,
|
|
60
114
|
required: {
|
|
61
115
|
api_key: String,
|
|
@@ -76,7 +130,7 @@ module GrepdataClient
|
|
|
76
130
|
def dimensions(params)
|
|
77
131
|
params[:api_key] = params[:api_key] || @api_key
|
|
78
132
|
|
|
79
|
-
Utils.check_attributes "
|
|
133
|
+
Utils.check_attributes "Request",
|
|
80
134
|
params: params,
|
|
81
135
|
required: {
|
|
82
136
|
api_key: String,
|
|
@@ -93,7 +147,7 @@ module GrepdataClient
|
|
|
93
147
|
Utils.preprocess_dates params, [:start_date, :end_date]
|
|
94
148
|
Utils.preprocess_dates access_key, [:expiration]
|
|
95
149
|
|
|
96
|
-
Utils.check_attributes "
|
|
150
|
+
Utils.check_attributes "Request",
|
|
97
151
|
params: params,
|
|
98
152
|
required: {
|
|
99
153
|
token: String,
|
|
@@ -113,8 +167,17 @@ module GrepdataClient
|
|
|
113
167
|
restricted: String,
|
|
114
168
|
expiration: String
|
|
115
169
|
}
|
|
170
|
+
|
|
171
|
+
headers = {}
|
|
172
|
+
if @send_with_headers
|
|
173
|
+
headers = access_key
|
|
174
|
+
else
|
|
175
|
+
params[:signature] = access_key[:signature]
|
|
176
|
+
params[:restricted] = access_key[:restricted]
|
|
177
|
+
params[:expiration] = access_key[:expiration]
|
|
178
|
+
end
|
|
116
179
|
|
|
117
|
-
request('fetch', params: params, headers:
|
|
180
|
+
request('fetch', params: params, headers: headers)
|
|
118
181
|
end
|
|
119
182
|
|
|
120
183
|
def run_requests
|