upvote 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +2 -0
- data/LICENSE.txt +22 -0
- data/Rakefile +1 -0
- data/lib/upvote.rb +69 -0
- data/lib/upvote/configuration.rb +29 -0
- data/lib/upvote/version.rb +3 -0
- data/spec/spec_helper.rb +4 -0
- data/spec/upvote_spec.rb +32 -0
- data/upvote.gemspec +25 -0
- metadata +126 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2841bd9f9f689699df386e32a205c6446b678d19
|
4
|
+
data.tar.gz: 1ed2f0441b4cf7d2927025a22b183be9c72bab25
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5859477d84008de83b4d4a5b225590d8f13a7c59d6672a7c549078a5a633af04990a63db552cdba612f211fe5d2e99342158b24cc23aa6b45827cfdc460b8bb1
|
7
|
+
data.tar.gz: b5cd266a7679fcd9eeae5b8c1f9b2093062de67c995ba6a5a450a631f3184e8bf64a53476e4f779ae1a9d16e82ca6c8ddb868bf55b43adadcb7a093eb7aa18c5
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Victor Rodrigues
|
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/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
data/lib/upvote.rb
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'upvote/version'
|
2
|
+
require 'upvote/configuration'
|
3
|
+
|
4
|
+
require 'active_support/core_ext/string/inflections'
|
5
|
+
require 'active_support/core_ext/object/blank'
|
6
|
+
require 'redis'
|
7
|
+
|
8
|
+
module Upvote
|
9
|
+
extend self
|
10
|
+
extend Configuration
|
11
|
+
|
12
|
+
def create(subject, target, execute: true)
|
13
|
+
creation = ->() {
|
14
|
+
redis.zadd set_key(subject), Time.now.to_f, key(target)
|
15
|
+
redis.zadd set_key(target), Time.now.to_f, key(subject)
|
16
|
+
}
|
17
|
+
|
18
|
+
execute ? redis.multi { creation.call } : creation
|
19
|
+
end
|
20
|
+
|
21
|
+
def exist?(subject, target)
|
22
|
+
redis.zscore(set_key(target), key(subject)).present?
|
23
|
+
end
|
24
|
+
|
25
|
+
def count(entity)
|
26
|
+
redis.zcard set_key(entity)
|
27
|
+
end
|
28
|
+
|
29
|
+
def voters(entity, start: 0, offset: -1)
|
30
|
+
redis.zrevrange set_key(entity), start, offset
|
31
|
+
end
|
32
|
+
|
33
|
+
def multi_create(upvotes)
|
34
|
+
redis.multi do
|
35
|
+
upvotes.each do |subject, *targets|
|
36
|
+
targets.each do |target|
|
37
|
+
create(subject, target, execute: false).call
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def multi_count(entities)
|
44
|
+
redis.multi { entities.each &method(:count) }
|
45
|
+
end
|
46
|
+
|
47
|
+
def multi_voters(entities, start: 0, offset: -1)
|
48
|
+
redis.multi {
|
49
|
+
entities.each { |e| voters entities, start: start, offset: offset }
|
50
|
+
}
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def set_key(entity)
|
56
|
+
"#{key(entity)}:likes"
|
57
|
+
end
|
58
|
+
|
59
|
+
def key(entity)
|
60
|
+
klass = entity.class
|
61
|
+
method = id_method(klass)
|
62
|
+
'${type}#${id}' % { type: klass.to_s.underscore, id: entity.send(method) }
|
63
|
+
end
|
64
|
+
|
65
|
+
def transaction
|
66
|
+
redis.multi { yield }
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Upvote
|
2
|
+
module Configuration
|
3
|
+
|
4
|
+
def redis=(client)
|
5
|
+
@client = client
|
6
|
+
end
|
7
|
+
|
8
|
+
def redis
|
9
|
+
@client ||= Redis.new
|
10
|
+
end
|
11
|
+
|
12
|
+
def id_method(klass)
|
13
|
+
id_methods[klass]
|
14
|
+
end
|
15
|
+
|
16
|
+
def id_methods
|
17
|
+
@id_methods ||= Hash.new { |h, k| h[k] = default_id_method }
|
18
|
+
end
|
19
|
+
|
20
|
+
def default_id_method=(method)
|
21
|
+
@id_method = method
|
22
|
+
end
|
23
|
+
|
24
|
+
def default_id_method
|
25
|
+
@id_method ||= :id
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
data/spec/spec_helper.rb
ADDED
data/spec/upvote_spec.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Upvote do
|
4
|
+
class User
|
5
|
+
attr_reader :id
|
6
|
+
|
7
|
+
def initialize(id)
|
8
|
+
@id = id
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class Resource
|
13
|
+
attr_reader :id
|
14
|
+
|
15
|
+
def initialize(id)
|
16
|
+
@id = id
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '.create' do
|
21
|
+
let(:user) { User.new 21 }
|
22
|
+
let(:resource) { Resource.new 42 }
|
23
|
+
|
24
|
+
it 'creates like' do
|
25
|
+
Upvote.create user, resource
|
26
|
+
|
27
|
+
expect(Upvote.count(resource)).to eq(1)
|
28
|
+
expect(Upvote.count(user)).to eq(1)
|
29
|
+
expect(Upvote.exist?(user, resource)).to be(true)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/upvote.gemspec
ADDED
@@ -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 'upvote/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'upvote'
|
8
|
+
spec.version = Upvote::VERSION
|
9
|
+
spec.authors = ['Victor Rodrigues']
|
10
|
+
spec.email = ['victorc.rodrigues@gmail.com']
|
11
|
+
spec.summary = 'Simple upvoting done via redis'
|
12
|
+
spec.homepage = 'https://github.com/rodrigues'
|
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 'bundler', '~> 1.7'
|
21
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
22
|
+
spec.add_development_dependency 'rspec'
|
23
|
+
spec.add_dependency 'redis'
|
24
|
+
spec.add_dependency 'activesupport'
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: upvote
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Victor Rodrigues
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-12-26 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: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: redis
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
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: activesupport
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description:
|
84
|
+
email:
|
85
|
+
- victorc.rodrigues@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- Gemfile
|
92
|
+
- LICENSE.txt
|
93
|
+
- Rakefile
|
94
|
+
- lib/upvote.rb
|
95
|
+
- lib/upvote/configuration.rb
|
96
|
+
- lib/upvote/version.rb
|
97
|
+
- spec/spec_helper.rb
|
98
|
+
- spec/upvote_spec.rb
|
99
|
+
- upvote.gemspec
|
100
|
+
homepage: https://github.com/rodrigues
|
101
|
+
licenses:
|
102
|
+
- MIT
|
103
|
+
metadata: {}
|
104
|
+
post_install_message:
|
105
|
+
rdoc_options: []
|
106
|
+
require_paths:
|
107
|
+
- lib
|
108
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
requirements: []
|
119
|
+
rubyforge_project:
|
120
|
+
rubygems_version: 2.2.2
|
121
|
+
signing_key:
|
122
|
+
specification_version: 4
|
123
|
+
summary: Simple upvoting done via redis
|
124
|
+
test_files:
|
125
|
+
- spec/spec_helper.rb
|
126
|
+
- spec/upvote_spec.rb
|