keen 0.6.1 → 0.7.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.
- data/.travis.yml +1 -1
- data/README.md +22 -9
- data/keen.gemspec +1 -1
- data/lib/keen.rb +4 -3
- data/lib/keen/client.rb +15 -9
- data/lib/keen/client/publishing_methods.rb +7 -5
- data/lib/keen/client/querying_methods.rb +2 -5
- data/lib/keen/version.rb +1 -1
- data/spec/integration/api_spec.rb +3 -2
- data/spec/keen/client/publishing_methods_spec.rb +20 -9
- data/spec/keen/client/querying_methods_spec.rb +14 -14
- data/spec/keen/client_spec.rb +10 -6
- data/spec/keen/keen_spec.rb +10 -5
- data/spec/spec_helper.rb +10 -7
- data/spec/synchrony/synchrony_spec.rb +3 -2
- metadata +3 -2
data/.travis.yml
CHANGED
@@ -10,7 +10,7 @@ rvm:
|
|
10
10
|
|
11
11
|
env:
|
12
12
|
- PATTERN=keen
|
13
|
-
- PATTERN=integration
|
13
|
+
- PATTERN=integration KEEN_WRITE_KEY=f806128f31c349fda124b62d1f4cf4b2 KEEN_READ_KEY=f806128f31c349fda124b62d1f4cf4b2 KEEN_PROJECT_ID=50e5ffa6897a2c319b000000
|
14
14
|
- PATTERN=synchrony
|
15
15
|
|
16
16
|
matrix:
|
data/README.md
CHANGED
@@ -23,13 +23,14 @@ keen is tested with Ruby 1.8 and 1.9 on:
|
|
23
23
|
|
24
24
|
### Usage
|
25
25
|
|
26
|
-
Before making any API calls, you must supply keen-gem with a Project ID.
|
27
|
-
(If you need a Keen IO account, [sign up here](https://keen.io/) - it's free.)
|
26
|
+
Before making any API calls, you must supply keen-gem with a Project ID and one or both of your Write and Read Keys.
|
27
|
+
(If you need a Keen IO account, [sign up here](https://keen.io/) - it's free.) The Write key is required for publishing
|
28
|
+
events, and the Read key is required for running queries.
|
28
29
|
|
29
|
-
The recommended way to do this is to set `KEEN_PROJECT_ID` in your
|
30
|
+
The recommended way to do this is to set `KEEN_PROJECT_ID`, `KEEN_WRITE_KEY`, and `KEEN_READ_KEY` in your
|
30
31
|
environment. If you're using [foreman](http://ddollar.github.com/foreman/), add this to your `.env` file:
|
31
32
|
|
32
|
-
KEEN_PROJECT_ID=xxxxxxxxxxxxxxxx
|
33
|
+
KEEN_PROJECT_ID=xxxxxxxxxxxxxxxx KEEN_WRITE_KEY=yyyyyyyyyyyyy KEEN_READ_KEY=zzzzzzzzzzzzz
|
33
34
|
|
34
35
|
If not, make to to export the variable into your shell or put it before the command you use to start your server.
|
35
36
|
|
@@ -95,9 +96,9 @@ to resume processing immediately.
|
|
95
96
|
|
96
97
|
The Keen IO API provides rich querying capabilities against your event data set. For more information, see the [Data Analysis API Guide](https://keen.io/docs/data-analysis/).
|
97
98
|
|
98
|
-
|
99
|
+
Queries require that a Read Key is provided. Just like project ID, we encourage that you set this as an environment variable:
|
99
100
|
|
100
|
-
|
101
|
+
KEEN_READ_KEY=yyyyyyyyyyyyyyyy
|
101
102
|
|
102
103
|
Here's are some examples of querying with keen-gem. Let's assume you've added some events to the "purchases" collection.
|
103
104
|
|
@@ -137,13 +138,17 @@ Detailed information on available parameters for each API resource can be found
|
|
137
138
|
To configure keen-gem in code, do as follows:
|
138
139
|
|
139
140
|
```ruby
|
140
|
-
Keen.project_id = '
|
141
|
+
Keen.project_id = 'xxxxxxxxxxxxxxx'
|
142
|
+
Keen.write_key = 'yyyyyyyyyyyyyyy'
|
143
|
+
Keen.read_key = 'zzzzzzzzzzzzzzz'
|
141
144
|
```
|
142
145
|
|
143
146
|
You can also configure individual client instances as follows:
|
144
147
|
|
145
148
|
```ruby
|
146
|
-
keen = Keen::Client.new(:project_id => '
|
149
|
+
keen = Keen::Client.new(:project_id => 'xxxxxxxxxxxxxxx',
|
150
|
+
:write_key => 'yyyyyyyyyyyyyyy',
|
151
|
+
:read_key => 'zzzzzzzzzzzzzzz')
|
147
152
|
```
|
148
153
|
|
149
154
|
#### em-synchrony
|
@@ -162,14 +167,22 @@ In this situation, the JSON event data is passed by encoding it base-64 and addi
|
|
162
167
|
The `beacon_url` method found on the `Keen::Client` does this for you. Here's an example:
|
163
168
|
|
164
169
|
```ruby
|
170
|
+
Keen.project_id = 'xxxxxx';
|
171
|
+
Keen.write_key = 'yyyyyy';
|
165
172
|
Keen.beacon_url("sign_ups", :recipient => "foo@foo.com")
|
166
|
-
# => "https://api.keen.io/3.0/projects/
|
173
|
+
# => "https://api.keen.io/3.0/projects/xxxxxx/events/email_opens?api_key=yyyyyy&data=eyJyZWNpcGllbnQiOiJmb29AZm9vLmNvbSJ9"
|
167
174
|
```
|
168
175
|
|
169
176
|
To track email opens, simply add an image to your email template that points to this URL.
|
170
177
|
|
171
178
|
### Changelog
|
172
179
|
|
180
|
+
##### 0.7.0
|
181
|
+
+ BREAKING CHANGE! Added support for read and write scoped keys to reflect the new Keen IO security architecture.
|
182
|
+
The advantage of scoped keys is finer grained permission control. Public clients that
|
183
|
+
publish events (like a web browser) require a key that can write but not read. On the other hand, private dashboards and
|
184
|
+
server-side querying processes require a Read key that should not be made public.
|
185
|
+
|
173
186
|
##### 0.6.1
|
174
187
|
+ Improved logging and exception handling.
|
175
188
|
|
data/keen.gemspec
CHANGED
@@ -6,7 +6,7 @@ Gem::Specification.new do |s|
|
|
6
6
|
s.name = "keen"
|
7
7
|
s.version = Keen::VERSION
|
8
8
|
s.platform = Gem::Platform::RUBY
|
9
|
-
s.authors = ["Kyle Wild", "Josh Dzielak"]
|
9
|
+
s.authors = ["Kyle Wild", "Josh Dzielak", "Daniel Kador"]
|
10
10
|
s.email = "josh@keen.io"
|
11
11
|
s.homepage = "https://github.com/keenlabs/keen-gem"
|
12
12
|
s.summary = "Keen IO API Client"
|
data/lib/keen.rb
CHANGED
@@ -25,8 +25,8 @@ module Keen
|
|
25
25
|
class << self
|
26
26
|
extend Forwardable
|
27
27
|
|
28
|
-
def_delegators :default_client, :project_id, :
|
29
|
-
:project_id=, :
|
28
|
+
def_delegators :default_client, :project_id, :write_key, :read_key,
|
29
|
+
:project_id=, :write_key=, :read_key=, :publish, :publish_async,
|
30
30
|
:beacon_url, :count, :count_unique, :minimum, :maximum,
|
31
31
|
:sum, :average, :select_unique, :funnel, :extraction
|
32
32
|
|
@@ -45,7 +45,8 @@ module Keen
|
|
45
45
|
def default_client
|
46
46
|
@default_client ||= Keen::Client.new(
|
47
47
|
:project_id => ENV['KEEN_PROJECT_ID'],
|
48
|
-
:
|
48
|
+
:write_key => ENV['KEEN_WRITE_KEY'],
|
49
|
+
:read_key => ENV['KEEN_READ_KEY']
|
49
50
|
)
|
50
51
|
end
|
51
52
|
end
|
data/lib/keen/client.rb
CHANGED
@@ -13,7 +13,7 @@ module Keen
|
|
13
13
|
include Keen::Client::PublishingMethods
|
14
14
|
include Keen::Client::QueryingMethods
|
15
15
|
|
16
|
-
attr_accessor :project_id, :
|
16
|
+
attr_accessor :project_id, :write_key, :read_key
|
17
17
|
|
18
18
|
CONFIG = {
|
19
19
|
:api_host => "api.keen.io",
|
@@ -25,14 +25,15 @@ module Keen
|
|
25
25
|
:verify_depth => 5,
|
26
26
|
:ca_file => File.expand_path("../../../config/cacert.pem", __FILE__) },
|
27
27
|
:api_async_http_options => {},
|
28
|
-
:api_headers => lambda { |sync_or_async|
|
28
|
+
:api_headers => lambda { |authorization, sync_or_async|
|
29
29
|
user_agent = "keen-gem, v#{Keen::VERSION}, #{sync_or_async}"
|
30
30
|
user_agent += ", #{RUBY_VERSION}, #{RUBY_PLATFORM}, #{RUBY_PATCHLEVEL}"
|
31
31
|
if defined?(RUBY_ENGINE)
|
32
32
|
user_agent += ", #{RUBY_ENGINE}"
|
33
33
|
end
|
34
34
|
{ "Content-Type" => "application/json",
|
35
|
-
"User-Agent" => user_agent
|
35
|
+
"User-Agent" => user_agent,
|
36
|
+
"Authorization" => authorization }
|
36
37
|
}
|
37
38
|
}
|
38
39
|
|
@@ -42,12 +43,13 @@ module Keen
|
|
42
43
|
# deprecated, pass a hash of options instead
|
43
44
|
options = {
|
44
45
|
:project_id => args[0],
|
45
|
-
:
|
46
|
-
|
46
|
+
:write_key => args[1],
|
47
|
+
:read_key => args[2],
|
48
|
+
}.merge(args[3] || {})
|
47
49
|
end
|
48
50
|
|
49
|
-
@project_id, @
|
50
|
-
:project_id, :
|
51
|
+
@project_id, @write_key, @read_key = options.values_at(
|
52
|
+
:project_id, :write_key, :read_key)
|
51
53
|
end
|
52
54
|
|
53
55
|
private
|
@@ -76,8 +78,12 @@ module Keen
|
|
76
78
|
raise ConfigurationError, "Project ID must be set" unless self.project_id
|
77
79
|
end
|
78
80
|
|
79
|
-
def
|
80
|
-
raise ConfigurationError, "
|
81
|
+
def ensure_write_key!
|
82
|
+
raise ConfigurationError, "Write Key must be set for sending events" unless self.write_key
|
83
|
+
end
|
84
|
+
|
85
|
+
def ensure_read_key!
|
86
|
+
raise ConfigurationError, "Read Key must be set for queries" unless self.read_key
|
81
87
|
end
|
82
88
|
|
83
89
|
def method_missing(_method, *args, &block)
|
@@ -23,13 +23,14 @@ module Keen
|
|
23
23
|
# @return the JSON response from the API
|
24
24
|
def publish(event_collection, properties)
|
25
25
|
ensure_project_id!
|
26
|
+
ensure_write_key!
|
26
27
|
check_event_data!(event_collection, properties)
|
27
28
|
|
28
29
|
begin
|
29
30
|
response = Keen::HTTP::Sync.new(
|
30
31
|
api_host, api_port, api_sync_http_options).post(
|
31
32
|
:path => api_event_resource_path(event_collection),
|
32
|
-
:headers => api_headers("sync"),
|
33
|
+
:headers => api_headers(self.write_key, "sync"),
|
33
34
|
:body => MultiJson.encode(properties))
|
34
35
|
rescue Exception => http_error
|
35
36
|
raise HttpError.new("HTTP publish failure: #{http_error.message}", http_error)
|
@@ -47,16 +48,17 @@ module Keen
|
|
47
48
|
# @return a deferrable to apply callbacks to
|
48
49
|
def publish_async(event_collection, properties)
|
49
50
|
ensure_project_id!
|
51
|
+
ensure_write_key!
|
50
52
|
check_event_data!(event_collection, properties)
|
51
53
|
|
52
54
|
deferrable = EventMachine::DefaultDeferrable.new
|
53
55
|
|
54
56
|
http_client = Keen::HTTP::Async.new(api_host, api_port, api_async_http_options)
|
55
|
-
http = http_client.post(
|
57
|
+
http = http_client.post(
|
56
58
|
:path => api_event_resource_path(event_collection),
|
57
|
-
:headers => api_headers("async"),
|
59
|
+
:headers => api_headers(self.write_key, "async"),
|
58
60
|
:body => MultiJson.encode(properties)
|
59
|
-
|
61
|
+
)
|
60
62
|
|
61
63
|
if defined?(EM::Synchrony)
|
62
64
|
if http.error
|
@@ -96,7 +98,7 @@ module Keen
|
|
96
98
|
def beacon_url(event_collection, properties)
|
97
99
|
json = MultiJson.encode(properties)
|
98
100
|
data = [json].pack("m0").tr("+/", "-_").gsub("\n", "")
|
99
|
-
"https://#{api_host}#{api_event_resource_path(event_collection)}?data=#{data}"
|
101
|
+
"https://#{api_host}#{api_event_resource_path(event_collection)}?api_key=#{self.write_key}&data=#{data}"
|
100
102
|
end
|
101
103
|
|
102
104
|
private
|
@@ -1,4 +1,3 @@
|
|
1
|
-
|
2
1
|
module Keen
|
3
2
|
class Client
|
4
3
|
module QueryingMethods
|
@@ -163,9 +162,7 @@ module Keen
|
|
163
162
|
|
164
163
|
def query(query_name, event_collection, params)
|
165
164
|
ensure_project_id!
|
166
|
-
|
167
|
-
|
168
|
-
params[:api_key] = self.api_key
|
165
|
+
ensure_read_key!
|
169
166
|
|
170
167
|
if event_collection
|
171
168
|
params[:event_collection] = event_collection
|
@@ -177,7 +174,7 @@ module Keen
|
|
177
174
|
response = Keen::HTTP::Sync.new(
|
178
175
|
api_host, api_port, api_sync_http_options).get(
|
179
176
|
:path => "#{api_query_resource_path(query_name)}?#{query_params}",
|
180
|
-
:headers => api_headers("sync"))
|
177
|
+
:headers => api_headers(self.read_key, "sync"))
|
181
178
|
rescue Exception => http_error
|
182
179
|
raise HttpError.new("Couldn't perform #{query_name} on Keen IO: #{http_error.message}", http_error)
|
183
180
|
end
|
data/lib/keen/version.rb
CHANGED
@@ -2,6 +2,7 @@ require File.expand_path("../spec_helper", __FILE__)
|
|
2
2
|
|
3
3
|
describe "Keen IO API" do
|
4
4
|
let(:project_id) { ENV['KEEN_PROJECT_ID'] }
|
5
|
+
let(:write_key) { ENV['KEEN_WRITE_KEY'] }
|
5
6
|
|
6
7
|
describe "publishing" do
|
7
8
|
let(:collection) { "users" }
|
@@ -16,7 +17,7 @@ describe "Keen IO API" do
|
|
16
17
|
|
17
18
|
describe "failure" do
|
18
19
|
it "should raise a not found error if an invalid project id" do
|
19
|
-
client = Keen::Client.new(:project_id => "riker")
|
20
|
+
client = Keen::Client.new(:project_id => "riker", :write_key => "whatever")
|
20
21
|
expect {
|
21
22
|
client.publish(collection, event_properties)
|
22
23
|
}.to raise_error(Keen::NotFoundError)
|
@@ -53,7 +54,7 @@ describe "Keen IO API" do
|
|
53
54
|
end
|
54
55
|
|
55
56
|
describe "queries" do
|
56
|
-
let(:
|
57
|
+
let(:read_key) { ENV['KEEN_READ_KEY'] }
|
57
58
|
let(:event_collection) { "purchases_" + rand(100000).to_s }
|
58
59
|
let(:returns_event_collection) { "returns_" + rand(100000).to_s }
|
59
60
|
|
@@ -2,17 +2,18 @@ require File.expand_path("../../spec_helper", __FILE__)
|
|
2
2
|
|
3
3
|
describe Keen::Client::PublishingMethods do
|
4
4
|
let(:project_id) { "12345" }
|
5
|
+
let(:write_key) { "abcde" }
|
5
6
|
let(:api_host) { "api.keen.io" }
|
6
7
|
let(:collection) { "users" }
|
7
8
|
let(:event_properties) { { "name" => "Bob" } }
|
8
9
|
let(:api_success) { { "created" => true } }
|
9
|
-
let(:client) { Keen::Client.new(:project_id => project_id) }
|
10
|
+
let(:client) { Keen::Client.new(:project_id => project_id, :write_key => write_key) }
|
10
11
|
|
11
12
|
describe "publish" do
|
12
13
|
it "should post using the collection and properties" do
|
13
14
|
stub_keen_post(api_event_resource_url(collection), 201, "")
|
14
15
|
client.publish(collection, event_properties)
|
15
|
-
expect_keen_post(api_event_resource_url(collection), event_properties, "sync")
|
16
|
+
expect_keen_post(api_event_resource_url(collection), event_properties, "sync", write_key)
|
16
17
|
end
|
17
18
|
|
18
19
|
it "should return the proper response" do
|
@@ -36,7 +37,7 @@ describe Keen::Client::PublishingMethods do
|
|
36
37
|
it "should url encode the event collection" do
|
37
38
|
stub_keen_post(api_event_resource_url("foo%20bar"), 201, "")
|
38
39
|
client.publish("foo bar", event_properties)
|
39
|
-
expect_keen_post(api_event_resource_url("foo%20bar"), event_properties, "sync")
|
40
|
+
expect_keen_post(api_event_resource_url("foo%20bar"), event_properties, "sync", write_key)
|
40
41
|
end
|
41
42
|
|
42
43
|
it "should wrap exceptions" do
|
@@ -55,8 +56,18 @@ describe Keen::Client::PublishingMethods do
|
|
55
56
|
|
56
57
|
it "should raise an exception if client has no project_id" do
|
57
58
|
expect {
|
58
|
-
Keen::Client.new
|
59
|
-
|
59
|
+
Keen::Client.new(
|
60
|
+
:write_key => "abcde"
|
61
|
+
).publish(collection, event_properties)
|
62
|
+
}.to raise_error(Keen::ConfigurationError, "Keen IO Exception: Project ID must be set")
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should raise an exception if client has no write_key" do
|
66
|
+
expect {
|
67
|
+
Keen::Client.new(
|
68
|
+
:project_id => "12345"
|
69
|
+
).publish(collection, event_properties)
|
70
|
+
}.to raise_error(Keen::ConfigurationError, "Keen IO Exception: Write Key must be set for sending events")
|
60
71
|
end
|
61
72
|
end
|
62
73
|
|
@@ -74,7 +85,7 @@ describe Keen::Client::PublishingMethods do
|
|
74
85
|
EM.run {
|
75
86
|
client.publish_async(collection, event_properties).callback {
|
76
87
|
begin
|
77
|
-
expect_keen_post(api_event_resource_url(collection), event_properties, "async")
|
88
|
+
expect_keen_post(api_event_resource_url(collection), event_properties, "async", write_key)
|
78
89
|
ensure
|
79
90
|
EM.stop
|
80
91
|
end
|
@@ -87,7 +98,7 @@ describe Keen::Client::PublishingMethods do
|
|
87
98
|
EM.run {
|
88
99
|
client.publish_async("foo bar", event_properties).callback {
|
89
100
|
begin
|
90
|
-
expect_keen_post(api_event_resource_url("foo%20bar"), event_properties, "async")
|
101
|
+
expect_keen_post(api_event_resource_url("foo%20bar"), event_properties, "async", write_key)
|
91
102
|
ensure
|
92
103
|
EM.stop
|
93
104
|
end
|
@@ -168,9 +179,9 @@ describe Keen::Client::PublishingMethods do
|
|
168
179
|
|
169
180
|
describe "beacon_url" do
|
170
181
|
it "should return a url with a base-64 encoded json param" do
|
171
|
-
client = Keen::Client.new(project_id)
|
182
|
+
client = Keen::Client.new(:project_id => project_id, :write_key => write_key)
|
172
183
|
client.beacon_url("sign_ups", { :name => "Bob" }).should ==
|
173
|
-
"https://api.keen.io/3.0/projects/12345/events/sign_ups?data=eyJuYW1lIjoiQm9iIn0="
|
184
|
+
"https://api.keen.io/3.0/projects/12345/events/sign_ups?api_key=#{write_key}&data=eyJuYW1lIjoiQm9iIn0="
|
174
185
|
end
|
175
186
|
end
|
176
187
|
end
|
@@ -2,11 +2,11 @@ require File.expand_path("../../spec_helper", __FILE__)
|
|
2
2
|
|
3
3
|
describe Keen::Client do
|
4
4
|
let(:project_id) { "12345" }
|
5
|
-
let(:
|
5
|
+
let(:read_key) { "abcde" }
|
6
6
|
let(:api_host) { "api.keen.io" }
|
7
7
|
let(:api_version) { "3.0" }
|
8
8
|
let(:event_collection) { "users" }
|
9
|
-
let(:client) { Keen::Client.new(:project_id => project_id, :
|
9
|
+
let(:client) { Keen::Client.new(:project_id => project_id, :read_key => read_key) }
|
10
10
|
|
11
11
|
def query_url(query_name, query_params)
|
12
12
|
"https://#{api_host}/#{api_version}/projects/#{project_id}/queries/#{query_name}#{query_params}"
|
@@ -34,14 +34,14 @@ describe Keen::Client do
|
|
34
34
|
describe "with an improperly configured client" do
|
35
35
|
it "should require a project id" do
|
36
36
|
expect {
|
37
|
-
Keen::Client.new(:
|
38
|
-
}.to raise_error(Keen::ConfigurationError)
|
37
|
+
Keen::Client.new(:read_key => read_key).count("users", {})
|
38
|
+
}.to raise_error(Keen::ConfigurationError, "Keen IO Exception: Project ID must be set")
|
39
39
|
end
|
40
40
|
|
41
|
-
it "should require
|
41
|
+
it "should require a read key" do
|
42
42
|
expect {
|
43
43
|
Keen::Client.new(:project_id => project_id).count("users", {})
|
44
|
-
}.to raise_error(Keen::ConfigurationError)
|
44
|
+
}.to raise_error(Keen::ConfigurationError, "Keen IO Exception: Read Key must be set for queries")
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
@@ -51,13 +51,13 @@ describe Keen::Client do
|
|
51
51
|
let(:api_response) { { "result" => 1 } }
|
52
52
|
|
53
53
|
def test_query(extra_query_params="", extra_query_hash={})
|
54
|
-
expected_query_params = "?
|
54
|
+
expected_query_params = "?event_collection=#{event_collection}"
|
55
55
|
expected_query_params += extra_query_params
|
56
56
|
expected_url = query_url(query_name, expected_query_params)
|
57
57
|
stub_keen_get(expected_url, 200, :result => 1)
|
58
58
|
response = query.call(query_name, event_collection, extra_query_hash)
|
59
59
|
response.should == api_response["result"]
|
60
|
-
expect_keen_get(expected_url, "sync")
|
60
|
+
expect_keen_get(expected_url, "sync", read_key)
|
61
61
|
end
|
62
62
|
|
63
63
|
it "should call the API w/ proper headers and return the processed json response" do
|
@@ -98,35 +98,35 @@ describe Keen::Client do
|
|
98
98
|
end
|
99
99
|
|
100
100
|
it "should raise a failed responses" do
|
101
|
-
query_params = "?
|
101
|
+
query_params = "?event_collection=#{event_collection}"
|
102
102
|
url = query_url(query_name, query_params)
|
103
103
|
|
104
104
|
stub_keen_get(url, 401, :error => {})
|
105
105
|
expect {
|
106
106
|
query.call(query_name, event_collection, {})
|
107
107
|
}.to raise_error(Keen::AuthenticationError)
|
108
|
-
expect_keen_get(url, "sync")
|
108
|
+
expect_keen_get(url, "sync", read_key)
|
109
109
|
end
|
110
110
|
end
|
111
111
|
end
|
112
112
|
|
113
113
|
describe "#count" do
|
114
114
|
it "should not require params" do
|
115
|
-
query_params = "?
|
115
|
+
query_params = "?event_collection=#{event_collection}"
|
116
116
|
url = query_url("count", query_params)
|
117
117
|
stub_keen_get(url, 200, :result => 10)
|
118
118
|
client.count(event_collection).should == 10
|
119
|
-
expect_keen_get(url, "sync")
|
119
|
+
expect_keen_get(url, "sync", read_key)
|
120
120
|
end
|
121
121
|
end
|
122
122
|
|
123
123
|
describe "#extraction" do
|
124
124
|
it "should not require params" do
|
125
|
-
query_params = "?
|
125
|
+
query_params = "?event_collection=#{event_collection}"
|
126
126
|
url = query_url("extraction", query_params)
|
127
127
|
stub_keen_get(url, 200, :result => { "a" => 1 } )
|
128
128
|
client.extraction(event_collection).should == { "a" => 1 }
|
129
|
-
expect_keen_get(url, "sync")
|
129
|
+
expect_keen_get(url, "sync", read_key)
|
130
130
|
end
|
131
131
|
end
|
132
132
|
end
|
data/spec/keen/client_spec.rb
CHANGED
@@ -2,14 +2,16 @@ require File.expand_path("../spec_helper", __FILE__)
|
|
2
2
|
|
3
3
|
describe Keen::Client do
|
4
4
|
let(:project_id) { "12345" }
|
5
|
-
let(:
|
5
|
+
let(:write_key) { "abcdewrite" }
|
6
|
+
let(:read_key) { "abcderead" }
|
6
7
|
let(:client) { Keen::Client.new(:project_id => project_id) }
|
7
8
|
|
8
9
|
describe "#initialize" do
|
9
10
|
context "deprecated" do
|
10
|
-
it "should allow created via project_id and
|
11
|
-
client = Keen::Client.new(project_id,
|
12
|
-
client.
|
11
|
+
it "should allow created via project_id and key args" do
|
12
|
+
client = Keen::Client.new(project_id, write_key, read_key)
|
13
|
+
client.write_key.should == write_key
|
14
|
+
client.read_key.should == read_key
|
13
15
|
client.project_id.should == project_id
|
14
16
|
end
|
15
17
|
end
|
@@ -17,8 +19,10 @@ describe Keen::Client do
|
|
17
19
|
it "should initialize with options" do
|
18
20
|
client = Keen::Client.new(
|
19
21
|
:project_id => project_id,
|
20
|
-
:
|
21
|
-
|
22
|
+
:write_key => write_key,
|
23
|
+
:read_key => read_key)
|
24
|
+
client.write_key.should == write_key
|
25
|
+
client.read_key.should == read_key
|
22
26
|
client.project_id.should == project_id
|
23
27
|
end
|
24
28
|
end
|
data/spec/keen/keen_spec.rb
CHANGED
@@ -6,7 +6,8 @@ describe Keen do
|
|
6
6
|
before do
|
7
7
|
Keen.instance_variable_set(:@default_client, nil)
|
8
8
|
ENV["KEEN_PROJECT_ID"] = "12345"
|
9
|
-
ENV["
|
9
|
+
ENV["KEEN_WRITE_KEY"] = "abcdewrite"
|
10
|
+
ENV["KEEN_READ_KEY"] = "abcderead"
|
10
11
|
end
|
11
12
|
|
12
13
|
let(:client) { Keen.send(:default_client) }
|
@@ -15,8 +16,12 @@ describe Keen do
|
|
15
16
|
client.project_id.should == "12345"
|
16
17
|
end
|
17
18
|
|
18
|
-
it "should set
|
19
|
-
client.
|
19
|
+
it "should set a write key from the environment" do
|
20
|
+
client.write_key.should == "abcdewrite"
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should set a read key from the environment" do
|
24
|
+
client.read_key.should == "abcderead"
|
20
25
|
end
|
21
26
|
end
|
22
27
|
end
|
@@ -38,14 +43,14 @@ describe Keen do
|
|
38
43
|
Keen.stub(:default_client).and_return(@default_client)
|
39
44
|
end
|
40
45
|
|
41
|
-
[:project_id, :
|
46
|
+
[:project_id, :write_key, :read_key].each do |_method|
|
42
47
|
it "should forward the #{_method} method" do
|
43
48
|
@default_client.should_receive(_method)
|
44
49
|
Keen.send(_method)
|
45
50
|
end
|
46
51
|
end
|
47
52
|
|
48
|
-
[:project_id=, :
|
53
|
+
[:project_id=, :write_key=, :read_key=].each do |_method|
|
49
54
|
it "should forward the #{_method} method" do
|
50
55
|
@default_client.should_receive(_method).with("12345")
|
51
56
|
Keen.send(_method, "12345")
|
data/spec/spec_helper.rb
CHANGED
@@ -23,26 +23,29 @@ module Keen::SpecHelpers
|
|
23
23
|
stub_keen_request(:get, url, status, MultiJson.encode(response_body))
|
24
24
|
end
|
25
25
|
|
26
|
-
def expect_keen_request(method, url, body, sync_or_async_ua)
|
26
|
+
def expect_keen_request(method, url, body, sync_or_async_ua, read_or_write_key)
|
27
27
|
user_agent = "keen-gem, v#{Keen::VERSION}, #{sync_or_async_ua}"
|
28
28
|
user_agent += ", #{RUBY_VERSION}, #{RUBY_PLATFORM}, #{RUBY_PATCHLEVEL}"
|
29
29
|
if defined?(RUBY_ENGINE)
|
30
30
|
user_agent += ", #{RUBY_ENGINE}"
|
31
31
|
end
|
32
32
|
|
33
|
+
headers = { "Content-Type" => "application/json",
|
34
|
+
"User-Agent" => user_agent,
|
35
|
+
"Authorization" => read_or_write_key }
|
36
|
+
|
33
37
|
WebMock.should have_requested(method, url).with(
|
34
38
|
:body => body,
|
35
|
-
:headers =>
|
36
|
-
"User-Agent" => user_agent })
|
39
|
+
:headers => headers)
|
37
40
|
|
38
41
|
end
|
39
42
|
|
40
|
-
def expect_keen_get(url, sync_or_async_ua)
|
41
|
-
expect_keen_request(:get, url, "", sync_or_async_ua)
|
43
|
+
def expect_keen_get(url, sync_or_async_ua, read_key)
|
44
|
+
expect_keen_request(:get, url, "", sync_or_async_ua, read_key)
|
42
45
|
end
|
43
46
|
|
44
|
-
def expect_keen_post(url, event_properties, sync_or_async_ua)
|
45
|
-
expect_keen_request(:post, url, MultiJson.encode(event_properties), sync_or_async_ua)
|
47
|
+
def expect_keen_post(url, event_properties, sync_or_async_ua, write_key)
|
48
|
+
expect_keen_request(:post, url, MultiJson.encode(event_properties), sync_or_async_ua, write_key)
|
46
49
|
end
|
47
50
|
|
48
51
|
def api_event_resource_url(collection)
|
@@ -2,13 +2,14 @@ require File.expand_path("../spec_helper", __FILE__)
|
|
2
2
|
|
3
3
|
describe Keen::HTTP::Async do
|
4
4
|
let(:project_id) { "12345" }
|
5
|
+
let(:write_key) { "abcdewrite" }
|
5
6
|
let(:collection) { "users" }
|
6
7
|
let(:event_properties) { { "name" => "Bob" } }
|
7
8
|
let(:api_success) { { "created" => true } }
|
8
9
|
|
9
10
|
describe "synchrony" do
|
10
11
|
before do
|
11
|
-
@client = Keen::Client.new(:project_id => project_id)
|
12
|
+
@client = Keen::Client.new(:project_id => project_id, :write_key => write_key)
|
12
13
|
end
|
13
14
|
|
14
15
|
describe "success" do
|
@@ -16,7 +17,7 @@ describe Keen::HTTP::Async do
|
|
16
17
|
stub_keen_post(api_event_resource_url(collection), 201, api_success)
|
17
18
|
EM.synchrony {
|
18
19
|
@client.publish_async(collection, event_properties)
|
19
|
-
expect_keen_post(api_event_resource_url(collection), event_properties, "async")
|
20
|
+
expect_keen_post(api_event_resource_url(collection), event_properties, "async", write_key)
|
20
21
|
EM.stop
|
21
22
|
}
|
22
23
|
end
|
metadata
CHANGED
@@ -1,16 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: keen
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Kyle Wild
|
9
9
|
- Josh Dzielak
|
10
|
+
- Daniel Kador
|
10
11
|
autorequire:
|
11
12
|
bindir: bin
|
12
13
|
cert_chain: []
|
13
|
-
date: 2013-
|
14
|
+
date: 2013-05-01 00:00:00.000000000 Z
|
14
15
|
dependencies:
|
15
16
|
- !ruby/object:Gem::Dependency
|
16
17
|
name: multi_json
|