dragonfly-mongo_data_store 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: 1c43fa4eab4ad2e74095650d1eb9e82725d45428
4
+ data.tar.gz: 7663d2bfc58ce2d652d9b5ca3b5156fc521d282c
5
+ SHA512:
6
+ metadata.gz: 2f5820727acf128a5a930404c99955b7ab81e3290a1dfa63deec3a529e650c92fa7ac463bddac91d08600e993e611b6c38ad81a8a9cfdc94f66f42a76dd1669a
7
+ data.tar.gz: be43db014ca8ba243fd35d3ed2799e490f789caba75e92c02a734b364e6353ae363edc4c4d5cde2db803ec8ee41ba3a00fa7092e3de32cdbe4f59cb679ff7e56
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Mark Evans
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,44 @@
1
+ # Dragonfly::MongoDataStore
2
+
3
+ Mongo data store for use with the [Dragonfly](http://github.com/markevans/dragonfly) gem.
4
+
5
+ ## Gemfile
6
+
7
+ ```ruby
8
+ gem 'dragonfly-mongo_data_store'
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ In your dragonfly config block (with default options):
14
+
15
+ ```ruby
16
+ Dragonfly.app.configure do
17
+ # ...
18
+
19
+ datastore :mongo
20
+
21
+ # ...
22
+ end
23
+ ```
24
+
25
+ Or with options:
26
+
27
+ ```ruby
28
+ datastore :mongo, host: 'my.host', database: 'my_database'
29
+ ```
30
+
31
+ ### Available options
32
+
33
+ ```ruby
34
+ :host # e.g. 'my.domain'
35
+ :hosts # for replica sets, e.g. ['n1.mydb.net:27017', 'n2.mydb.net:27017']
36
+ :connection_opts # hash that passes through to Mongo::Connection or Mongo::ReplSetConnection
37
+ :port # e.g. 27017
38
+ :database # defaults to 'dragonfly'
39
+ :username
40
+ :password
41
+ :connection # use this if you already have a Mongo::Connection object
42
+ :db # use this if you already have a Mongo::DB object
43
+ ```
44
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dragonfly/mongo_data_store/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dragonfly-mongo_data_store"
8
+ spec.version = Dragonfly::MongoDataStore::VERSION
9
+ spec.authors = ["Mark Evans"]
10
+ spec.email = ["mark@new-bamboo.co.uk"]
11
+ spec.description = %q{Mongo data store for Dragonfly}
12
+ spec.summary = %q{Data store for storing content (e.g. images) handled with the Dragonfly gem in a mongo database.}
13
+ spec.homepage = "http://github.com/markevans/dragonfly-mongo_data_store"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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_runtime_dependency "dragonfly", "~> 1.0"
22
+ spec.add_runtime_dependency "mongo", "~> 1.7"
23
+ spec.add_development_dependency "rspec", "~> 2.0"
24
+ end
25
+
@@ -0,0 +1,86 @@
1
+ require 'mongo'
2
+ require 'dragonfly'
3
+
4
+ Dragonfly::App.register_datastore(:mongo){ Dragonfly::MongoDataStore }
5
+
6
+ module Dragonfly
7
+ class MongoDataStore
8
+
9
+ include Serializer
10
+
11
+ def initialize(opts={})
12
+ @host = opts[:host]
13
+ @hosts = opts[:hosts]
14
+ @connection_opts = opts[:connection_opts] || {}
15
+ @port = opts[:port]
16
+ @database = opts[:database] || 'dragonfly'
17
+ @username = opts[:username]
18
+ @password = opts[:password]
19
+ @connection = opts[:connection]
20
+ @db = opts[:db]
21
+ end
22
+
23
+ attr_accessor :host, :hosts, :connection_opts, :port, :database, :username, :password
24
+
25
+ def write(content, opts={})
26
+ ensure_authenticated!
27
+ content.file do |f|
28
+ mongo_id = grid.put(f, :content_type => content.mime_type, :metadata => content.meta)
29
+ mongo_id.to_s
30
+ end
31
+ end
32
+
33
+ def read(uid)
34
+ ensure_authenticated!
35
+ grid_io = grid.get(bson_id(uid))
36
+ meta = extract_meta(grid_io)
37
+ [grid_io.read, meta]
38
+ rescue Mongo::GridFileNotFound, BSON::InvalidObjectId => e
39
+ nil
40
+ end
41
+
42
+ def destroy(uid)
43
+ ensure_authenticated!
44
+ grid.delete(bson_id(uid))
45
+ rescue Mongo::GridFileNotFound, BSON::InvalidObjectId => e
46
+ Dragonfly.warn("#{self.class.name} destroy error: #{e}")
47
+ end
48
+
49
+ def connection
50
+ @connection ||= if hosts
51
+ Mongo::ReplSetConnection.new(hosts, connection_opts)
52
+ else
53
+ Mongo::Connection.new(host, port, connection_opts)
54
+ end
55
+ end
56
+
57
+ def db
58
+ @db ||= connection.db(database)
59
+ end
60
+
61
+ def grid
62
+ @grid ||= Mongo::Grid.new(db)
63
+ end
64
+
65
+ private
66
+
67
+ def ensure_authenticated!
68
+ if username
69
+ @authenticated ||= db.authenticate(username, password)
70
+ end
71
+ end
72
+
73
+ def bson_id(uid)
74
+ BSON::ObjectId.from_string(uid)
75
+ end
76
+
77
+ def extract_meta(grid_io)
78
+ meta = grid_io.metadata
79
+ meta = Utils.stringify_keys(marshal_b64_decode(meta)) if meta.is_a?(String) # Deprecated encoded meta
80
+ meta.merge!('stored_at' => grid_io.upload_date)
81
+ meta
82
+ end
83
+
84
+ end
85
+ end
86
+
@@ -0,0 +1,6 @@
1
+ module Dragonfly
2
+ class MongoDataStore
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
6
+
@@ -0,0 +1,96 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+ require 'dragonfly/mongo_data_store'
4
+ require 'dragonfly/spec/data_store_examples'
5
+ require 'mongo'
6
+
7
+ describe Dragonfly::MongoDataStore do
8
+
9
+ let(:app) { Dragonfly.app }
10
+ let(:content) { Dragonfly::Content.new(app, "Pernumbucano") }
11
+ let(:new_content) { Dragonfly::Content.new(app) }
12
+
13
+ before(:each) do
14
+ @data_store = Dragonfly::MongoDataStore.new :database => 'dragonfly_test'
15
+ end
16
+
17
+ describe "configuring the app" do
18
+ it "can be configured with a symbol" do
19
+ app.configure do
20
+ datastore :mongo
21
+ end
22
+ app.datastore.should be_a(Dragonfly::MongoDataStore)
23
+ end
24
+ end
25
+
26
+ it_should_behave_like 'data_store'
27
+
28
+ describe "connecting to a replica set" do
29
+ it "should initiate a replica set connection if hosts is set" do
30
+ @data_store.hosts = ['1.2.3.4:27017', '1.2.3.4:27017']
31
+ @data_store.connection_opts = {:name => 'testingset'}
32
+ Mongo::ReplSetConnection.should_receive(:new).with(['1.2.3.4:27017', '1.2.3.4:27017'], :name => 'testingset')
33
+ @data_store.connection
34
+ end
35
+ end
36
+
37
+ describe "authenticating" do
38
+ it "should not attempt to authenticate if a username is not given" do
39
+ @data_store.db.should_not_receive(:authenticate)
40
+ @data_store.write(content)
41
+ end
42
+
43
+ it "should attempt to authenticate once if a username is given" do
44
+ @data_store.username = 'terry'
45
+ @data_store.password = 'butcher'
46
+ @data_store.db.should_receive(:authenticate).exactly(:once).with('terry','butcher').and_return(true)
47
+ uid = @data_store.write(content)
48
+ @data_store.read(uid)
49
+ end
50
+ end
51
+
52
+ describe "sharing already configured stuff" do
53
+ before(:each) do
54
+ @connection = Mongo::Connection.new
55
+ end
56
+
57
+ it "should allow sharing the connection" do
58
+ data_store = Dragonfly::MongoDataStore.new :connection => @connection
59
+ @connection.should_receive(:db).and_return(db=double)
60
+ data_store.db.should == db
61
+ end
62
+
63
+ it "should allow sharing the db" do
64
+ db = @connection.db('dragonfly_test_yo')
65
+ data_store = Dragonfly::MongoDataStore.new :db => db
66
+ data_store.grid.instance_eval{@db}.should == db # so wrong
67
+ end
68
+ end
69
+
70
+ describe "content type" do
71
+ it "should serve straight from mongo with the correct content type (taken from ext)" do
72
+ content.name = 'text.txt'
73
+ uid = @data_store.write(content)
74
+ response = @data_store.grid.get(BSON::ObjectId(uid))
75
+ response.content_type.should == 'text/plain'
76
+ response.read.should == content.data
77
+ end
78
+ end
79
+
80
+ describe "already stored stuff" do
81
+ it "still works" do
82
+ uid = @data_store.grid.put("DOOBS", :metadata => {'some' => 'meta'}).to_s
83
+ new_content.update(*@data_store.read(uid))
84
+ new_content.data.should == "DOOBS"
85
+ new_content.meta['some'].should == 'meta'
86
+ end
87
+
88
+ it "still works when meta was stored as a marshal dumped hash (but stringifies keys)" do
89
+ uid = @data_store.grid.put("DOOBS", :metadata => Dragonfly::Serializer.marshal_b64_encode(:some => 'stuff')).to_s
90
+ c, meta = @data_store.read(uid)
91
+ meta['some'].should == 'stuff'
92
+ end
93
+ end
94
+
95
+ end
96
+
@@ -0,0 +1,7 @@
1
+ RSpec.configure do |config|
2
+ config.treat_symbols_as_metadata_keys_with_true_values = true
3
+ config.run_all_when_everything_filtered = true
4
+ config.filter_run :focus
5
+ config.order = 'random'
6
+ end
7
+
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dragonfly-mongo_data_store
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Mark Evans
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dragonfly
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: mongo
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ description: Mongo data store for Dragonfly
56
+ email:
57
+ - mark@new-bamboo.co.uk
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .rspec
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - dragonfly-mongo_data_store.gemspec
69
+ - lib/dragonfly/mongo_data_store.rb
70
+ - lib/dragonfly/mongo_data_store/version.rb
71
+ - spec/mongo_data_store_spec.rb
72
+ - spec/spec_helper.rb
73
+ homepage: http://github.com/markevans/dragonfly-mongo_data_store
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.1.11
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Data store for storing content (e.g. images) handled with the Dragonfly gem
97
+ in a mongo database.
98
+ test_files:
99
+ - spec/mongo_data_store_spec.rb
100
+ - spec/spec_helper.rb