rollout-dynamodb 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fec540bc701467a7e30414f581699009230e0185
4
+ data.tar.gz: 28dff4465caca5250270c6216cc104de4efa8312
5
+ SHA512:
6
+ metadata.gz: e8b2c4128eb2c427ec632c90219d4fa5d86f8fd9c828196e27b3aeec9315a0d01d0c0d2dcb331c5d2b95db420bd7a2d95337311eaa37033e6f9f94816592d21d
7
+ data.tar.gz: 380f702669187f9dd65bd83e71baf2583b256aafc7cc7cf1f8dd78d4a4b1535705f4b81ee3c48932d7d1fc29a617ce79c7b0907a59e99a22a5f7d452c3a215a1
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ bin/test*
16
+ .env
17
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+ ruby "2.1.3"
3
+ # Specify your gem's dependencies in rollout-dynamo.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Charles Hooper <charles@heroku.com>
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,50 @@
1
+ # DynamoDB storage adapter for Rollout
2
+
3
+ This is a storage adapter for
4
+ [Rollout](https://github.com/FetLife/rollout) that uses DynamoDB.
5
+ Rollout allows you to flip feature flags around in a sane way.
6
+
7
+ I wrote this adapter for those who either don't want to stand up a new
8
+ redis instance or introduce a new single point of failure into their
9
+ architecture.
10
+
11
+ [![Build Status](https://travis-ci.org/chooper/rollout-dynamodb.svg?branch=master)](https://travis-ci.org/chooper/rollout-dynamodb)
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'rollout-dynamodb'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ $ bundle
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install rollout-dynamodb
28
+
29
+ ## Usage
30
+
31
+ ```ruby
32
+ $storage = Rollout::DynamoDB::Storage.new(aws_access_key, aws_secret_key, aws_table_name, aws_region)
33
+ $rollout = Rollout.new($storage)
34
+
35
+ $rollout.active?(:chat)
36
+ # -> false
37
+ $rollout.activate(:chat)
38
+ # -> {}
39
+ $rollout.active?(:chat)
40
+ # -> true
41
+ ```
42
+
43
+ ## Contributing
44
+
45
+ 1. Fork it ( https://github.com/chooper/rollout-dynamodb/fork )
46
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
47
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
48
+ 4. Push to the branch (`git push origin my-new-feature`)
49
+ 5. Create a new Pull Request
50
+
data/Rakefile ADDED
@@ -0,0 +1,92 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task :default => :spec
6
+
7
+ def name
8
+ @name ||= Dir['*.gemspec'].first.split('.').first
9
+ end
10
+
11
+ def version
12
+ line = File.read("lib/#{name}.rb")[/^\s*VERSION\s*=\s*.*/]
13
+ line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
14
+ end
15
+
16
+ def date
17
+ Date.today.to_s
18
+ end
19
+
20
+ def rubyforge_project
21
+ name
22
+ end
23
+
24
+ def gemspec_file
25
+ "#{name}.gemspec"
26
+ end
27
+
28
+ def gem_file
29
+ "#{name}-#{version}.gem"
30
+ end
31
+
32
+ def replace_header(head, header_name)
33
+ head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"}
34
+ end
35
+
36
+ desc "Open an irb session preloaded with this library"
37
+ task :console do
38
+ sh "irb -rubygems -r ./lib/#{name}.rb"
39
+ end
40
+
41
+ desc "Create tag v#{version} and build and push #{gem_file} to Rubygems"
42
+ task :release => :build do
43
+ unless `git branch` =~ /^\* master$/
44
+ puts "You must be on the master branch to release!"
45
+ exit!
46
+ end
47
+ sh "git commit --allow-empty -a -m 'Release #{version}'"
48
+ sh "git tag v#{version}"
49
+ sh "git push origin master"
50
+ sh "git push origin v#{version}"
51
+ sh "gem push pkg/#{name}-#{version}.gem"
52
+ end
53
+
54
+ desc "Build #{gem_file} into the pkg directory"
55
+ task :build => :gemspec do
56
+ sh "mkdir -p pkg"
57
+ sh "gem build #{gemspec_file}"
58
+ sh "mv #{gem_file} pkg"
59
+ end
60
+
61
+ desc "Generate #{gemspec_file}"
62
+ task :gemspec => :validate do
63
+ # read spec file and split out manifest section
64
+ spec = File.read(gemspec_file)
65
+ #head, manifest, tail = spec,split(" # = MANIFEST =\n")
66
+
67
+ # replace name version and date
68
+ replace_header(spec, :name)
69
+ replace_header(spec, :version)
70
+ replace_header(spec, :date)
71
+ #comment this out if your rubyforge_project has a different name
72
+ replace_header(spec, :rubyforge_project)
73
+
74
+ # piece file back together and write
75
+ File.open(gemspec_file, 'w') { |io| io.write(spec) }
76
+ puts "Updated #{gemspec_file}"
77
+ end
78
+
79
+ desc "Validate #{gemspec_file}"
80
+ task :validate do
81
+ prefix = name[/^([^-]*)/, 1]
82
+ libfiles = Dir['lib/*'] - ["lib/#{name}.rb", "lib/#{name}", "lib/#{prefix}"]
83
+ unless libfiles.empty?
84
+ puts "Directory `lib` should only contain a `#{name}.rb` file and `#{name}` dir."
85
+ exit!
86
+ end
87
+ unless Dir['VERSION*'].empty?
88
+ puts "A `VERSION` file at root level violates Gem best practices."
89
+ exit!
90
+ end
91
+ end
92
+
@@ -0,0 +1,51 @@
1
+
2
+ require 'fog'
3
+
4
+ module Rollout::DynamoDB
5
+ class Storage
6
+ def initialize(aws_access_key, aws_secret_key, table_name, region, options = {})
7
+ @aws_access_key = aws_access_key
8
+ @aws_secret_key = aws_secret_key
9
+ @table_name = table_name
10
+ @region = region
11
+ @options = options
12
+
13
+ # timeouts are expressed in seconds
14
+ @options[:connection_options] ||= {}
15
+ @options[:connection_options][:connect_timeout] ||= 3
16
+ @options[:connection_options][:read_timeout] ||= 3
17
+ @options[:connection_options][:write_timeout] ||= 3
18
+ @options[:connection_options][:retry_limit] ||= 4
19
+ end
20
+
21
+ def get(key)
22
+ response = db.get_item(@table_name, {'HashKeyElement' => {'S' => key}})
23
+ get_item_from_body(response.body)
24
+ end
25
+
26
+ def set(key, value)
27
+ response = db.put_item(@table_name, {'id' => {'S' => key}, 'value' => {'S' => value}})
28
+ true
29
+ end
30
+
31
+ def del(key)
32
+ response = db.delete_item(@table_name, {'HashKeyElement' => {'S' => key}})
33
+ true
34
+ end
35
+
36
+ private
37
+
38
+ def db
39
+ @db ||= Fog::AWS::DynamoDB.new(
40
+ @options.merge(
41
+ :aws_access_key_id => @aws_access_key,
42
+ :aws_secret_access_key => @aws_secret_key,
43
+ :region => @region))
44
+ end
45
+
46
+ def get_item_from_body(body)
47
+ body.fetch('Item', {}).fetch('value', {}).fetch('S', nil)
48
+ end
49
+ end
50
+ end
51
+
@@ -0,0 +1,5 @@
1
+ class Rollout
2
+ module DynamoDB
3
+ VERSION = "0.0.3"
4
+ end
5
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require_relative "lib/rollout-dynamodb.rb"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rollout-dynamodb"
8
+ spec.version = Rollout::DynamoDB::VERSION
9
+ spec.authors = ["Charles Hooper"]
10
+ spec.email = ["charles@heroku.com"]
11
+ spec.summary = "DynamoDB storage adapter for Rollout"
12
+ spec.description = "DynamoDB storage adapter for Rollout"
13
+ spec.homepage = "https://github.com/chooper/rollout-dynamodb"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "foreman", "~> 0.77"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.2"
25
+ spec.add_development_dependency "webmock", "~> 1.20"
26
+ spec.add_dependency 'rollout', "~> 2.0"
27
+ spec.add_dependency "fog", "~> 1.28"
28
+ end
@@ -0,0 +1,89 @@
1
+ #require "rollout/storage/dynamodb"
2
+ require "spec_helper"
3
+
4
+ describe Rollout::DynamoDB do
5
+ before do
6
+ @storage = Rollout::DynamoDB::Storage.new("access-key", "secret-key", "table", "us-east-99")
7
+ @dynamo_url = "https://dynamodb.us-east-99.amazonaws.com/"
8
+ end
9
+
10
+ describe "#initialize" do
11
+ it "sets default options" do
12
+ defaults = {
13
+ :connection_options => {
14
+ :connect_timeout => 3,
15
+ :read_timeout => 3,
16
+ :write_timeout => 3,
17
+ :retry_limit => 4}}
18
+
19
+ assert_equal @storage.instance_variable_get(:@options), defaults
20
+ end
21
+
22
+ it "sets AWS/DynamoDB instance variables" do
23
+ assert_equal @storage.instance_variable_get(:@aws_access_key), "access-key"
24
+ assert_equal @storage.instance_variable_get(:@aws_secret_key), "secret-key"
25
+ assert_equal @storage.instance_variable_get(:@table_name), "table"
26
+ assert_equal @storage.instance_variable_get(:@region), "us-east-99"
27
+ end
28
+ end
29
+
30
+ describe "#get" do
31
+ it "sends a valid post and returns valid data when key exists" do
32
+ stub_request(:post, @dynamo_url).
33
+ to_return(:body => "{\"ConsumedCapacityUnits\":0.5,\"Item\":{\"value\":{\"S\":\"my-value\"},\"id\":{\"S\":\"my-key\"}}}")
34
+
35
+ value = @storage.get("my-key")
36
+ assert_requested(:post, @dynamo_url) do |req|
37
+ body = MultiJson.decode(req.body)
38
+ body == {"Key"=>{"HashKeyElement"=>{"S"=>"my-key"}}, "TableName"=>"table"} &&
39
+ req.headers["X-Amz-Target"] == "DynamoDB_20111205.GetItem"
40
+ end
41
+ assert_equal value, "my-value"
42
+ end
43
+
44
+ it "returns valid data when key does not exist" do
45
+ stub_request(:post, @dynamo_url).
46
+ to_return(:body => "{\"ConsumedCapacityUnits\":0.5}")
47
+
48
+ response = @storage.get("my-nonexistent-key")
49
+ assert_requested(:post, @dynamo_url) do |req|
50
+ body = MultiJson.decode(req.body)
51
+ body == {"Key"=>{"HashKeyElement"=>{"S"=>"my-nonexistent-key"}}, "TableName"=>"table"} &&
52
+ req.headers["X-Amz-Target"] == "DynamoDB_20111205.GetItem"
53
+ end
54
+ assert_nil response
55
+ end
56
+ end
57
+
58
+ describe "#set" do
59
+ it "sends a valid post when called" do
60
+ stub_request(:post, @dynamo_url).
61
+ to_return(:body => "{\"Key\":{\"HashKeyElement\":{\"S\":\"my-key\"}},\"TableName\":\"table\"}")
62
+
63
+ response = @storage.set("my-key", "my-value")
64
+ assert_requested(:post, @dynamo_url) do |req|
65
+ body = MultiJson.decode(req.body)
66
+ body == {"Item"=>{"id"=>{"S"=>"my-key"}, "value"=>{"S"=>"my-value"}}, "TableName"=>"table"} &&
67
+ req.headers["X-Amz-Target"] == "DynamoDB_20120810.PutItem"
68
+ end
69
+ assert response
70
+ end
71
+ end
72
+
73
+ describe "#del" do
74
+ it "sends a valid post when called" do
75
+ stub_request(:post, @dynamo_url).
76
+ to_return(:body => "{\"ConsumedCapacityUnits\":1.0}")
77
+
78
+ response = @storage.del("my-key")
79
+ assert_requested(:post, @dynamo_url) do |req|
80
+ body = MultiJson.decode(req.body)
81
+ body == {"Key"=>{"HashKeyElement"=>{"S"=>"my-key"}}, "TableName"=>"table"} &&
82
+ req.headers["X-Amz-Target"] == "DynamoDB_20111205.DeleteItem"
83
+ end
84
+ assert response
85
+ end
86
+ end
87
+ # ...
88
+ end
89
+
@@ -0,0 +1,12 @@
1
+ require "rubygems"
2
+ require "bundler"
3
+
4
+ Bundler.require(:default, :development)
5
+
6
+ require "webmock/rspec"
7
+
8
+ require "rollout/dynamodb/storage"
9
+
10
+ RSpec.configure do |config|
11
+ config.expect_with :test_unit
12
+ end
@@ -0,0 +1,8 @@
1
+ require "spec_helper"
2
+
3
+ describe Rollout::DynamoDB do
4
+ it "has a VERSION constant set to a string" do
5
+ Rollout::DynamoDB::VERSION.is_a?(String)
6
+ end
7
+ end
8
+
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rollout-dynamodb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Charles Hooper
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: foreman
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.77'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.77'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.20'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.20'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rollout
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '2.0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '2.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: fog
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.28'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.28'
111
+ description: DynamoDB storage adapter for Rollout
112
+ email:
113
+ - charles@heroku.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - Gemfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - lib/rollout-dynamodb.rb
126
+ - lib/rollout/dynamodb/storage.rb
127
+ - rollout-dynamodb.gemspec
128
+ - spec/dynamodb_spec.rb
129
+ - spec/spec_helper.rb
130
+ - spec/version_spec.rb
131
+ homepage: https://github.com/chooper/rollout-dynamodb
132
+ licenses:
133
+ - MIT
134
+ metadata: {}
135
+ post_install_message:
136
+ rdoc_options: []
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ requirements: []
150
+ rubyforge_project:
151
+ rubygems_version: 2.2.2
152
+ signing_key:
153
+ specification_version: 4
154
+ summary: DynamoDB storage adapter for Rollout
155
+ test_files:
156
+ - spec/dynamodb_spec.rb
157
+ - spec/spec_helper.rb
158
+ - spec/version_spec.rb