alephant-sequencer 0.0.7 → 0.0.8

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: 2cfb808e20d9d7e1ff9bf36eca6c61d3fb042152
4
- data.tar.gz: 4936135a727783f3925d23ec2c018e04b6baf2f5
3
+ metadata.gz: 0390458d47e5ab213103d386505b95893d3b22ff
4
+ data.tar.gz: 6233dd2447347b73b0cb62349aa28fa3c6f74a3d
5
5
  SHA512:
6
- metadata.gz: 83c66149df5dab3d97d6a619bbbc3b42adeb8887e1a3adc7cf39669f3a147c35fafacbf16062f91b737b9ef3123791fa7b9ee32148bd916917a329b8e3bbb7a5
7
- data.tar.gz: d11b43af46a3329ac430b3e7669d054787b009370fa4957667fed40e06592e7e131e2599cd64eae7c273c91ed83de989b6f1cf475700209a0ff5823c640699c0
6
+ metadata.gz: b587d839b93e7eca666ca6e947e8d5c941331b582d0629bf92fe541dab39c899dd9494d65c3c57e214e0c43dc6a374609039dd4fc7ff782da8ef17444ad5e5ed
7
+ data.tar.gz: 6b54e05af1e9997358adbe0abf83010f9701da8781310578bbb8df75a113dd3d962186e810e8380d05c4fdabbffdb323623b1e1ec0f76e8fae6a428d8e12c2cd
data/README.md CHANGED
@@ -24,29 +24,34 @@ Or install it yourself as:
24
24
  ## Usage
25
25
 
26
26
  ```rb
27
+ require "alephant/sequencer"
27
28
 
28
- require 'alephant-sequencer'
29
+ # Ensure you check your AWS region configuration before you start using `AWS.config.region`
30
+ # To set the value use: AWS.config(region: 'eu-west-1')
29
31
 
30
- #Optional JSONPath specifying location of sequence_id
31
- sequence_id = '$.sequence_number'
32
+ table_name = "foo"
33
+ component_id = "bar_baz/e8c81cbbbeb3967a423bb49e352eed0e"
34
+ sequence_id = "$.sequence_number" # Optional JSONPath (specifying location of sequence_id)
32
35
 
33
- sequencer = Sequencer.create(table_name, sqs_queue_url, sequence_id)
36
+ sequencer = Alephant::Sequencer.create(table_name, component_id, sequence_id)
34
37
 
35
38
  # Data from SQS message
36
- data = Struct.new(:body).new({:sequence_number => 3})
39
+ json = JSON.generate({ :sequence_number => 3 })
40
+ msg = Struct.new(:body).new(json)
37
41
 
38
42
  # Sets last seen id
39
- sequencer.set_last_seen(data)
43
+ sequencer.set_last_seen(msg)
40
44
 
41
45
  # Gets last seen id
42
46
  sequencer.get_last_seen
43
47
  # => 3
44
48
 
45
49
  # Is the message sequential?
46
- sequencer.sequential?(data)
50
+ sequencer.sequential?(msg)
47
51
 
48
52
  # Reset sequence
49
53
  sequencer.delete!
54
+ ```
50
55
 
51
56
  ## Contributing
52
57
  1. Fork it ( http://github.com/<my-github-username>/alephant-sequencer/fork )
@@ -29,5 +29,6 @@ Gem::Specification.new do |spec|
29
29
 
30
30
  spec.add_runtime_dependency 'aws-sdk', '~> 1.0'
31
31
  spec.add_runtime_dependency 'alephant-logger'
32
+ spec.add_runtime_dependency 'alephant-support'
32
33
  spec.add_runtime_dependency 'jsonpath'
33
34
  end
@@ -1,21 +1,17 @@
1
- require 'aws-sdk'
2
- require 'thread'
3
- require 'timeout'
1
+ require "aws-sdk"
2
+ require "thread"
3
+ require "timeout"
4
4
 
5
- require 'alephant/logger'
5
+ require "alephant/logger"
6
+ require "alephant/support/dynamodb/table"
6
7
 
7
8
  module Alephant
8
9
  module Sequencer
9
- class SequenceTable
10
+ class SequenceTable < ::Alephant::Support::DynamoDB::Table
10
11
  include ::Alephant::Logger
11
12
 
12
13
  attr_reader :table_name
13
14
 
14
- TIMEOUT = 120
15
- DEFAULT_CONFIG = {
16
- :write_units => 5,
17
- :read_units => 10,
18
- }
19
15
  SCHEMA = {
20
16
  :hash_key => {
21
17
  :key => :string,
@@ -47,7 +43,7 @@ module Alephant
47
43
 
48
44
  def sequence_for(ident)
49
45
  rows = batch_get_value_for(ident)
50
- rows.count >= 1 ? rows.first['value'].to_i : nil
46
+ rows.count >= 1 ? rows.first["value"].to_i : nil
51
47
  end
52
48
 
53
49
  def set_sequence_for(ident, value, last_seen_check = nil)
@@ -78,12 +74,13 @@ module Alephant
78
74
  end
79
75
 
80
76
  private
77
+
81
78
  def put_condition(last_seen_check)
82
79
  last_seen_check.nil? ? unless_exists(:key) : if_value(last_seen_check)
83
80
  end
84
81
 
85
82
  def batch_get_value_for(ident)
86
- table.batch_get(['value'],[ident],batch_get_opts)
83
+ table.batch_get(["value"],[ident],batch_get_opts)
87
84
  end
88
85
 
89
86
  def unless_exists(key)
@@ -42,6 +42,10 @@ module Alephant
42
42
  @sequence_table.delete_item!(ident)
43
43
  end
44
44
 
45
+ def truncate!
46
+ @sequence_table.truncate!
47
+ end
48
+
45
49
  def set_last_seen(msg, last_seen_check = nil)
46
50
  seen_id = Sequencer.sequence_id_from(msg, jsonpath)
47
51
 
@@ -1,5 +1,5 @@
1
1
  module Alephant
2
2
  module Sequencer
3
- VERSION = "0.0.7"
3
+ VERSION = "0.0.8"
4
4
  end
5
5
  end
@@ -1,18 +1,23 @@
1
- require 'spec_helper'
1
+ require "spec_helper"
2
2
 
3
3
  describe Alephant::Sequencer do
4
- let(:ident) { :ident }
4
+ let(:ident) { :ident }
5
5
  let(:jsonpath) { "$.sequence_id" }
6
6
 
7
7
  describe ".create(table_name, ident, jsonpath)" do
8
8
  it "should return a Sequencer" do
9
- Alephant::Sequencer::SequenceTable.any_instance.stub(:create)
10
- expect(subject.create(:table_name, ident, jsonpath)).to be_a Alephant::Sequencer::Sequencer
9
+ Alephant::Sequencer::SequenceTable
10
+ .any_instance
11
+ .stub(:create)
12
+
13
+ expect(
14
+ subject.create(:table_name, ident, jsonpath)
15
+ ).to be_a Alephant::Sequencer::Sequencer
11
16
  end
12
17
  end
13
18
 
14
19
  describe Alephant::Sequencer::Sequencer do
15
- let(:data) { double() }
20
+ let(:data) { double() }
16
21
  let(:last_seen) { 42 }
17
22
 
18
23
  def sequence_table
@@ -21,7 +26,7 @@ describe Alephant::Sequencer do
21
26
  table.stub(:sequence_exists)
22
27
  table.stub(:sequence_for)
23
28
  table.stub(:set_sequence_for)
24
-
29
+ table.stub(:truncate!)
25
30
  table
26
31
  end
27
32
 
@@ -42,12 +47,10 @@ describe Alephant::Sequencer do
42
47
  end
43
48
  end
44
49
 
45
-
46
50
  describe "#sequence(msg, &block)" do
47
51
  let(:message) do
48
52
  m = double()
49
53
  m.stub(:body)
50
-
51
54
  m
52
55
  end
53
56
 
@@ -71,15 +74,19 @@ describe Alephant::Sequencer do
71
74
 
72
75
  context "last_seen_id is nil" do
73
76
  before(:each) do
74
- Alephant::Sequencer::Sequencer.any_instance
75
- .stub(:get_last_seen).and_return(nil)
77
+ Alephant::Sequencer::Sequencer
78
+ .any_instance
79
+ .stub(:get_last_seen)
80
+ .and_return(nil)
76
81
 
77
82
  Alephant::Sequencer::Sequencer
78
- .stub(:sequence_id_from).and_return(stubbed_seen_high)
83
+ .stub(:sequence_id_from)
84
+ .and_return(stubbed_seen_high)
79
85
  end
80
86
 
81
87
  it "should not call set_last_seen(msg, last_seen_id)" do
82
- Alephant::Sequencer::Sequencer.any_instance
88
+ Alephant::Sequencer::Sequencer
89
+ .any_instance
83
90
  .should_receive(:set_last_seen)
84
91
  .with(message, nil)
85
92
 
@@ -90,15 +97,19 @@ describe Alephant::Sequencer do
90
97
 
91
98
  context "last_seen_id == sequence_id_from(msg)" do
92
99
  before(:each) do
93
- Alephant::Sequencer::Sequencer.any_instance
94
- .stub(:get_last_seen).and_return(stubbed_last_seen)
100
+ Alephant::Sequencer::Sequencer
101
+ .any_instance
102
+ .stub(:get_last_seen)
103
+ .and_return(stubbed_last_seen)
95
104
 
96
105
  Alephant::Sequencer::Sequencer
97
- .stub(:sequence_id_from).and_return(stubbed_last_seen)
106
+ .stub(:sequence_id_from)
107
+ .and_return(stubbed_last_seen)
98
108
  end
99
109
 
100
110
  it "should not call set_last_seen(msg, last_seen_id)" do
101
- Alephant::Sequencer::Sequencer.any_instance
111
+ Alephant::Sequencer::Sequencer
112
+ .any_instance
102
113
  .should_not_receive(:set_last_seen)
103
114
 
104
115
  subject = Alephant::Sequencer::Sequencer.new(sequence_table, ident, jsonpath)
@@ -108,15 +119,20 @@ describe Alephant::Sequencer do
108
119
 
109
120
  context "last_seen_id > sequence_id_from(msg)" do
110
121
  before(:each) do
111
- Alephant::Sequencer::Sequencer.any_instance
112
- .stub(:get_last_seen).and_return(stubbed_last_seen)
122
+ Alephant::Sequencer::Sequencer
123
+ .any_instance
124
+ .stub(:get_last_seen)
125
+ .and_return(stubbed_last_seen)
113
126
 
114
- Alephant::Sequencer::Sequencer.any_instance
115
- .stub(:sequence_id_from).and_return(stubbed_seen_low)
127
+ Alephant::Sequencer::Sequencer
128
+ .any_instance
129
+ .stub(:sequence_id_from)
130
+ .and_return(stubbed_seen_low)
116
131
  end
117
132
 
118
133
  it "should not call set_last_seen(msg, last_seen_id)" do
119
- Alephant::Sequencer::Sequencer.any_instance
134
+ Alephant::Sequencer::Sequencer
135
+ .any_instance
120
136
  .should_not_receive(:set_last_seen)
121
137
 
122
138
  subject = Alephant::Sequencer::Sequencer.new(sequence_table, ident, jsonpath)
@@ -126,15 +142,19 @@ describe Alephant::Sequencer do
126
142
 
127
143
  context "last_seen_id < sequence_id_from(msg)" do
128
144
  before(:each) do
129
- Alephant::Sequencer::Sequencer.any_instance
130
- .stub(:get_last_seen).and_return(stubbed_last_seen)
145
+ Alephant::Sequencer::Sequencer
146
+ .any_instance
147
+ .stub(:get_last_seen)
148
+ .and_return(stubbed_last_seen)
131
149
 
132
150
  Alephant::Sequencer::Sequencer
133
- .stub(:sequence_id_from).and_return(stubbed_seen_high)
151
+ .stub(:sequence_id_from)
152
+ .and_return(stubbed_seen_high)
134
153
  end
135
154
 
136
155
  it "should call set_last_seen(msg, last_seen_id)" do
137
- Alephant::Sequencer::Sequencer.any_instance
156
+ Alephant::Sequencer::Sequencer
157
+ .any_instance
138
158
  .should_receive(:set_last_seen)
139
159
  .with(message, stubbed_last_seen)
140
160
 
@@ -149,17 +169,23 @@ describe Alephant::Sequencer do
149
169
  table = double()
150
170
  table.stub(:sequence_exists)
151
171
  table.stub(:create)
152
- table.should_receive(:sequence_for).with(ident).and_return(:expected_value)
172
+ table.should_receive(:sequence_for)
173
+ .with(ident)
174
+ .and_return(:expected_value)
153
175
 
154
176
  expect(
155
- Alephant::Sequencer::Sequencer.new(table, ident, jsonpath).get_last_seen
177
+ Alephant::Sequencer::Sequencer
178
+ .new(table, ident, jsonpath)
179
+ .get_last_seen
156
180
  ).to eq(:expected_value)
157
181
  end
158
182
  end
159
183
 
160
184
  describe "#set_last_seen(data)" do
161
185
  before(:each) do
162
- Alephant::Sequencer::Sequencer.stub(:sequence_id_from).and_return(last_seen)
186
+ Alephant::Sequencer::Sequencer
187
+ .stub(:sequence_id_from)
188
+ .and_return(last_seen)
163
189
  end
164
190
 
165
191
  it "calls set_sequence_for(ident, last_seen)" do
@@ -167,32 +193,46 @@ describe Alephant::Sequencer do
167
193
  table.stub(:sequence_exists)
168
194
  table.stub(:create)
169
195
  table.stub(:sequence_for)
170
- table.should_receive(:set_sequence_for).with(ident, last_seen, nil)
196
+ table.should_receive(:set_sequence_for)
197
+ .with(ident, last_seen, nil)
171
198
 
172
- Alephant::Sequencer::Sequencer.new(table, ident, jsonpath).set_last_seen(data)
199
+ Alephant::Sequencer::Sequencer
200
+ .new(table, ident, jsonpath)
201
+ .set_last_seen(data)
173
202
  end
174
203
  end
175
204
 
176
205
  describe ".sequence_id_from(data)" do
177
206
  subject { Alephant::Sequencer::Sequencer }
207
+
178
208
  it "should return the id described by the set jsonpath" do
179
- msg = Struct.new(:body).new({ "set_sequence_id" => 1 })
180
- expect(subject.sequence_id_from(msg,'$.set_sequence_id')).to eq(1)
209
+ msg = Struct.new(:body).new("set_sequence_id" => 1)
210
+
211
+ expect(
212
+ subject.sequence_id_from(msg, "$.set_sequence_id")
213
+ ).to eq(1)
181
214
  end
182
215
  end
183
216
 
184
217
  describe "#sequential?(data, jsonpath)" do
185
-
186
218
  before(:each) do
187
- Alephant::Sequencer::Sequencer.any_instance.stub(:get_last_seen).and_return(1)
188
- data.stub(:body).and_return('sequence_id' => id_value)
219
+ Alephant::Sequencer::Sequencer
220
+ .any_instance
221
+ .stub(:get_last_seen)
222
+ .and_return(1)
223
+
224
+ data.stub(:body)
225
+ .and_return("sequence_id" => id_value)
189
226
  end
190
227
 
191
228
  context "jsonpath = '$.sequence_id'" do
192
- let(:jsonpath) { '$.sequence_id' }
229
+ let(:jsonpath) { "$.sequence_id" }
230
+
193
231
  subject { Alephant::Sequencer::Sequencer.new(sequence_table, :ident, jsonpath) }
232
+
194
233
  context "sequential" do
195
234
  let(:id_value) { 2 }
235
+
196
236
  it "is true" do
197
237
  expect(subject.sequential?(data)).to be_true
198
238
  end
@@ -200,6 +240,7 @@ describe Alephant::Sequencer do
200
240
 
201
241
  context "nonsequential" do
202
242
  let(:id_value) { 0 }
243
+
203
244
  it "is false" do
204
245
  expect(subject.sequential?(data)).to be_false
205
246
  end
@@ -207,11 +248,13 @@ describe Alephant::Sequencer do
207
248
  end
208
249
 
209
250
  context "jsonpath = nil" do
210
- let(:jsonpath) { '$.sequence_id' }
251
+ let(:jsonpath) { "$.sequence_id" }
252
+
211
253
  subject { Alephant::Sequencer::Sequencer.new(sequence_table, :ident, jsonpath) }
212
254
 
213
255
  context "sequential" do
214
256
  let(:id_value) { 2 }
257
+
215
258
  it "is true" do
216
259
  expect(subject.sequential?(data)).to be_true
217
260
  end
@@ -219,12 +262,24 @@ describe Alephant::Sequencer do
219
262
 
220
263
  context "nonsequential" do
221
264
  let(:id_value) { 0 }
265
+
222
266
  it "is false" do
223
267
  expect(subject.sequential?(data)).to be_false
224
268
  end
225
269
  end
226
270
  end
271
+ end
272
+
273
+ describe "#truncate!" do
274
+ it "verify SequenceTable#truncate!" do
275
+ table = double()
276
+ table.stub(:create)
277
+ table.stub(:sequence_exists)
278
+ table.should_receive(:truncate!)
227
279
 
280
+ subject = Alephant::Sequencer::Sequencer.new(table, ident, jsonpath)
281
+ subject.truncate!
282
+ end
228
283
  end
229
284
  end
230
285
  end
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  $: << File.join(File.dirname(__FILE__),"..", "lib")
2
2
 
3
- require 'pry'
4
- require 'alephant/sequencer'
3
+ require "pry"
4
+ require "alephant/sequencer"
5
+ require "alephant/support"
5
6
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alephant-sequencer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Kenny
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-14 00:00:00.000000000 Z
11
+ date: 2014-04-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -164,6 +164,20 @@ dependencies:
164
164
  version: '0'
165
165
  prerelease: false
166
166
  type: :runtime
167
+ - !ruby/object:Gem::Dependency
168
+ name: alephant-support
169
+ version_requirements: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - '>='
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ requirement: !ruby/object:Gem::Requirement
175
+ requirements:
176
+ - - '>='
177
+ - !ruby/object:Gem::Version
178
+ version: '0'
179
+ prerelease: false
180
+ type: :runtime
167
181
  - !ruby/object:Gem::Dependency
168
182
  name: jsonpath
169
183
  version_requirements: !ruby/object:Gem::Requirement