replay_api 0.0.3 → 0.0.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dabf42fd3f06393835e7a03d3b9be1b762e802c7
4
- data.tar.gz: 21184818aca27ea627132bcc761b92ee5dc161d2
3
+ metadata.gz: 5eb0597cd56bcc5bf50df1247ae3b7b053096103
4
+ data.tar.gz: 2e6cc14f21dd67133ba86076d246d1bdc13744fa
5
5
  SHA512:
6
- metadata.gz: 4d2a0096dbf2ab8507019b66d9cceed9189b823f0a3cbd0ec4cc66ae358083a9887b70ea94a75be3ff2669f02795cfb01a21f56bee342cb3214343c8e43423b9
7
- data.tar.gz: 2744e9eb161280b6aa859a8cd20e07593a83d7f4e8c4dd7ba14378b7cd3e986737686f767abb2ddb23246b68a255d646cab935fd6023aeab38366a2c08cc1718
6
+ metadata.gz: 7a9464458585d09249ab5b19132453a113fbda3e5ea1c731cf6f6b0bd91b0fbafdf090041b6ec3414515c4b68e050a62df64bb937f3fe99397331240d432ec6d
7
+ data.tar.gz: 4cdbdae7dcd9b072ccd8c8d2f798c25fe4d5fa921ef6636d2acd402fba182ab45ba108d80836e2f442afdc19cd5107993b431e909264b2b32bdf0d66fe6e941c
data/lib/replay_api.rb CHANGED
@@ -8,6 +8,7 @@ require 'replay_api/configuration'
8
8
  require 'replay_api/event'
9
9
  require 'replay_api/trait'
10
10
  require 'replay_api/client'
11
+ require 'replay_api/request_deliverer'
11
12
 
12
13
  module ReplayApi
13
14
  end
@@ -6,37 +6,25 @@ require 'typhoeus/adapters/faraday'
6
6
  module ReplayApi
7
7
  class Client
8
8
  attr_reader :configuration
9
- attr_writer :connection
9
+ attr_accessor :deliverer
10
10
 
11
- def initialize(configuration=ReplayApi.configuration)
11
+ def initialize(configuration=ReplayApi.configuration, deliverer=ReplayApi::RequestDeliverer.instance)
12
12
  @configuration ||= configuration
13
+ @deliverer ||= deliverer
13
14
  end
14
15
 
15
16
  def event(event=Event.new)
16
17
  yield event if block_given?
17
- connection.post '/events', payload(event.compact_attributes)
18
+ deliverer.post(uri: '/events', payload: payload(event.compact_attributes))
18
19
  end
19
20
 
20
21
  def trait(trait=Trait.new)
21
22
  yield trait if block_given?
22
- connection.post '/traits', payload(trait.compact_attributes)
23
+ deliverer.post(uri: '/traits', payload: payload(trait.compact_attributes))
23
24
  end
24
25
 
25
26
  private
26
27
 
27
- def connection
28
- @connection ||= Faraday.new(url: "#{protocol}://#{configuration.replay_server}") do |conn|
29
- conn.request :json
30
- conn.response :json
31
- conn.adapter :typhoeus
32
- end
33
- end
34
-
35
- def protocol
36
- return 'http' unless configuration.ssl?
37
- 'https'
38
- end
39
-
40
28
  def payload(data)
41
29
  { replay_key: configuration.replay_key }.merge data
42
30
  end
@@ -12,10 +12,14 @@ module ReplayApi
12
12
  attr_accessor :replay_key
13
13
  attr_accessor :replay_server
14
14
  attr_accessor :ssl
15
+ attr_accessor :max_request_batch_size
16
+ attr_accessor :max_request_batch_delay
15
17
 
16
18
  def initialize
17
19
  @replay_server = 'api.replay.io'
18
20
  @ssl = true
21
+ @max_request_batch_size = 100
22
+ @max_request_batch_delay = 1
19
23
  end
20
24
 
21
25
  alias_method :ssl?, :ssl
@@ -20,14 +20,6 @@ module ReplayApi
20
20
  attribute :page_name, String
21
21
  attribute :page_url, String
22
22
  attribute :past_event, Integer, default: 0
23
- attribute :timestamp, Integer, default: -> (_, _) { clock.now.to_i }
24
-
25
- def self.clock=(val)
26
- @clock = val
27
- end
28
-
29
- def self.clock
30
- @clock ||= Time
31
- end
23
+
32
24
  end
33
25
  end
@@ -0,0 +1,76 @@
1
+ module ReplayApi
2
+ class RequestDeliverer
3
+ include Singleton
4
+
5
+ attr_accessor :configuration
6
+ attr_accessor :connection
7
+ attr_reader :request_buffer
8
+ attr_reader :time_last_write
9
+ attr_reader :delivery_thread
10
+
11
+ def initialize(configuration=ReplayApi.configuration)
12
+ @configuration ||= configuration
13
+ @time_last_write = Time.now
14
+ @request_buffer = Queue.new
15
+
16
+ start_delivery_thread
17
+ end
18
+
19
+ def post(request)
20
+ @request_buffer.push(request)
21
+ end
22
+
23
+ private
24
+
25
+ def start_delivery_thread
26
+ return if @delivery_thread
27
+
28
+ @delivery_thread = Thread.new do
29
+ loop do
30
+ sendRequests(inboundRequests)
31
+ @time_last_write = Time.now
32
+ end
33
+ end
34
+ end
35
+
36
+ def inboundRequests
37
+ requests = [request_buffer.pop]
38
+ requests << request_buffer.pop until (request_buffer.empty? or batch_full?(requests) or max_delay_exceeded?)
39
+ requests
40
+ end
41
+
42
+ def sendRequests(requests)
43
+ connection.in_parallel do
44
+ requests.each do |request|
45
+ connection.post request[:uri], request[:payload]
46
+ end
47
+ end
48
+ end
49
+
50
+ def batch_full?(requests)
51
+ requests.size >= configuration.max_request_batch_size
52
+ end
53
+
54
+ def max_delay_exceeded?
55
+ time_since_last_write > configuration.max_request_batch_delay
56
+ end
57
+
58
+
59
+ def time_since_last_write
60
+ Time.now - time_last_write
61
+ end
62
+
63
+ def connection
64
+ @connection ||= Faraday.new(url: "#{protocol}://#{configuration.replay_server}") do |conn|
65
+ conn.request :json
66
+ conn.response :json
67
+ conn.adapter :typhoeus
68
+ end
69
+ end
70
+
71
+ def protocol
72
+ return 'http' unless configuration.ssl?
73
+ 'https'
74
+ end
75
+ end
76
+ end
@@ -21,16 +21,8 @@ module ReplayApi
21
21
  attribute :page_url, String
22
22
  attribute :past_event, Integer, default: 0
23
23
  attribute :phone, String
24
- attribute :timestamp, Integer, default: -> (_, _) { clock.now }
25
24
  attribute :title, String
26
25
  attribute :username, String
27
26
 
28
- def self.clock=(val)
29
- @clock = val
30
- end
31
-
32
- def self.clock
33
- @clock ||= Time
34
- end
35
27
  end
36
28
  end
@@ -1,3 +1,3 @@
1
1
  module ReplayApi
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.5'
3
3
  end
data/replay_api.gemspec CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ['support@replay.io']
11
11
  spec.summary = %q{Ruby SDK for Replay.io API}
12
12
  spec.description = %q{Ruby SDK for Replay.io API}
13
- spec.homepage = 'https://github.com/Originate/replay-gem'
13
+ spec.homepage = 'https://github.com/Originate/replay-ruby'
14
14
  spec.license = 'MIT'
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0")
@@ -2,12 +2,9 @@ require 'spec_helper'
2
2
 
3
3
  module ReplayApi
4
4
  describe Client do
5
- subject(:client) { Client.new(configuration) }
6
- let(:connection) { double :connection, post: nil }
5
+ subject(:client) { Client.new(configuration, deliverer) }
6
+ let(:deliverer) { double :deliverer, post: nil }
7
7
  let(:configuration) { Configuration.new.tap { |c| c.replay_key = 'my_key' } }
8
- before do
9
- client.connection = connection
10
- end
11
8
 
12
9
  describe '#event' do
13
10
  let(:event) { Event.new }
@@ -19,7 +16,7 @@ module ReplayApi
19
16
 
20
17
  it 'posts the event' do
21
18
  client.event(event)
22
- expect(connection).to have_received(:post).with('/events', payload)
19
+ expect(deliverer).to have_received(:post).with(uri: '/events', payload: payload)
23
20
  end
24
21
  end
25
22
 
@@ -33,7 +30,7 @@ module ReplayApi
33
30
 
34
31
  it 'posts the trait' do
35
32
  client.trait(trait)
36
- expect(connection).to have_received(:post).with('/traits', payload)
33
+ expect(deliverer).to have_received(:post).with(uri: '/traits', payload: payload)
37
34
  end
38
35
  end
39
36
  end
@@ -19,7 +19,7 @@ module ReplayApi
19
19
  { attribute1: :value1, attribute2: { attribute3: :value3 } } => { attribute1: :value1, attribute2: { attribute3: :value3 } },
20
20
  { attribute1: :value1, attribute2: { attribute3: nil } } => { attribute1: :value1 },
21
21
  { attribute1: :value1, attribute2: { attribute3: :value3, attribute4: nil } } => { attribute1: :value1, attribute2: { attribute3: :value3 } },
22
- { attribute1: :value1, attribute2: Event.new(event_name: 'Test Event', properties: { timestamp: 12345 }) } => { attribute1: :value1, attribute2: { event_name: 'Test Event', properties: { event_category: 'general', past_event: 0, timestamp: 12345 } } },
22
+ { attribute1: :value1, attribute2: Event.new(event_name: 'Test Event', properties: { }) } => { attribute1: :value1, attribute2: { event_name: 'Test Event', properties: { event_category: 'general', past_event: 0 } } },
23
23
  }
24
24
 
25
25
  test_cases.each do |input, output|
@@ -3,9 +3,6 @@ require 'spec_helper'
3
3
  module ReplayApi
4
4
  describe EventProperties do
5
5
  subject(:properties) { EventProperties.new }
6
- let(:clock) { double :clock, now: Time.now }
7
- before { EventProperties.clock = clock }
8
- after { EventProperties.clock = nil }
9
6
 
10
7
  describe '#event_category' do
11
8
  specify { expect(properties.event_category).to eq 'general' }
@@ -14,9 +11,5 @@ module ReplayApi
14
11
  describe '#past_event' do
15
12
  specify { expect(properties.past_event).to eq 0 }
16
13
  end
17
-
18
- describe '#timestamp' do
19
- specify { expect(properties.timestamp).to eq clock.now.to_i }
20
- end
21
14
  end
22
15
  end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ module ReplayApi
4
+ describe RequestDeliverer do
5
+ subject(:request_deliverer) { RequestDeliverer.instance }
6
+ let(:connection) { double :connection, post: nil }
7
+ let(:configuration) { Configuration.new.tap { |c| c.replay_key = 'my_key' } }
8
+
9
+ before do
10
+ allow(connection).to receive(:in_parallel).and_yield
11
+ request_deliverer.configuration = configuration
12
+ request_deliverer.connection = connection
13
+ end
14
+
15
+ describe '#post' do
16
+ let(:event) { Event.new }
17
+ let(:payload) { { replay_key: 'my_key' }.merge event.compact_attributes }
18
+
19
+ it 'sends the request' do
20
+ request_deliverer.post(uri: '/events', payload: payload)
21
+ {} until request_deliverer.delivery_thread.status == "sleep"
22
+ expect(connection).to have_received(:post).with('/events', payload)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -3,10 +3,7 @@ require 'spec_helper'
3
3
  module ReplayApi
4
4
  describe TraitProperties do
5
5
  subject(:properties) { TraitProperties.new }
6
- let(:clock) { double :clock, now: Time.now }
7
- before { TraitProperties.clock = clock }
8
- after { TraitProperties.clock = nil }
9
-
6
+
10
7
  describe '#address' do
11
8
  specify { expect(properties.address).to be_a(ReplayApi::Address) }
12
9
  end
@@ -14,9 +11,5 @@ module ReplayApi
14
11
  describe '#past_event' do
15
12
  specify { expect(properties.past_event).to eq 0 }
16
13
  end
17
-
18
- describe '#timestamp' do
19
- specify { expect(properties.timestamp).to eq clock.now.to_i }
20
- end
21
14
  end
22
15
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: replay_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Originate
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-26 00:00:00.000000000 Z
11
+ date: 2015-03-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -131,21 +131,22 @@ files:
131
131
  - lib/replay_api/event_properties.rb
132
132
  - lib/replay_api/event_properties_extensions.rb
133
133
  - lib/replay_api/model.rb
134
+ - lib/replay_api/request_deliverer.rb
134
135
  - lib/replay_api/trait.rb
135
136
  - lib/replay_api/trait_properties.rb
136
137
  - lib/replay_api/trait_properties_extensions.rb
137
138
  - lib/replay_api/version.rb
138
139
  - replay_api.gemspec
139
- - spec/feature/client_spec.rb
140
140
  - spec/replay_api/client_spec.rb
141
141
  - spec/replay_api/compact_attributes_spec.rb
142
142
  - spec/replay_api/configuration_spec.rb
143
143
  - spec/replay_api/event_properties_spec.rb
144
144
  - spec/replay_api/event_spec.rb
145
+ - spec/replay_api/request_deliverer_spec.rb
145
146
  - spec/replay_api/trait_properties_spec.rb
146
147
  - spec/replay_api/trait_spec.rb
147
148
  - spec/spec_helper.rb
148
- homepage: https://github.com/Originate/replay-gem
149
+ homepage: https://github.com/Originate/replay-ruby
149
150
  licenses:
150
151
  - MIT
151
152
  metadata: {}
@@ -165,17 +166,17 @@ required_rubygems_version: !ruby/object:Gem::Requirement
165
166
  version: '0'
166
167
  requirements: []
167
168
  rubyforge_project:
168
- rubygems_version: 2.2.2
169
+ rubygems_version: 2.4.3
169
170
  signing_key:
170
171
  specification_version: 4
171
172
  summary: Ruby SDK for Replay.io API
172
173
  test_files:
173
- - spec/feature/client_spec.rb
174
174
  - spec/replay_api/client_spec.rb
175
175
  - spec/replay_api/compact_attributes_spec.rb
176
176
  - spec/replay_api/configuration_spec.rb
177
177
  - spec/replay_api/event_properties_spec.rb
178
178
  - spec/replay_api/event_spec.rb
179
+ - spec/replay_api/request_deliverer_spec.rb
179
180
  - spec/replay_api/trait_properties_spec.rb
180
181
  - spec/replay_api/trait_spec.rb
181
182
  - spec/spec_helper.rb
@@ -1,16 +0,0 @@
1
- require 'spec_helper'
2
-
3
- ReplayApi.configure do |config|
4
- config.replay_key = 'bogus-key'
5
- config.ssl = false
6
- end
7
-
8
- describe 'Posting Events' do
9
- let(:client) { ReplayApi::Client.new }
10
-
11
- it 'can post an event' do
12
- client.event do |event|
13
- event.event_name = 'Test Event'
14
- end
15
- end
16
- end