alephant-lookup 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 98503e14f9675716baec8c74c0cfe7882a12c628
4
- data.tar.gz: 444d864611be03547cdd1539998d5538bde4fa56
3
+ metadata.gz: ee8710b1105981b02407c5f9cde7eb768add6f7a
4
+ data.tar.gz: 7b5e984fcc9df4abd22a6381efedff235ddecd4e
5
5
  SHA512:
6
- metadata.gz: 7eacdceed2927d2b13fb2687346ae6c4eb5e69735a8f32870f17fc0357e92b107fa43d889084de7c3cda302f12eaa9a812ce828eb2cd84d0556acc3c56cc1e65
7
- data.tar.gz: fa017c4d8c8a384aa951f97370e667263c65b331cde16b62e596958abf89142ef1b5df5365e01a4e8e981d6de61a8747db82ea39b7e927e8176682a7ba6dd02f
6
+ metadata.gz: 15bb96737efdd8f21ae30527f200007d542fa9d19771efaf51622498d7bb8319d572b3ce8ed7fd317778dd50839aade495b48ff74a5c5262edba23097e812132
7
+ data.tar.gz: 0fa7a64f459bae0ee17cc94a7b2ffd354892b548d3b70ae9db6cc0685c4f9528b66165743038c4b8aeceddc3300c25219fa0809e9d8f12f2b6e503abbaffee62
data/README.md CHANGED
@@ -25,7 +25,7 @@ Or install it yourself as:
25
25
  ## Usage
26
26
 
27
27
  ```rb
28
- require 'alephant-lookup'
28
+ require 'alephant/lookup'
29
29
 
30
30
  lookup = Alephant::Lookup.create('table_name', 'component_id')
31
31
 
@@ -29,5 +29,7 @@ Gem::Specification.new do |spec|
29
29
  spec.add_development_dependency "rake"
30
30
 
31
31
  spec.add_runtime_dependency 'aws-sdk', '~> 1.0'
32
+ spec.add_runtime_dependency 'alephant-logger'
33
+
32
34
  spec.add_runtime_dependency "crimp"
33
35
  end
@@ -1,5 +1,5 @@
1
1
  require "alephant/lookup/version"
2
- require "alephant/lookup/lookup"
2
+ require "alephant/lookup/lookup_helper"
3
3
  require "alephant/lookup/lookup_table"
4
4
 
5
5
  module Alephant
@@ -8,7 +8,7 @@ module Alephant
8
8
 
9
9
  def self.create(table_name, component_id)
10
10
  @@lookup_tables[table_name] ||= LookupTable.new(table_name)
11
- Lookup.new(@@lookup_tables[table_name], component_id)
11
+ LookupHelper.new(@@lookup_tables[table_name], component_id)
12
12
  end
13
13
  end
14
14
  end
@@ -0,0 +1,71 @@
1
+ require 'aws-sdk'
2
+ require 'alephant/lookup/lookup_location'
3
+ require 'alephant/lookup/lookup_query'
4
+ require 'alephant/logger'
5
+
6
+ module Alephant
7
+ module Lookup
8
+ class LocationRead
9
+ include ::Alephant::Logger
10
+
11
+ attr_reader :table_name
12
+
13
+ def initialize(lookup_table)
14
+ @client = AWS::DynamoDB::Client::V20120810.new
15
+ @table_name = lookup_table.table_name
16
+ end
17
+
18
+ def read(lookup)
19
+ logger.info("LocationRead#read: looking up #{lookup.to_h}")
20
+ raise TypeError unless lookup.is_a? LookupQuery
21
+
22
+ location = LookupLocation.new(
23
+ lookup.component_id,
24
+ lookup.opts_hash,
25
+ s3_location_from(
26
+ run_query(
27
+ lookup.component_id,
28
+ lookup.opts_hash
29
+ )
30
+ )
31
+ )
32
+ logger.info("LocationRead#read: got location #{location.to_h}")
33
+
34
+ location
35
+ end
36
+
37
+ private
38
+
39
+ def s3_location_from(result)
40
+ result[:count] == 1 ? result[:member].first[LookupLocation::S3_LOCATION_FIELD][:s] : nil
41
+ end
42
+
43
+ def run_query(component_id, opts_hash)
44
+ @client.query(query(component_id, opts_hash))
45
+ end
46
+
47
+ def query(component_id, opts_hash)
48
+ {
49
+ :table_name => table_name,
50
+ :consistent_read => true,
51
+ :select => 'SPECIFIC_ATTRIBUTES',
52
+ :attributes_to_get => [LookupLocation::S3_LOCATION_FIELD],
53
+ :key_conditions => {
54
+ 'component_id' => {
55
+ :comparison_operator => 'EQ',
56
+ :attribute_value_list => [
57
+ { 's' => component_id.to_s }
58
+ ],
59
+ },
60
+ 'opts_hash' => {
61
+ :comparison_operator => 'EQ',
62
+ :attribute_value_list => [
63
+ { 's' => opts_hash }
64
+ ]
65
+ }
66
+ }
67
+ }
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,50 @@
1
+ require 'aws-sdk'
2
+ require 'alephant/lookup/lookup_query'
3
+ require 'alephant/logger'
4
+
5
+ module Alephant
6
+ module Lookup
7
+ class LocationWrite
8
+ include ::Alephant::Logger
9
+
10
+ attr_reader :table_name
11
+ attr_reader :lookups
12
+
13
+ def initialize(lookup_table)
14
+ @table_name = lookup_table.table_name
15
+ @batch = AWS::DynamoDB::BatchWrite.new
16
+ @lookups = []
17
+ @processed = false
18
+ end
19
+
20
+ def <<(lookup)
21
+ raise TypeError unless lookup.is_a? LookupQuery
22
+
23
+ @lookups << lookup
24
+ end
25
+
26
+ def processed?
27
+ @processed
28
+ end
29
+
30
+ def process!
31
+ logger.info("LocationWrite#process! #{processed? ? "is" : "not"} running batch put on #{table_name}")
32
+ processed? ? false : process_batch_put
33
+ end
34
+
35
+ private
36
+
37
+ def process_batch_put
38
+ logger.info("LocationWrite#process_batch_put to #{table_name} for #{@lookups.map { |lookup| lookup.to_h }}")
39
+
40
+ @batch.put(
41
+ table_name,
42
+ @lookups.map { |lookup| lookup.to_h }
43
+ )
44
+ @batch.process!
45
+
46
+ processed = true
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,46 @@
1
+ require 'alephant/lookup/lookup_table'
2
+ require 'alephant/lookup/lookup_query'
3
+ require 'alephant/lookup/location_read'
4
+ require 'alephant/lookup/location_write'
5
+
6
+ module Alephant
7
+ module Lookup
8
+ class LookupHelper
9
+ attr_reader :component_id
10
+
11
+ def initialize(lookup_table, component_id)
12
+ @lookup_table = lookup_table
13
+ @component_id = component_id
14
+ create_lookup_table
15
+ end
16
+
17
+ def read(opts)
18
+ reader = LocationRead.new(@lookup_table)
19
+ reader.read(LookupQuery.new(@component_id, opts)).location
20
+ end
21
+
22
+ def write(opts, location)
23
+ writer = LocationWrite.new(@lookup_table)
24
+ writer << LookupQuery.new(@component_id, opts, location)
25
+ writer.process!
26
+ end
27
+
28
+ def batch_write(opts, location)
29
+ @batch_write ||= LocationWrite.new(@lookup_table)
30
+ @batch_write << LookupQuery.new(@component_id, opts, location)
31
+ end
32
+
33
+ def batch_process
34
+ @batch_write.process!
35
+ @batch_write = nil
36
+ end
37
+
38
+ private
39
+
40
+ def create_lookup_table
41
+ @lookup_table.create
42
+ end
43
+
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,23 @@
1
+ module Alephant
2
+ module Lookup
3
+ class LookupLocation
4
+ S3_LOCATION_FIELD = 'location'
5
+
6
+ attr_reader :component_id, :opts, :location
7
+
8
+ def initialize(component_id, opts, location)
9
+ @component_id = component_id
10
+ @opts = opts
11
+ @location = location
12
+ end
13
+
14
+ def to_h
15
+ {
16
+ :component_id => @component_id,
17
+ :opts => opts,
18
+ S3_LOCATION_FIELD => @location
19
+ }
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,30 @@
1
+ require 'crimp'
2
+ require 'alephant/lookup/lookup_location'
3
+
4
+ module Alephant
5
+ module Lookup
6
+ class LookupQuery < LookupLocation
7
+ attr_reader :opts_hash
8
+ attr_writer :location
9
+
10
+ def initialize(component_id, opts, location = nil)
11
+ super(component_id, opts, location)
12
+ @opts_hash = hash_for(opts)
13
+ end
14
+
15
+ def to_h
16
+ {
17
+ :component_id => @component_id,
18
+ :opts_hash => @opts_hash,
19
+ S3_LOCATION_FIELD => @location
20
+ }
21
+ end
22
+
23
+ private
24
+
25
+ def hash_for(opts)
26
+ Crimp.signature opts
27
+ end
28
+ end
29
+ end
30
+ end
@@ -1,7 +1,6 @@
1
1
  require 'aws-sdk'
2
2
  require 'thread'
3
3
  require 'timeout'
4
- require 'crimp'
5
4
 
6
5
  module Alephant
7
6
  module Lookup
@@ -22,12 +21,9 @@ module Alephant
22
21
  }
23
22
  }
24
23
 
25
- S3_LOCATION_FIELD = 's3_location'
26
-
27
24
  def initialize(table_name, config = DEFAULT_CONFIG)
28
25
  @mutex = Mutex.new
29
26
  @dynamo_db = AWS::DynamoDB.new
30
- @client = AWS::DynamoDB::Client::V20120810.new
31
27
  @table_name = table_name
32
28
  @config = config
33
29
  end
@@ -43,48 +39,8 @@ module Alephant
43
39
  @table ||= @dynamo_db.tables[@table_name]
44
40
  end
45
41
 
46
-
47
- def location_for(component_id, opts)
48
- result = @client.query({
49
- :table_name => @table_name,
50
- :consistent_read => true,
51
- :select => 'SPECIFIC_ATTRIBUTES',
52
- :attributes_to_get => [S3_LOCATION_FIELD],
53
- :key_conditions => {
54
- 'component_id' => {
55
- :comparison_operator => 'EQ',
56
- :attribute_value_list => [
57
- { 's' => component_id.to_s }
58
- ],
59
- },
60
- 'opts_hash' => {
61
- :comparison_operator => 'EQ',
62
- :attribute_value_list => [
63
- { 's' => hash_for(opts) }
64
- ]
65
- }
66
- }
67
- })
68
- result[:count] == 1 ? result[:member].first[S3_LOCATION_FIELD][:s] : nil
69
- end
70
-
71
- def write_location_for(component_id, opts, location)
72
-
73
- @table.batch_put([
74
- {
75
- :component_id => component_id,
76
- :opts_hash => hash_for(opts),
77
- :s3_location => location
78
- }
79
- ])
80
- end
81
-
82
42
  private
83
43
 
84
- def hash_for(opts)
85
- Crimp.signature opts
86
- end
87
-
88
44
  def ensure_table_exists
89
45
  create_dynamodb_table unless table.exists?
90
46
  end
@@ -1,5 +1,5 @@
1
1
  module Alephant
2
2
  module Lookup
3
- VERSION = "0.0.3"
3
+ VERSION = "0.0.4"
4
4
  end
5
5
  end
data/spec/lookup_spec.rb CHANGED
@@ -3,17 +3,17 @@ require 'spec_helper'
3
3
  describe Alephant::Lookup do
4
4
  describe '.create(table_name, component_id)' do
5
5
  it 'returns a lookup' do
6
- Alephant::Lookup::Lookup
6
+ Alephant::Lookup::LookupHelper
7
7
  .any_instance
8
8
  .stub(:initialize)
9
9
  .and_return(double())
10
10
 
11
- expect(subject.create(:table_name, :component_id)).to be_a Alephant::Lookup::Lookup
11
+ expect(subject.create(:table_name, :component_id)).to be_a Alephant::Lookup::LookupHelper
12
12
  end
13
13
  end
14
14
 
15
- describe Alephant::Lookup::Lookup do
16
- subject { Alephant::Lookup::Lookup }
15
+ describe Alephant::Lookup::LookupHelper do
16
+ subject { Alephant::Lookup::LookupHelper }
17
17
 
18
18
  describe '#initialize(table_name)' do
19
19
  it 'calls create on lookup_table' do
@@ -26,8 +26,9 @@ describe Alephant::Lookup do
26
26
  describe '#read(opts)' do
27
27
  let (:lookup_table) { Alephant::Lookup::LookupTable }
28
28
  let (:s3_location) { '/s3-render-example/test/html/england_council_election_results/responsive' }
29
+ let (:location_read){ Alephant::Lookup::LocationRead }
29
30
 
30
- it 'returns lookup_table.location_for(component_id, opts)' do
31
+ it 'returns correct S3 Location' do
31
32
  subject
32
33
  .any_instance
33
34
  .stub(:create_lookup_table)
@@ -36,10 +37,12 @@ describe Alephant::Lookup do
36
37
  .any_instance
37
38
  .stub(:initialize)
38
39
 
39
- lookup_table
40
+ location = Alephant::Lookup::LookupLocation.new(:component_id,:opts_hash, s3_location)
41
+
42
+ location_read
40
43
  .any_instance
41
- .stub(:location_for)
42
- .and_return(s3_location)
44
+ .stub(:read)
45
+ .and_return(location)
43
46
 
44
47
  pal_opts = {
45
48
  :id => :england_council_election_results,
@@ -56,8 +59,9 @@ describe Alephant::Lookup do
56
59
 
57
60
  describe '#write(opts, location)' do
58
61
 
59
- let (:lookup_table) { Alephant::Lookup::LookupTable }
60
- let (:s3_location) { '/s3-render-example/test/html/england_council_election_results/responsive' }
62
+ let (:lookup_table) { Alephant::Lookup::LookupTable }
63
+ let (:s3_location) { '/s3-render-example/test/html/england_council_election_results/responsive' }
64
+ let (:location_write){ Alephant::Lookup::LocationWrite }
61
65
 
62
66
  it 'returns lookup_table.update_location_for(component_id, opts_hash, data)' do
63
67
 
@@ -69,23 +73,39 @@ describe Alephant::Lookup do
69
73
  .any_instance
70
74
  .stub(:initialize)
71
75
 
76
+ lookup_table
77
+ .any_instance
78
+ .stub(:table_name)
79
+ .and_return('table_name')
80
+
72
81
  pal_opts = {
73
82
  :id => :england_council_election_results,
74
83
  :env => :test,
75
84
  :type => :responsive
76
85
  }
77
86
 
78
- lookup_table
87
+ AWS::DynamoDB::BatchWrite
79
88
  .any_instance
80
- .stub(:write_location_for)
81
- .with(pal_opts[:id], pal_opts, :s3_location)
82
- .and_return(nil)
89
+ .should_receive(:put)
90
+ .with(
91
+ 'table_name',
92
+ [
93
+ {
94
+ :component_id=> :england_council_election_results,
95
+ :opts_hash=>"52a25baaaa8c4527ddc869feaa285c3a",
96
+ "location"=>:s3_location
97
+ }
98
+ ]
99
+ )
100
+
101
+ AWS::DynamoDB::BatchWrite
102
+ .any_instance
103
+ .should_receive(:process!)
83
104
 
84
105
  instance = subject.new(lookup_table.new, pal_opts[:id])
85
106
  write_return = instance.write(pal_opts, :s3_location)
86
107
 
87
- expect(write_return).to eq(nil)
88
-
108
+ expect(write_return).to eq(true)
89
109
  end
90
110
  end
91
111
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alephant-lookup
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
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-02-17 00:00:00.000000000 Z
11
+ date: 2014-02-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -150,6 +150,20 @@ dependencies:
150
150
  version: '1.0'
151
151
  prerelease: false
152
152
  type: :runtime
153
+ - !ruby/object:Gem::Dependency
154
+ name: alephant-logger
155
+ version_requirements: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - '>='
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ requirement: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - '>='
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ prerelease: false
166
+ type: :runtime
153
167
  - !ruby/object:Gem::Dependency
154
168
  name: crimp
155
169
  version_requirements: !ruby/object:Gem::Requirement
@@ -181,7 +195,11 @@ files:
181
195
  - Rakefile
182
196
  - alephant-lookup.gemspec
183
197
  - lib/alephant/lookup.rb
184
- - lib/alephant/lookup/lookup.rb
198
+ - lib/alephant/lookup/location_read.rb
199
+ - lib/alephant/lookup/location_write.rb
200
+ - lib/alephant/lookup/lookup_helper.rb
201
+ - lib/alephant/lookup/lookup_location.rb
202
+ - lib/alephant/lookup/lookup_query.rb
185
203
  - lib/alephant/lookup/lookup_table.rb
186
204
  - lib/alephant/lookup/version.rb
187
205
  - spec/lookup_spec.rb
@@ -1,28 +0,0 @@
1
- module Alephant
2
- module Lookup
3
- class Lookup
4
- attr_reader :component_id
5
-
6
- def initialize(lookup_table, component_id)
7
- @lookup_table = lookup_table
8
- @component_id = component_id
9
- create_lookup_table
10
- end
11
-
12
- def read(opts)
13
- @lookup_table.location_for(@component_id, opts)
14
- end
15
-
16
- def write(opts, location)
17
- @lookup_table.write_location_for(@component_id, opts, location)
18
- end
19
-
20
- private
21
-
22
- def create_lookup_table
23
- @lookup_table.create
24
- end
25
-
26
- end
27
- end
28
- end