keen 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +1 -0
- data/README.md +61 -11
- data/lib/keen.rb +2 -1
- data/lib/keen/client.rb +10 -77
- data/lib/keen/client/publishing_methods.rb +111 -0
- data/lib/keen/client/querying_methods.rb +199 -0
- data/lib/keen/http.rb +6 -0
- data/lib/keen/version.rb +1 -1
- data/spec/integration/api_spec.rb +112 -34
- data/spec/keen/client/publishing_methods_spec.rb +161 -0
- data/spec/keen/client/querying_methods_spec.rb +132 -0
- data/spec/keen/client_spec.rb +22 -188
- data/spec/spec_helper.rb +23 -8
- data/spec/synchrony/synchrony_spec.rb +4 -4
- metadata +17 -7
- checksums.yaml +0 -7
data/spec/keen/client_spec.rb
CHANGED
@@ -3,9 +3,7 @@ require File.expand_path("../spec_helper", __FILE__)
|
|
3
3
|
describe Keen::Client do
|
4
4
|
let(:project_id) { "12345" }
|
5
5
|
let(:api_key) { "abcde" }
|
6
|
-
let(:
|
7
|
-
let(:event_properties) { { "name" => "Bob" } }
|
8
|
-
let(:api_success) { { "created" => true } }
|
6
|
+
let(:client) { Keen::Client.new(:project_id => project_id) }
|
9
7
|
|
10
8
|
describe "#initialize" do
|
11
9
|
context "deprecated" do
|
@@ -25,200 +23,36 @@ describe Keen::Client do
|
|
25
23
|
end
|
26
24
|
end
|
27
25
|
|
28
|
-
describe "
|
29
|
-
|
30
|
-
|
31
|
-
it "should raise an exception if no project_id" do
|
32
|
-
expect {
|
33
|
-
Keen::Client.new(:api_key => api_key).
|
34
|
-
send(_method, collection, event_properties)
|
35
|
-
}.to raise_error(Keen::ConfigurationError)
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
26
|
+
describe "process_response" do
|
27
|
+
let (:body) { "{ \"wazzup\": 1 }" }
|
28
|
+
let (:process_response) { client.method(:process_response) }
|
40
29
|
|
41
|
-
|
42
|
-
|
43
|
-
@client = Keen::Client.new(:project_id => project_id)
|
30
|
+
it "should raise a bad request error for a 400" do
|
31
|
+
process_response.call(200, body).should == { "wazzup" => 1 }
|
44
32
|
end
|
45
33
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
expect_post(api_url(collection), event_properties, "sync")
|
51
|
-
end
|
52
|
-
|
53
|
-
it "should return the proper response" do
|
54
|
-
api_response = { "created" => true }
|
55
|
-
stub_api(api_url(collection), 201, api_response)
|
56
|
-
@client.publish(collection, event_properties).should == api_response
|
57
|
-
end
|
58
|
-
|
59
|
-
it "should raise an argument error if no event collection is specified" do
|
60
|
-
expect {
|
61
|
-
@client.publish(nil, {})
|
62
|
-
}.to raise_error(ArgumentError)
|
63
|
-
end
|
64
|
-
|
65
|
-
it "should raise an argument error if no properties are specified" do
|
66
|
-
expect {
|
67
|
-
@client.publish(collection, nil)
|
68
|
-
}.to raise_error(ArgumentError)
|
69
|
-
end
|
70
|
-
|
71
|
-
it "should url encode the event collection" do
|
72
|
-
stub_api(api_url("foo%20bar"), 201, "")
|
73
|
-
@client.publish("foo bar", event_properties)
|
74
|
-
expect_post(api_url("foo%20bar"), event_properties, "sync")
|
75
|
-
end
|
76
|
-
|
77
|
-
it "should wrap exceptions" do
|
78
|
-
stub_request(:post, api_url(collection)).to_timeout
|
79
|
-
e = nil
|
80
|
-
begin
|
81
|
-
@client.publish(collection, event_properties)
|
82
|
-
rescue Exception => exception
|
83
|
-
e = exception
|
84
|
-
end
|
85
|
-
|
86
|
-
e.class.should == Keen::HttpError
|
87
|
-
e.original_error.class.should == Timeout::Error
|
88
|
-
e.message.should == "Couldn't connect to Keen IO: execution expired"
|
89
|
-
end
|
34
|
+
it "should raise a bad request error for a 400" do
|
35
|
+
expect {
|
36
|
+
process_response.call(400, body)
|
37
|
+
}.to raise_error(Keen::BadRequestError)
|
90
38
|
end
|
91
39
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
it "should require a running event loop" do
|
97
|
-
expect {
|
98
|
-
@client.publish_async(collection, event_properties)
|
99
|
-
}.to raise_error(Keen::Error)
|
100
|
-
end
|
101
|
-
|
102
|
-
it "should post the event data" do
|
103
|
-
stub_api(api_url(collection), 201, api_success)
|
104
|
-
EM.run {
|
105
|
-
@client.publish_async(collection, event_properties).callback {
|
106
|
-
begin
|
107
|
-
expect_post(api_url(collection), event_properties, "async")
|
108
|
-
ensure
|
109
|
-
EM.stop
|
110
|
-
end
|
111
|
-
}
|
112
|
-
}
|
113
|
-
end
|
114
|
-
|
115
|
-
it "should uri encode the event collection" do
|
116
|
-
stub_api(api_url("foo%20bar"), 201, api_success)
|
117
|
-
EM.run {
|
118
|
-
@client.publish_async("foo bar", event_properties).callback {
|
119
|
-
begin
|
120
|
-
expect_post(api_url("foo%20bar"), event_properties, "async")
|
121
|
-
ensure
|
122
|
-
EM.stop
|
123
|
-
end
|
124
|
-
}
|
125
|
-
}
|
126
|
-
end
|
127
|
-
|
128
|
-
it "should raise an argument error if no event collection is specified" do
|
129
|
-
expect {
|
130
|
-
@client.publish_async(nil, {})
|
131
|
-
}.to raise_error(ArgumentError)
|
132
|
-
end
|
133
|
-
|
134
|
-
it "should raise an argument error if no properties are specified" do
|
135
|
-
expect {
|
136
|
-
@client.publish_async(collection, nil)
|
137
|
-
}.to raise_error(ArgumentError)
|
138
|
-
end
|
139
|
-
|
140
|
-
describe "deferrable callbacks" do
|
141
|
-
it "should trigger callbacks" do
|
142
|
-
stub_api(api_url(collection), 201, api_success)
|
143
|
-
EM.run {
|
144
|
-
@client.publish_async(collection, event_properties).callback { |response|
|
145
|
-
begin
|
146
|
-
response.should == api_success
|
147
|
-
ensure
|
148
|
-
EM.stop
|
149
|
-
end
|
150
|
-
}
|
151
|
-
}
|
152
|
-
end
|
153
|
-
|
154
|
-
it "should trigger errbacks" do
|
155
|
-
stub_request(:post, api_url(collection)).to_timeout
|
156
|
-
EM.run {
|
157
|
-
@client.publish_async(collection, event_properties).errback { |error|
|
158
|
-
begin
|
159
|
-
error.should_not be_nil
|
160
|
-
error.message.should == "Couldn't connect to Keen IO: WebMock timeout error"
|
161
|
-
ensure
|
162
|
-
EM.stop
|
163
|
-
end
|
164
|
-
}
|
165
|
-
}
|
166
|
-
end
|
167
|
-
end
|
168
|
-
end
|
169
|
-
|
40
|
+
it "should raise a authentication error for a 401" do
|
41
|
+
expect {
|
42
|
+
process_response.call(401, body)
|
43
|
+
}.to raise_error(Keen::AuthenticationError)
|
170
44
|
end
|
171
45
|
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
end
|
177
|
-
|
178
|
-
it "should return the json body for a 200-201" do
|
179
|
-
api_response = { "created" => "true" }
|
180
|
-
stub_status_and_publish(200, api_response).should == api_response
|
181
|
-
stub_status_and_publish(201, api_response).should == api_response
|
182
|
-
end
|
183
|
-
|
184
|
-
it "should raise a bad request error for a 400" do
|
185
|
-
expect {
|
186
|
-
stub_status_and_publish(400)
|
187
|
-
}.to raise_error(Keen::BadRequestError)
|
188
|
-
end
|
189
|
-
|
190
|
-
it "should raise a authentication error for a 401" do
|
191
|
-
expect {
|
192
|
-
stub_status_and_publish(401)
|
193
|
-
}.to raise_error(Keen::AuthenticationError)
|
194
|
-
end
|
195
|
-
|
196
|
-
it "should raise a not found error for a 404" do
|
197
|
-
expect {
|
198
|
-
stub_status_and_publish(404)
|
199
|
-
}.to raise_error(Keen::NotFoundError)
|
200
|
-
end
|
201
|
-
|
202
|
-
it "should raise an http error otherwise" do
|
203
|
-
expect {
|
204
|
-
stub_status_and_publish(420)
|
205
|
-
}.to raise_error(Keen::HttpError)
|
206
|
-
end
|
46
|
+
it "should raise a not found error for a 404" do
|
47
|
+
expect {
|
48
|
+
process_response.call(404, body)
|
49
|
+
}.to raise_error(Keen::NotFoundError)
|
207
50
|
end
|
208
51
|
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
end
|
214
|
-
end
|
215
|
-
end
|
216
|
-
|
217
|
-
describe "beacon_url" do
|
218
|
-
it "should return a url with a base-64 encoded json param" do
|
219
|
-
client = Keen::Client.new(project_id)
|
220
|
-
client.beacon_url("sign_ups", { :name => "Bob" }).should ==
|
221
|
-
"https://api.keen.io/3.0/projects/12345/events/sign_ups?data=eyJuYW1lIjoiQm9iIn0="
|
52
|
+
it "should raise an http error otherwise" do
|
53
|
+
expect {
|
54
|
+
process_response.call(420, body)
|
55
|
+
}.to raise_error(Keen::HttpError)
|
222
56
|
end
|
223
57
|
end
|
224
58
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -11,26 +11,41 @@ require 'em-http'
|
|
11
11
|
require File.expand_path("../../lib/keen", __FILE__)
|
12
12
|
|
13
13
|
module Keen::SpecHelpers
|
14
|
-
def
|
15
|
-
stub_request(
|
16
|
-
:status => status,
|
17
|
-
:body => MultiJson.encode(json_body))
|
14
|
+
def stub_keen_request(method, url, status, response_body)
|
15
|
+
stub_request(method, url).to_return(:status => status, :body => response_body)
|
18
16
|
end
|
19
17
|
|
20
|
-
def
|
18
|
+
def stub_keen_post(url, status, response_body)
|
19
|
+
stub_keen_request(:post, url, status, MultiJson.encode(response_body))
|
20
|
+
end
|
21
|
+
|
22
|
+
def stub_keen_get(url, status, response_body)
|
23
|
+
stub_keen_request(:get, url, status, MultiJson.encode(response_body))
|
24
|
+
end
|
25
|
+
|
26
|
+
def expect_keen_request(method, url, body, sync_or_async_ua)
|
21
27
|
user_agent = "keen-gem, v#{Keen::VERSION}, #{sync_or_async_ua}"
|
22
28
|
user_agent += ", #{RUBY_VERSION}, #{RUBY_PLATFORM}, #{RUBY_PATCHLEVEL}"
|
23
29
|
if defined?(RUBY_ENGINE)
|
24
30
|
user_agent += ", #{RUBY_ENGINE}"
|
25
31
|
end
|
26
32
|
|
27
|
-
WebMock.should have_requested(
|
28
|
-
:body =>
|
33
|
+
WebMock.should have_requested(method, url).with(
|
34
|
+
:body => body,
|
29
35
|
:headers => { "Content-Type" => "application/json",
|
30
36
|
"User-Agent" => user_agent })
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
def expect_keen_get(url, sync_or_async_ua)
|
41
|
+
expect_keen_request(:get, url, "", sync_or_async_ua)
|
42
|
+
end
|
43
|
+
|
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)
|
31
46
|
end
|
32
47
|
|
33
|
-
def
|
48
|
+
def api_event_resource_url(collection)
|
34
49
|
"https://api.keen.io/3.0/projects/#{project_id}/events/#{collection}"
|
35
50
|
end
|
36
51
|
end
|
@@ -13,16 +13,16 @@ describe Keen::HTTP::Async do
|
|
13
13
|
|
14
14
|
describe "success" do
|
15
15
|
it "should post the event data" do
|
16
|
-
|
16
|
+
stub_keen_post(api_event_resource_url(collection), 201, api_success)
|
17
17
|
EM.synchrony {
|
18
18
|
@client.publish_async(collection, event_properties)
|
19
|
-
|
19
|
+
expect_keen_post(api_event_resource_url(collection), event_properties, "async")
|
20
20
|
EM.stop
|
21
21
|
}
|
22
22
|
end
|
23
23
|
|
24
24
|
it "should recieve the right response 'synchronously'" do
|
25
|
-
|
25
|
+
stub_keen_post(api_event_resource_url(collection), 201, api_success)
|
26
26
|
EM.synchrony {
|
27
27
|
@client.publish_async(collection, event_properties).should == api_success
|
28
28
|
EM.stop
|
@@ -32,7 +32,7 @@ describe Keen::HTTP::Async do
|
|
32
32
|
|
33
33
|
describe "failure" do
|
34
34
|
it "should raise an exception" do
|
35
|
-
stub_request(:post,
|
35
|
+
stub_request(:post, api_event_resource_url(collection)).to_timeout
|
36
36
|
e = nil
|
37
37
|
EM.synchrony {
|
38
38
|
begin
|
metadata
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: keen
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
|
+
prerelease:
|
5
6
|
platform: ruby
|
6
7
|
authors:
|
7
8
|
- Kyle Wild
|
@@ -9,11 +10,12 @@ authors:
|
|
9
10
|
autorequire:
|
10
11
|
bindir: bin
|
11
12
|
cert_chain: []
|
12
|
-
date: 2013-
|
13
|
+
date: 2013-04-05 00:00:00.000000000 Z
|
13
14
|
dependencies:
|
14
15
|
- !ruby/object:Gem::Dependency
|
15
16
|
name: multi_json
|
16
17
|
requirement: !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
17
19
|
requirements:
|
18
20
|
- - ~>
|
19
21
|
- !ruby/object:Gem::Version
|
@@ -21,6 +23,7 @@ dependencies:
|
|
21
23
|
type: :runtime
|
22
24
|
prerelease: false
|
23
25
|
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
24
27
|
requirements:
|
25
28
|
- - ~>
|
26
29
|
- !ruby/object:Gem::Version
|
@@ -43,10 +46,14 @@ files:
|
|
43
46
|
- keen.gemspec
|
44
47
|
- lib/keen.rb
|
45
48
|
- lib/keen/client.rb
|
49
|
+
- lib/keen/client/publishing_methods.rb
|
50
|
+
- lib/keen/client/querying_methods.rb
|
46
51
|
- lib/keen/http.rb
|
47
52
|
- lib/keen/version.rb
|
48
53
|
- spec/integration/api_spec.rb
|
49
54
|
- spec/integration/spec_helper.rb
|
55
|
+
- spec/keen/client/publishing_methods_spec.rb
|
56
|
+
- spec/keen/client/querying_methods_spec.rb
|
50
57
|
- spec/keen/client_spec.rb
|
51
58
|
- spec/keen/keen_spec.rb
|
52
59
|
- spec/keen/spec_helper.rb
|
@@ -55,30 +62,33 @@ files:
|
|
55
62
|
- spec/synchrony/synchrony_spec.rb
|
56
63
|
homepage: https://github.com/keenlabs/keen-gem
|
57
64
|
licenses: []
|
58
|
-
metadata: {}
|
59
65
|
post_install_message:
|
60
66
|
rdoc_options: []
|
61
67
|
require_paths:
|
62
68
|
- lib
|
63
69
|
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
64
71
|
requirements:
|
65
|
-
- - '>='
|
72
|
+
- - ! '>='
|
66
73
|
- !ruby/object:Gem::Version
|
67
74
|
version: '0'
|
68
75
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
69
77
|
requirements:
|
70
|
-
- - '>='
|
78
|
+
- - ! '>='
|
71
79
|
- !ruby/object:Gem::Version
|
72
80
|
version: '0'
|
73
81
|
requirements: []
|
74
82
|
rubyforge_project:
|
75
|
-
rubygems_version:
|
83
|
+
rubygems_version: 1.8.23
|
76
84
|
signing_key:
|
77
|
-
specification_version:
|
85
|
+
specification_version: 3
|
78
86
|
summary: Keen IO API Client
|
79
87
|
test_files:
|
80
88
|
- spec/integration/api_spec.rb
|
81
89
|
- spec/integration/spec_helper.rb
|
90
|
+
- spec/keen/client/publishing_methods_spec.rb
|
91
|
+
- spec/keen/client/querying_methods_spec.rb
|
82
92
|
- spec/keen/client_spec.rb
|
83
93
|
- spec/keen/keen_spec.rb
|
84
94
|
- spec/keen/spec_helper.rb
|
checksums.yaml
DELETED
@@ -1,7 +0,0 @@
|
|
1
|
-
---
|
2
|
-
SHA1:
|
3
|
-
metadata.gz: b5661cff5eaa3017d59716166dc7b265519b95c9
|
4
|
-
data.tar.gz: 4644e32d886566a16b91bf3693bee6de1ba46c65
|
5
|
-
SHA512:
|
6
|
-
metadata.gz: 7eeb6df1b69794c412f914a770da11addb9cbbddef4dfc17cab0cf96a27b44a9856c9eec5d654464c8f43d2c697510ba6386d16e203e954abb24bc81d0fc2f6f
|
7
|
-
data.tar.gz: 14e3bbf44cdc20227605e4452de88e4097f7d9081ab262ecd5fc5a2ce5c2d3072ead63ed1f25994953736eb0f74b0911f3076529ee46c1260223b0e7bdd962f9
|