keen 0.8.6 → 0.8.7

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -153,6 +153,12 @@ Keen.query("median", "purchases", :target_property => "price") # => 60
153
153
 
154
154
  This simplifes querying code where the analysis type is dynamic.
155
155
 
156
+ ##### Query Options
157
+
158
+ Each query method or alias takes an optional hash of options as an additional parameter. Possible keys are:
159
+
160
+ `:response` – Set to `:all_keys` to return the full API response (usually only the value of the `"result"` key is returned).
161
+
156
162
  ##### Getting Query URLs
157
163
 
158
164
  Sometimes you just want the URL for a query, but don't actually need to run it. Maybe to paste into a dashboard, or open in your browser. In that case, use the `query_url` method:
@@ -200,6 +206,20 @@ Keen.delete(:signups, filters: [{
200
206
 
201
207
  ### Other code examples
202
208
 
209
+ #### Overwriting event timestamps
210
+
211
+ Two time-related properties are included in your event automatically. The properties “keen.timestamp” and “keen.created_at” are set at the time your event is recorded. You have the ability to overwrite the keen.timestamp property. This could be useful, for example, if you are backfilling historical data. Be sure to use [ISO-8601 Format](https://keen.io/docs/event-data-modeling/event-data-intro/#iso-8601-format).
212
+
213
+ Keen stores all date and time information in UTC!
214
+
215
+ ```ruby
216
+ Keen.publish(:sign_ups, {
217
+ :keen => { :timestamp => "2012-12-14T20:24:01.123000+00:00" },
218
+ :username => "lloyd",
219
+ :referred_by => "harry"
220
+ })
221
+ ```
222
+
203
223
  #### Batch publishing
204
224
 
205
225
  The keen-gem supports publishing events in batches via the `publish_batch` method. Here's an example usage:
@@ -323,6 +343,9 @@ EventMachine itself won't do this because it runs in a different thread. Here's
323
343
 
324
344
  ### Changelog
325
345
 
346
+ ##### 0.8.7
347
+ + Add support for returning all keys back from query API responses
348
+
326
349
  ##### 0.8.6
327
350
  + Add support for getting [query URLs](https://github.com/keenlabs/keen-gem/pull/47)
328
351
  + Make the `query` method public so code supporting dynamic analysis types is easier to write
@@ -80,15 +80,15 @@ module Keen
80
80
  end
81
81
 
82
82
  def ensure_write_key!
83
- raise ConfigurationError, "Write Key must be set for sending events" unless self.write_key
83
+ raise ConfigurationError, "Write Key must be set for this operation" unless self.write_key
84
84
  end
85
85
 
86
86
  def ensure_master_key!
87
- raise ConfigurationError, "Master Key must be set for delete event collections" unless self.master_key
87
+ raise ConfigurationError, "Master Key must be set for this operation" unless self.master_key
88
88
  end
89
89
 
90
90
  def ensure_read_key!
91
- raise ConfigurationError, "Read Key must be set for queries" unless self.read_key
91
+ raise ConfigurationError, "Read Key must be set for this operation" unless self.read_key
92
92
  end
93
93
 
94
94
  def api_event_collection_resource_path(event_collection)
@@ -104,13 +104,7 @@ module Keen
104
104
  preprocess_percentile(params)
105
105
  preprocess_property_names(params)
106
106
 
107
- query_params = ""
108
- params.each do |param, value|
109
- query_params << "#{param}=#{CGI.escape(value)}&"
110
- end
111
-
112
- query_params.chop!
113
- query_params
107
+ params.map { |key, value| "#{key}=#{CGI.escape(value)}" }.join('&')
114
108
  end
115
109
 
116
110
  def preprocess_encodables(params)
@@ -15,8 +15,8 @@ module Keen
15
15
  # interval (optional)
16
16
  # filters (optional) [Array]
17
17
  # timezone (optional)
18
- def count(event_collection, params={})
19
- query(__method__, event_collection, params)
18
+ def count(event_collection, params={}, options={})
19
+ query(__method__, event_collection, params, options)
20
20
  end
21
21
 
22
22
  # Runs a count unique query.
@@ -31,8 +31,8 @@ module Keen
31
31
  # interval (optional)
32
32
  # filters (optional) [Array]
33
33
  # timezone (optional)
34
- def count_unique(event_collection, params)
35
- query(__method__, event_collection, params)
34
+ def count_unique(event_collection, params, options={})
35
+ query(__method__, event_collection, params, options)
36
36
  end
37
37
 
38
38
  # Runs a minimum query.
@@ -47,8 +47,8 @@ module Keen
47
47
  # interval (optional)
48
48
  # filters (optional) [Array]
49
49
  # timezone (optional)
50
- def minimum(event_collection, params)
51
- query(__method__, event_collection, params)
50
+ def minimum(event_collection, params, options={})
51
+ query(__method__, event_collection, params, options)
52
52
  end
53
53
 
54
54
  # Runs a maximum query.
@@ -63,8 +63,8 @@ module Keen
63
63
  # interval (optional)
64
64
  # filters (optional) [Array]
65
65
  # timezone (optional)
66
- def maximum(event_collection, params)
67
- query(__method__, event_collection, params)
66
+ def maximum(event_collection, params, options={})
67
+ query(__method__, event_collection, params, options)
68
68
  end
69
69
 
70
70
  # Runs a sum query.
@@ -79,8 +79,8 @@ module Keen
79
79
  # interval (optional)
80
80
  # filters (optional) [Array]
81
81
  # timezone (optional)
82
- def sum(event_collection, params)
83
- query(__method__, event_collection, params)
82
+ def sum(event_collection, params, options={})
83
+ query(__method__, event_collection, params, options)
84
84
  end
85
85
 
86
86
  # Runs a average query.
@@ -95,8 +95,8 @@ module Keen
95
95
  # interval (optional)
96
96
  # filters (optional) [Array]
97
97
  # timezone (optional)
98
- def average(event_collection, params)
99
- query(__method__, event_collection, params)
98
+ def average(event_collection, params, options={})
99
+ query(__method__, event_collection, params, options)
100
100
  end
101
101
 
102
102
  # Runs a median query.
@@ -111,8 +111,8 @@ module Keen
111
111
  # interval (optional)
112
112
  # filters (optional) [Array]
113
113
  # timezone (optional)
114
- def median(event_collection, params)
115
- query(__method__, event_collection, params)
114
+ def median(event_collection, params, options={})
115
+ query(__method__, event_collection, params, options)
116
116
  end
117
117
 
118
118
  # Runs a percentile query.
@@ -128,8 +128,8 @@ module Keen
128
128
  # interval (optional)
129
129
  # filters (optional) [Array]
130
130
  # timezone (optional)
131
- def percentile(event_collection, params)
132
- query(__method__, event_collection, params)
131
+ def percentile(event_collection, params, options={})
132
+ query(__method__, event_collection, params, options)
133
133
  end
134
134
 
135
135
  # Runs a select_unique query.
@@ -144,8 +144,8 @@ module Keen
144
144
  # interval (optional)
145
145
  # filters (optional) [Array]
146
146
  # timezone (optional)
147
- def select_unique(event_collection, params)
148
- query(__method__, event_collection, params)
147
+ def select_unique(event_collection, params, options={})
148
+ query(__method__, event_collection, params, options)
149
149
  end
150
150
 
151
151
  # Runs a extraction query.
@@ -161,8 +161,8 @@ module Keen
161
161
  # filters (optional) [Array]
162
162
  # timezone (optional)
163
163
  # latest (optional)
164
- def extraction(event_collection, params={})
165
- query(__method__, event_collection, params)
164
+ def extraction(event_collection, params={}, options={})
165
+ query(__method__, event_collection, params, options)
166
166
  end
167
167
 
168
168
  # Runs a funnel query.
@@ -172,8 +172,8 @@ module Keen
172
172
  # @param event_collection
173
173
  # @param params [Hash] (optional)
174
174
  # steps (required)
175
- def funnel(params)
176
- query(__method__, nil, params)
175
+ def funnel(params, options={})
176
+ query(__method__, nil, params, options)
177
177
  end
178
178
 
179
179
  # Runs a multi-analysis query
@@ -189,8 +189,8 @@ module Keen
189
189
  # label (required)
190
190
  # analysis_type (required)
191
191
  # target_property (optional)
192
- def multi_analysis(event_collection, params)
193
- query(__method__, event_collection, params)
192
+ def multi_analysis(event_collection, params, options={})
193
+ query(__method__, event_collection, params, options)
194
194
  end
195
195
 
196
196
  # Returns the URL for a Query without running it
@@ -205,7 +205,7 @@ module Keen
205
205
  # @param options
206
206
  # exclude_api_key
207
207
  def query_url(analysis_type, event_collection, params={}, options={})
208
- str = _query_url(analysis_type, event_collection, params)
208
+ str = _query_url(analysis_type, event_collection, params, options)
209
209
  str << "&api_key=#{self.read_key}" unless options[:exclude_api_key]
210
210
  str
211
211
  end
@@ -219,16 +219,18 @@ module Keen
219
219
  # interval (optional)
220
220
  # filters (optional) [Array]
221
221
  # timezone (optional)
222
- def query(analysis_type, event_collection, params={})
223
- url = _query_url(analysis_type, event_collection, params)
222
+ def query(analysis_type, event_collection, params={}, options={})
223
+ url = _query_url(analysis_type, event_collection, params, options)
224
224
  response = get_response(url)
225
225
  response_body = response.body.chomp
226
- process_response(response.code, response_body)["result"]
226
+ api_result = process_response(response.code, response_body)
227
+ api_result = api_result["result"] unless options[:response] == :all_keys
228
+ api_result
227
229
  end
228
230
 
229
231
  private
230
232
 
231
- def _query_url(analysis_type, event_collection, params={})
233
+ def _query_url(analysis_type, event_collection, params={}, options={})
232
234
  ensure_project_id!
233
235
  ensure_read_key!
234
236
 
@@ -1,3 +1,3 @@
1
1
  module Keen
2
- VERSION = "0.8.6"
2
+ VERSION = "0.8.7"
3
3
  end
@@ -238,6 +238,18 @@ describe "Keen IO API" do
238
238
  results.should == [2, 1]
239
239
  end
240
240
 
241
+ it "should return all keys of valid funnel if full result option is passed" do
242
+ steps = [{
243
+ :event_collection => event_collection,
244
+ :actor_property => "username"
245
+ }, {
246
+ :event_collection => @returns_event_collection,
247
+ :actor_property => "username"
248
+ }]
249
+ results = Keen.funnel({ :steps => steps }, { :response => :all_keys })
250
+ results["result"].should == [2, 1]
251
+ end
252
+
241
253
  it "should apply filters" do
242
254
  Keen.count(event_collection, :filters => [{
243
255
  :property_name => "username",
@@ -69,7 +69,7 @@ describe Keen::Client::PublishingMethods do
69
69
  Keen::Client.new(
70
70
  :project_id => "12345"
71
71
  ).publish(collection, event_properties)
72
- }.to raise_error(Keen::ConfigurationError, "Keen IO Exception: Write Key must be set for sending events")
72
+ }.to raise_error(Keen::ConfigurationError, "Keen IO Exception: Write Key must be set for this operation")
73
73
  end
74
74
  end
75
75
 
@@ -100,7 +100,7 @@ describe Keen::Client::PublishingMethods do
100
100
  Keen::Client.new(
101
101
  :project_id => "12345"
102
102
  ).publish_batch(events)
103
- }.to raise_error(Keen::ConfigurationError, "Keen IO Exception: Write Key must be set for sending events")
103
+ }.to raise_error(Keen::ConfigurationError, "Keen IO Exception: Write Key must be set for this operation")
104
104
  end
105
105
 
106
106
  it "should publish a batch of events" do
@@ -240,7 +240,7 @@ describe Keen::Client::PublishingMethods do
240
240
  Keen::Client.new(
241
241
  :project_id => "12345"
242
242
  ).publish_batch_async(events)
243
- }.to raise_error(Keen::ConfigurationError, "Keen IO Exception: Write Key must be set for sending events")
243
+ }.to raise_error(Keen::ConfigurationError, "Keen IO Exception: Write Key must be set for this operation")
244
244
  end
245
245
 
246
246
  describe "deferrable callbacks" do
@@ -19,14 +19,14 @@ describe Keen::Client do
19
19
 
20
20
  ["minimum", "maximum", "sum", "average", "count", "count_unique", "select_unique", "extraction", "multi_analysis", "median", "percentile"].each do |query_name|
21
21
  it "should call keen query passing the query name" do
22
- client.should_receive(:query).with(query_name.to_sym, event_collection, params)
22
+ client.should_receive(:query).with(query_name.to_sym, event_collection, params, {})
23
23
  client.send(query_name, event_collection, params)
24
24
  end
25
25
  end
26
26
 
27
27
  describe "funnel" do
28
28
  it "should call keen query w/o event collection" do
29
- client.should_receive(:query).with(:funnel, nil, params)
29
+ client.should_receive(:query).with(:funnel, nil, params, {})
30
30
  client.funnel(params)
31
31
  end
32
32
  end
@@ -43,7 +43,7 @@ describe Keen::Client do
43
43
  it "should require a read key" do
44
44
  expect {
45
45
  Keen::Client.new(:project_id => project_id).count("users", {})
46
- }.to raise_error(Keen::ConfigurationError, "Keen IO Exception: Read Key must be set for queries")
46
+ }.to raise_error(Keen::ConfigurationError, "Keen IO Exception: Read Key must be set for this operation")
47
47
  end
48
48
  end
49
49
 
@@ -140,6 +140,13 @@ describe Keen::Client do
140
140
  test_query("&timeframe=#{timeframe_str}", options = {:timeframe => timeframe})
141
141
  options.should eq({:timeframe => timeframe})
142
142
  end
143
+
144
+ it "should return the full API response if the response option is set to all_keys" do
145
+ expected_url = query_url("funnel", "?steps=#{MultiJson.encode([])}")
146
+ stub_keen_get(expected_url, 200, :result => [1])
147
+ api_response = query.call("funnel", nil, { :steps => [] }, { :response => :all_keys })
148
+ api_response.should == { "result" => [1] }
149
+ end
143
150
  end
144
151
  end
145
152
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: keen
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.6
4
+ version: 0.8.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2014-08-04 00:00:00.000000000 Z
14
+ date: 2014-09-18 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: multi_json