vines-mongomapper 0.1.0

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 ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NWZiY2MwZWY1NDhkYjAyYzQ2YWVmODY4NWEwZjIxODJhYThmN2NjNg==
5
+ data.tar.gz: !binary |-
6
+ YzYxNTk5YmRiZjA3OTA5Zjk3NWU4NjVlYjE0OTc4NmQ3YTI1ODJlNg==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ YWRkNDAyOTdjZWVhOTczMTU5N2QxZTcyYjQyYTZjNDU4OWYzM2UwMjBiYzc2
10
+ OTcxNGU1MmIzZDU2ZTA3MGVkMWE2ZWM2Y2Y4YzA3MTI0NDljN2U2MjgwMzA4
11
+ ODk3YTE3MmE3NGNkMGMyMDhiMTVmMDg1MjI4ZjhhZWQxZWQ0M2I=
12
+ data.tar.gz: !binary |-
13
+ NjA3YTYyOTEwNTVhNGNiNTViODk0NDhiY2I1ODU0ZDgwNDA2N2NkZjY5YThi
14
+ Y2E5ZDVlZTc1MzNjMmVmOTI3ZWI2NTU2MjI2ZjUwZjFjNmM3ODBlZWE2NjQ3
15
+ YTUyNzY2ZTM5MDMyMDMyMDRiYjJkYzE0ZTkyNDg1MTdhN2E5MmI=
data/.gitignore ADDED
@@ -0,0 +1,19 @@
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
18
+ .idea
19
+
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 1.9.3-p392
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'mongo_mapper'
6
+ group :test do
7
+ gem 'database_cleaner'
8
+ gem 'rspec'
9
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 sawyerzhang
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,71 @@
1
+
2
+ # Welcome to Vines-Mongompper
3
+
4
+ Vines is a scalable XMPP chat server, using EventMachine for asynchronous IO.
5
+
6
+ This gem is inspired by another mongo storage for Vines: [vines-mongodb](https://github.com/negativecode/vines-mongodb.git).
7
+
8
+ And provides more:
9
+
10
+ 1. Use MongoMapper to model Resources.
11
+ 2. Use BOSN ObjectID to id a resource instead of using a JID. This makes integration with frameworks like Rails much easier.
12
+ 3. Test with real Mongodb, instead of Mock, this makes it more flexible to develop and extend
13
+
14
+
15
+ ## Assumptions
16
+
17
+ This storage has few assumptions:
18
+
19
+ 1. User is identified by his BSON id and user name, keyed by _id and user_name respectively.
20
+ 2. User can use _id@domain and user_name@domain as jid to login.
21
+
22
+ ## Usage
23
+
24
+ ```
25
+ $ gem install vines vines-mongomapper
26
+ $ vines init wonderland.lit
27
+ $ cd wonderland.lit && vines start
28
+ ```
29
+
30
+ ## Configuration
31
+
32
+ Add the following configuration block to a virtual host definition in
33
+ the server's `conf/config.rb` file.
34
+
35
+ ```ruby
36
+ storage 'mongomapper' do
37
+ host 'localhost', 27017
38
+ host 'localhost', 27018 # optional, connects to replica set
39
+ database 'xmpp'
40
+ tls true
41
+ username ''
42
+ password ''
43
+ pool 5
44
+ end
45
+ ```
46
+
47
+ ## Development
48
+
49
+ ```
50
+ $ bundle install
51
+ $ bundle exec rspec spec
52
+ ```
53
+
54
+ ## Contributing
55
+
56
+ 1. Fork it
57
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
58
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
59
+ 4. Push to the branch (`git push origin my-new-feature`)
60
+ 5. Create new Pull Request
61
+
62
+
63
+ ## Contact
64
+
65
+ * Sawyer ZHANG <sawyerzhangdev@gmeil.com>
66
+
67
+ ## License
68
+
69
+ Vines is released under the MIT license. Check the LICENSE file for details.
70
+
71
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ module Vines
2
+ class User
3
+
4
+
5
+ include MongoMapper::Document
6
+ set_collection_name "users"
7
+ key :name, String #display name
8
+ key :user_name,String # jid node
9
+ key :password,String
10
+
11
+
12
+
13
+ attr_accessor :jid,:roster
14
+
15
+
16
+ def initialize(args={})
17
+ @name = args[:name]
18
+ @user_name = args[:user_name]
19
+ @password = args[:password]
20
+ @roster = args[:roster] || []
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,153 @@
1
+ require_relative 'model/user'
2
+
3
+
4
+ module Vines
5
+ class Storage
6
+ class Mongomapper < Storage
7
+ register :mongomapper
8
+
9
+ %w[database tls username password pool].each do |name|
10
+ define_method(name) do |*args|
11
+ if args.first
12
+ @config[name.to_sym] = args.first
13
+ else
14
+ @config[name.to_sym]
15
+ end
16
+ end
17
+ end
18
+
19
+ def initialize(&block)
20
+ @config, @hosts = {}, []
21
+ instance_eval(&block)
22
+ raise "Must provide database" unless @config[:database]
23
+ raise "Must provide at least one host connection" if @hosts.empty?
24
+ connect
25
+ end
26
+
27
+ def host(name, port)
28
+ pair = [name, port]
29
+ raise "duplicate hosts not allowed: #{name}:#{port}" if @hosts.include?(pair)
30
+ @hosts << pair
31
+ end
32
+
33
+ def find_user(jid)
34
+
35
+
36
+ jid = JID.new(jid).bare.to_s
37
+ return if jid.empty?
38
+
39
+ #find a usr by it's bson id
40
+ u = Vines::User.where(:_id=>jid_to_bson_id(jid)).first
41
+
42
+ # find a user by it's name
43
+ user_name = jid.split('@')[0]
44
+ u = Vines::User.where(:user_name=>user_name).first unless u
45
+ u.jid = JID.new(jid)if u
46
+ u.roster=[] if u
47
+ u
48
+
49
+ end
50
+ #
51
+ # def save_user(user)
52
+ #
53
+ # jid = user.jid.bare.to_s
54
+ # user_name = user.user_name
55
+ #
56
+ # #find a user by name create a new if failed
57
+ # u = Vines::User.where(:user_name=>user_name).first
58
+ # u = Vines::User.new(jid:user.jid) unless u
59
+ #
60
+ #
61
+ # u.email = jid
62
+ # u.name = user.name
63
+ # u.user_name = user_name
64
+ # u.password = user.password
65
+ #
66
+ # # doc['roster'] = {}
67
+ # # user.roster.each do |contact|
68
+ # # doc['roster'][contact.jid.bare.to_s] = contact.to_h
69
+ # # end
70
+ # save(u)
71
+ #
72
+ #
73
+ #
74
+ #
75
+ #
76
+ #
77
+ # end
78
+
79
+ def find_vcard(jid)
80
+
81
+ end
82
+
83
+ def save_vcard(jid, card)
84
+
85
+ end
86
+
87
+ def find_fragment(jid, node)
88
+
89
+ end
90
+
91
+ def save_fragment(jid, node)
92
+
93
+ end
94
+
95
+
96
+ def get
97
+
98
+ end
99
+
100
+
101
+ def save(model)
102
+ model.save!
103
+ end
104
+
105
+ private
106
+
107
+ def fragment_id(jid, node)
108
+ id = Digest::SHA1.hexdigest("#{node.name}:#{node.namespace.href}")
109
+ "#{jid}:#{id}"
110
+ end
111
+
112
+
113
+ defer :get
114
+
115
+
116
+ defer :save
117
+
118
+
119
+
120
+
121
+ def jid_to_bson_id(jid)
122
+ id = jid.split('@')[0]
123
+ BSON::ObjectId(id) rescue nil
124
+ end
125
+
126
+ def connect
127
+
128
+ opts = {
129
+ pool_timeout: 5,
130
+ pool_size: @config[:pool] || 5,
131
+ ssl: @config[:tls]
132
+ }
133
+
134
+
135
+
136
+ conn = if @hosts.size == 1
137
+ Mongo::Connection.new(@hosts.first[0], @hosts.first[1], opts)
138
+ else
139
+ Mongo::ReplSetConnection.new(*@hosts, opts)
140
+ end
141
+
142
+ # conn.db(@config[:database]).tap do |db|
143
+ # user = @config[:username] || ''
144
+ # pass = @config[:password] || ''
145
+ # db.authenticate(user, pass) unless user.empty? || pass.empty?
146
+ # end
147
+ # MongoMapper.connection = conn
148
+ MongoMapper.database = @config[:database]
149
+ end
150
+
151
+ end
152
+ end
153
+ end
@@ -0,0 +1,34 @@
1
+ # This file is copied to spec/ when you run 'rails generate rspec:install'
2
+ ENV["RAILS_ENV"] ||= 'test'
3
+ require 'database_cleaner'
4
+ require 'mongo_mapper'
5
+ require 'vines'
6
+ require_relative '../lib/vines/mongomapper'
7
+
8
+ # require 'rspec/autorun'
9
+
10
+ RSpec.configure do |config|
11
+ # ## Mock Framework
12
+ #
13
+ # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
14
+ #
15
+ # config.mock_with :mocha
16
+ # config.mock_with :flexmock
17
+ # config.mock_with :rr
18
+
19
+ # Run specs in random order to surface order dependencies. If you find an
20
+ # order dependency and want to debug it, you can fix the order by providing
21
+ # the seed, which is printed after each run.
22
+ # --seed 1234
23
+ config.order = "random"
24
+ config.before :each do
25
+ DatabaseCleaner.strategy = :truncation
26
+ DatabaseCleaner.start
27
+ end
28
+
29
+ config.after do
30
+ DatabaseCleaner.clean
31
+ end
32
+ end
33
+
34
+
@@ -0,0 +1,77 @@
1
+ require 'spec_helper'
2
+
3
+ def storage
4
+ storage = Vines::Storage::Mongomapper.new do
5
+ host 'localhost', 27017
6
+ database 'vines_mongo_rails_test'
7
+ end
8
+ def storage.db
9
+ MOCK_MONGO
10
+ end
11
+ storage
12
+ end
13
+
14
+ def fibered
15
+ EM.run do
16
+ Fiber.new do
17
+ yield
18
+ EM.stop
19
+ end.resume
20
+ end
21
+ end
22
+
23
+ describe 'User' do
24
+
25
+
26
+ it 'should save a user' do
27
+
28
+ fibered do
29
+ db = storage
30
+ user = Vines::User.new(
31
+ jid: 'saved_user@domain.tld/resource1',
32
+ name: 'Save User',
33
+ user_name:'saved_user',
34
+ password: 'secret')
35
+ user.roster << Vines::Contact.new(
36
+ jid: 'contact1@domain.tld/resource2',
37
+ name: 'Contact 1')
38
+
39
+ user.save!
40
+ user = db.find_user('saved_user@domain.tld')
41
+ expect(user.jid.to_s).to eq 'saved_user@domain.tld'
42
+ expect(user.name).to eq('Save User')
43
+ expect(user.user_name).to eq('saved_user')
44
+
45
+
46
+ # user.roster.length.must_equal 1
47
+ # user.roster[0].jid.to_s.must_equal 'contact1@domain.tld'
48
+ # user.roster[0].name.must_equal 'Contact 1'
49
+ end
50
+
51
+ end
52
+
53
+
54
+ it 'should auth a user' do
55
+
56
+ fibered do
57
+ db = storage
58
+ Vines::User.create({jid: 'empty@wonderland.lit',user_name:'empty'})
59
+ Vines::User.create({jid: 'no_password@wonderland.lit',user_name:'no_password', 'foo' => 'bar'})
60
+ Vines::User.create({jid: 'clear_password@wonderland.lit',user_name:'clear_password', password:'secret'})
61
+ Vines::User.create({jid: 'bcrypt_password@wonderland.lit',user_name:'bcrypt_password',name:'a good user', password: BCrypt::Password.create('secret')})
62
+
63
+ expect(db.authenticate(nil, nil)).to be_nil
64
+ expect(db.authenticate(nil,'secret' )).to be_nil
65
+ expect(db.authenticate('bogus', nil)).to be_nil
66
+ expect(db.authenticate('bogus', 'secret')).to be_nil
67
+ expect(db.authenticate('empty@wonderland.lit', 'secret')).to be_nil
68
+ expect(db.authenticate('no_password@wonderland.lit', 'secret')).to be_nil
69
+ expect(db.authenticate('clear_password@wonderland.lit', 'secret')).to be_nil
70
+
71
+ user = db.authenticate('bcrypt_password@wonderland.lit', 'secret')
72
+ expect(user.name).to eq('a good user')
73
+ expect(user.jid.to_s).to eq 'bcrypt_password@wonderland.lit'
74
+
75
+ end
76
+ end
77
+ 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
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "vines-mongomapper"
7
+ spec.version = "0.1.0"
8
+ spec.authors = ["sawyerzhang"]
9
+ spec.email = ["sawyerzhangdev@gmail.com"]
10
+ spec.summary = %q[Provides a MongoMapper storage adapter for the Vines XMPP chat server.]
11
+ spec.description = %q[Stores Vines user data in MongoMapper.]
12
+
13
+ spec.homepage = ""
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_dependency 'mongo_mapper'
22
+ spec.add_dependency 'bson_ext'
23
+ spec.add_dependency 'vines', '>= 0.4.5'
24
+
25
+ spec.add_development_dependency "bundler"
26
+ spec.add_development_dependency "rake"
27
+ spec.required_ruby_version = '>= 1.9.3'
28
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vines-mongomapper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - sawyerzhang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mongo_mapper
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bson_ext
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: vines
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: 0.4.5
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: 0.4.5
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Stores Vines user data in MongoMapper.
84
+ email:
85
+ - sawyerzhangdev@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - .ruby-version
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - lib/vines/model/user.rb
97
+ - lib/vines/mongomapper.rb
98
+ - spec/spec_helper.rb
99
+ - spec/storage_spec.rb
100
+ - vines-mongomapper.gemspec
101
+ homepage: ''
102
+ licenses:
103
+ - MIT
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ! '>='
112
+ - !ruby/object:Gem::Version
113
+ version: 1.9.3
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ! '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.1.11
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: Provides a MongoMapper storage adapter for the Vines XMPP chat server.
125
+ test_files:
126
+ - spec/spec_helper.rb
127
+ - spec/storage_spec.rb