alephant-storage 1.0.0

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: eba0e664cd299e269713a3583951e006b67cae4c
4
+ data.tar.gz: d106ca2ef7faaaf819aa0ed95638f847ccae9450
5
+ SHA512:
6
+ metadata.gz: 83f1ff920e034f195e8b946e6fbad9ab9c15daa2189f29d2427d22b6faf7bc512954ffaf6a79fca6ad7a09ceeb40a80231ba192485c62de497e2080b6883affe
7
+ data.tar.gz: 647605f9c7a5e6bd27697f5fe2ef6afa8c6b7dda9d0b1ffbc89f01d6768b9281210bb2d8262a241254a721729b609c26822857d3ed0ad24b376254057a4d345a
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /config/*.yaml
2
+ /config/*.yml
3
+ Gemfile.lock
4
+ .rspec
5
+ *.gem
6
+
7
+ /pkg
8
+ /tmp
9
+ /components
10
+
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ jruby-1.7.17
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - "jruby"
4
+ notifications:
5
+ email:
6
+ recipients:
7
+ - DigitalNewsFrameworksTeam@bbc.co.uk
8
+ on_failure: change
9
+ on_success: never
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,6 @@
1
+ guard 'rspec' do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/.+\.rb$})
4
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
5
+ watch('spec/spec_helper.rb') { "spec" }
6
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Robert Kenny
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,42 @@
1
+ # Alephant::Storage
2
+
3
+ Simple abstraction layer over S3 for get/put.
4
+
5
+ [![Build
6
+ Status](https://travis-ci.org/BBC-News/alephant-storage.png)](https://travis-ci.org/BBC-News/alephant-storage)
7
+
8
+ [![Gem Version](https://badge.fury.io/rb/alephant-storage.png)](http://badge.fury.io/rb/alephant-storage)
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'alephant-storage'
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install alephant-storage
23
+
24
+ ## Usage
25
+
26
+ ```rb
27
+ require 'alephant/storage'
28
+
29
+ storage = Alephant::Storage.new('bucket_id', 'base/path')
30
+ storage.put('id', "string data")
31
+ storage.get('id')
32
+
33
+ # => "string data"
34
+ ```
35
+
36
+ ## Contributing
37
+
38
+ 1. Fork it ( http://github.com/BBC-News/alephant-storage/fork )
39
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
40
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
41
+ 4. Push to the branch (`git push origin my-new-feature`)
42
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), 'lib')
2
+
3
+ require 'rspec/core/rake_task'
4
+ require 'bundler/gem_tasks'
5
+ require 'alephant/storage'
6
+
7
+ RSpec::Core::RakeTask.new(:spec)
8
+
9
+ task :default => :spec
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'alephant/storage/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "alephant-storage"
8
+ spec.version = Alephant::Storage::VERSION
9
+ spec.authors = ["BBC News"]
10
+ spec.email = ["FutureMediaNewsRubyGems@bbc.co.uk"]
11
+ spec.summary = "Simple abstraction layer over S3 for get/put."
12
+ spec.homepage = "https://github.com/BBC-News/alephant-storage"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "rspec"
21
+ spec.add_development_dependency "rspec-nc"
22
+ spec.add_development_dependency "guard"
23
+ spec.add_development_dependency "guard-rspec"
24
+ spec.add_development_dependency "pry"
25
+ spec.add_development_dependency "pry-remote"
26
+ spec.add_development_dependency "pry-nav"
27
+ spec.add_development_dependency "bundler", "~> 1.5"
28
+ spec.add_development_dependency "rake"
29
+
30
+ spec.add_runtime_dependency 'aws-sdk', '~> 1.0'
31
+ spec.add_runtime_dependency 'alephant-logger'
32
+ end
@@ -0,0 +1,74 @@
1
+ require 'alephant/storage/version'
2
+ require 'alephant/logger'
3
+ require 'aws-sdk'
4
+
5
+ module Alephant
6
+ class Storage
7
+ include Logger
8
+ attr_reader :id, :bucket, :path
9
+
10
+ def initialize(id, path)
11
+ @id = id
12
+ @path = path
13
+ @bucket = AWS::S3.new.buckets[id]
14
+
15
+ logger.info(
16
+ "event" => "StorageInitialized",
17
+ "id" => id,
18
+ "path" => path,
19
+ "method" => "#{self.class}#initialize",
20
+ )
21
+ end
22
+
23
+ def clear
24
+ bucket.objects.with_prefix(path).delete_all
25
+ logger.info(
26
+ "event" => "StorageCleared",
27
+ "path" => path,
28
+ "method" => "#{self.class}#clear"
29
+ )
30
+ end
31
+
32
+ def put(id, data, content_type = 'text/plain', meta = {})
33
+ bucket.objects["#{path}/#{id}"].write(
34
+ data,
35
+ {
36
+ :content_type => content_type,
37
+ :metadata => meta
38
+ }
39
+ )
40
+
41
+ logger.metric "StoragePuts"
42
+ logger.info(
43
+ "event" => "StorageObjectStored",
44
+ "path" => path,
45
+ "id" => id,
46
+ "method" => "#{self.class}#put"
47
+ )
48
+ end
49
+
50
+ def get(id)
51
+ object = bucket.objects["#{path}/#{id}"]
52
+ content = object.read
53
+ content_type = object.content_type
54
+ meta_data = object.metadata.to_h
55
+
56
+ logger.metric "StorageGets"
57
+ logger.info(
58
+ "event" => "StorageObjectRetrieved",
59
+ "path" => path,
60
+ "id" => id,
61
+ "content" => content,
62
+ "contentType" => content_type,
63
+ "metadata" => meta_data,
64
+ "method" => "#{self.class}#get"
65
+ )
66
+
67
+ {
68
+ :content => content,
69
+ :content_type => content_type,
70
+ :meta => meta_data
71
+ }
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,5 @@
1
+ module Alephant
2
+ class Storage
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ $: << File.join(File.dirname(__FILE__),"..", "lib")
2
+
3
+ require 'pry'
4
+ require 'alephant/storage'
5
+
@@ -0,0 +1,96 @@
1
+ require 'spec_helper'
2
+ require 'pry'
3
+
4
+ describe Alephant::Storage do
5
+ let(:id) { :id }
6
+ let(:path) { :path }
7
+ let(:data) { :data }
8
+ subject { Alephant::Storage }
9
+
10
+ describe "initialize(id, path)" do
11
+ it "sets and exposes id, path instance variables " do
12
+ instance = subject.new(id, path)
13
+ expect(instance.id).to eq(id)
14
+ expect(instance.path).to eq(path)
15
+ end
16
+
17
+ it "sets bucket instance variable as S3 bucket with id" do
18
+ instance = subject.new(id, path)
19
+
20
+ expect(instance.bucket).to be_an AWS::S3::Bucket
21
+ expect(instance.bucket.name).to eq('id')
22
+ end
23
+ end
24
+
25
+ describe "clear" do
26
+ let(:num_object_in_path) { 100 }
27
+ it "deletes all objects for a path" do
28
+ filtered_object_collection = double()
29
+ expect(filtered_object_collection)
30
+ .to receive(:delete_all)
31
+
32
+ s3_object_collection = double()
33
+ expect(s3_object_collection)
34
+ .to receive(:with_prefix)
35
+ .with(path)
36
+ .and_return(filtered_object_collection)
37
+
38
+ s3_bucket = double()
39
+ expect(s3_bucket).to receive(:objects).and_return(s3_object_collection)
40
+
41
+ expect_any_instance_of(AWS::S3).to receive(:buckets).and_return({ id => s3_bucket })
42
+
43
+ instance = subject.new(id, path)
44
+ instance.clear
45
+ end
46
+ end
47
+
48
+ describe "put(id, data)" do
49
+ it "sets bucket path/id content data" do
50
+ s3_object_collection = double()
51
+ expect(s3_object_collection).to receive(:write).with(:data, { :content_type => 'foo/bar', :metadata=>{} })
52
+
53
+ s3_bucket = double()
54
+ expect(s3_bucket).to receive(:objects).and_return(
55
+ {
56
+ "path/id" => s3_object_collection
57
+ }
58
+ )
59
+
60
+ expect_any_instance_of(AWS::S3).to receive(:buckets).and_return({ id => s3_bucket })
61
+ instance = subject.new(id, path)
62
+
63
+ instance.put(id, data, 'foo/bar')
64
+ end
65
+ end
66
+
67
+ describe "get(id)" do
68
+ it "gets bucket path/id content data" do
69
+ s3_object_collection = double()
70
+ expect(s3_object_collection).to receive(:read).and_return("content")
71
+ expect(s3_object_collection).to receive(:content_type).and_return("foo/bar")
72
+ expect(s3_object_collection).to receive(:metadata).and_return({ :foo => :bar})
73
+
74
+ s3_bucket = double()
75
+ expect(s3_bucket).to receive(:objects).and_return(
76
+ {
77
+ "path/id" => s3_object_collection
78
+ }
79
+ )
80
+
81
+ expect_any_instance_of(AWS::S3).to receive(:buckets).and_return({ id => s3_bucket })
82
+
83
+ instance = subject.new(id, path)
84
+ object_hash = instance.get(id)
85
+
86
+ expected_hash = {
87
+ :content => "content",
88
+ :content_type => "foo/bar",
89
+ :meta => { :foo => :bar }
90
+ }
91
+
92
+ expect(object_hash).to eq(expected_hash)
93
+ end
94
+ end
95
+ end
96
+
metadata ADDED
@@ -0,0 +1,213 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: alephant-storage
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - BBC News
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - '>='
17
+ - !ruby/object:Gem::Version
18
+ version: '0'
19
+ name: rspec
20
+ prerelease: false
21
+ type: :development
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ name: rspec-nc
34
+ prerelease: false
35
+ type: :development
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ name: guard
48
+ prerelease: false
49
+ type: :development
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ name: guard-rspec
62
+ prerelease: false
63
+ type: :development
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ name: pry
76
+ prerelease: false
77
+ type: :development
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ requirement: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ name: pry-remote
90
+ prerelease: false
91
+ type: :development
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ requirement: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ name: pry-nav
104
+ prerelease: false
105
+ type: :development
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ requirement: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ~>
115
+ - !ruby/object:Gem::Version
116
+ version: '1.5'
117
+ name: bundler
118
+ prerelease: false
119
+ type: :development
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ~>
123
+ - !ruby/object:Gem::Version
124
+ version: '1.5'
125
+ - !ruby/object:Gem::Dependency
126
+ requirement: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - '>='
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ name: rake
132
+ prerelease: false
133
+ type: :development
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ requirement: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ~>
143
+ - !ruby/object:Gem::Version
144
+ version: '1.0'
145
+ name: aws-sdk
146
+ prerelease: false
147
+ type: :runtime
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ~>
151
+ - !ruby/object:Gem::Version
152
+ version: '1.0'
153
+ - !ruby/object:Gem::Dependency
154
+ requirement: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - '>='
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ name: alephant-logger
160
+ prerelease: false
161
+ type: :runtime
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - '>='
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ description:
168
+ email:
169
+ - FutureMediaNewsRubyGems@bbc.co.uk
170
+ executables: []
171
+ extensions: []
172
+ extra_rdoc_files: []
173
+ files:
174
+ - .gitignore
175
+ - .ruby-version
176
+ - .travis.yml
177
+ - Gemfile
178
+ - Guardfile
179
+ - LICENSE.txt
180
+ - README.md
181
+ - Rakefile
182
+ - alephant-cache.gemspec
183
+ - lib/alephant/storage.rb
184
+ - lib/alephant/storage/version.rb
185
+ - spec/spec_helper.rb
186
+ - spec/storage_spec.rb
187
+ homepage: https://github.com/BBC-News/alephant-storage
188
+ licenses:
189
+ - MIT
190
+ metadata: {}
191
+ post_install_message:
192
+ rdoc_options: []
193
+ require_paths:
194
+ - lib
195
+ required_ruby_version: !ruby/object:Gem::Requirement
196
+ requirements:
197
+ - - '>='
198
+ - !ruby/object:Gem::Version
199
+ version: '0'
200
+ required_rubygems_version: !ruby/object:Gem::Requirement
201
+ requirements:
202
+ - - '>='
203
+ - !ruby/object:Gem::Version
204
+ version: '0'
205
+ requirements: []
206
+ rubyforge_project:
207
+ rubygems_version: 2.1.9
208
+ signing_key:
209
+ specification_version: 4
210
+ summary: Simple abstraction layer over S3 for get/put.
211
+ test_files:
212
+ - spec/spec_helper.rb
213
+ - spec/storage_spec.rb