amico 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format nested
@@ -0,0 +1,3 @@
1
+ # 1.0.0
2
+
3
+ * Initial release
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in amico.gemspec
4
+ gemspec
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 David Czarnecki
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,98 @@
1
+ # amico
2
+
3
+ Relationships (e.g. friendships) backed by Redis.
4
+
5
+ ## Installation
6
+
7
+ `gem install amico`
8
+
9
+ or in your `Gemfile`
10
+
11
+ ```ruby
12
+ gem 'amico'
13
+ ```
14
+
15
+ Make sure your redis server is running! Redis configuration is outside the scope of this README, but
16
+ check out the Redis documentation, http://redis.io/documentation.
17
+
18
+ ## Usage
19
+
20
+ Configure amico:
21
+
22
+ ```ruby
23
+ Amico.configure do |configuration|
24
+ configuration.redis = Redis.new
25
+ configuration.namespace = 'amico'
26
+ configuration.following_key = 'following'
27
+ configuration.followers_key = 'followers'
28
+ configuration.page_size = 25
29
+ end
30
+ ```
31
+
32
+ Use amico:
33
+
34
+ ```ruby
35
+ require 'amico'
36
+ => true
37
+
38
+ Amico.configure do |configuration|
39
+ configuration.redis = Redis.new
40
+ configuration.namespace = 'amico'
41
+ configuration.following_key = 'following'
42
+ configuration.followers_key = 'followers'
43
+ configuration.page_size = 25
44
+ end
45
+
46
+ Amico.follow(1, 11)
47
+ => [1, 1]
48
+
49
+ Amico.following?(1, 11)
50
+ => true
51
+
52
+ Amico.following?(11, 1)
53
+ => false
54
+
55
+ Amico.follow(11, 1)
56
+ => [1, 1]
57
+
58
+ Amico.following?(11, 1)
59
+ => true
60
+
61
+ Amico.following_count(1)
62
+ => 1
63
+
64
+ Amico.followers_count(1)
65
+ => 1
66
+
67
+ Amico.unfollow(11, 1)
68
+ => [1, 1]
69
+
70
+ Amico.following_count(11)
71
+ => 0
72
+
73
+ Amico.following_count(1)
74
+ => 1
75
+
76
+ Amico.follower?(1, 11)
77
+ => false
78
+
79
+ Amico.following(1)
80
+ => ["11"]
81
+ ```
82
+
83
+ You can pass `:page => 1` and `:page_size => 25` options into the `following` and `followers` methods. Set the values as appropriate.
84
+
85
+ ## Contributing to amico
86
+
87
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
88
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
89
+ * Fork the project
90
+ * Start a feature/bugfix branch
91
+ * Commit and push until you are happy with your contribution
92
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
93
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
94
+
95
+ ## Copyright
96
+
97
+ Copyright (c) 2012 David Czarnecki. See LICENSE.txt for further details.
98
+
@@ -0,0 +1,11 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake'
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec) do |spec|
6
+ spec.pattern = 'spec/**/*_spec.rb'
7
+ spec.rspec_opts = ['--backtrace']
8
+ # spec.ruby_opts = ['-w']
9
+ end
10
+
11
+ task :default => :spec
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require 'amico/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "amico"
7
+ s.version = Amico::VERSION
8
+ s.authors = ["David Czarnecki"]
9
+ s.email = ["dczarnecki@agoragames.com"]
10
+ s.homepage = "https://github.com/agoragames/amico"
11
+ s.summary = %q{Relationships (e.g. friendships) backed by Redis}
12
+ s.description = %q{Relationships (e.g. friendships) backed by Redis}
13
+
14
+ s.rubyforge_project = "amico"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_dependency('redis')
22
+
23
+ s.add_development_dependency('rake')
24
+ s.add_development_dependency('rspec')
25
+ end
@@ -0,0 +1,9 @@
1
+ require 'redis'
2
+ require 'amico/version'
3
+ require 'amico/configuration'
4
+ require 'amico/relationships'
5
+
6
+ module Amico
7
+ extend Configuration
8
+ extend Relationships
9
+ end
@@ -0,0 +1,29 @@
1
+ module Amico
2
+ module Configuration
3
+ attr_accessor :redis
4
+ attr_accessor :namespace
5
+ attr_accessor :following_key
6
+ attr_accessor :followers_key
7
+ attr_accessor :page_size
8
+
9
+ def configure
10
+ yield self
11
+ end
12
+
13
+ def namespace
14
+ @namespace ||= 'amico'
15
+ end
16
+
17
+ def following_key
18
+ @following_key ||= 'following'
19
+ end
20
+
21
+ def followers_key
22
+ @followers_key ||= 'followers'
23
+ end
24
+
25
+ def page_size
26
+ @page_size ||= 25
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,82 @@
1
+ module Amico
2
+ module Relationships
3
+ def follow(from_id, to_id)
4
+ return if from_id == to_id
5
+
6
+ Amico.redis.multi do
7
+ Amico.redis.zadd("#{Amico.namespace}:#{Amico.following_key}:#{from_id}", Time.now.to_i, to_id)
8
+ Amico.redis.zadd("#{Amico.namespace}:#{Amico.followers_key}:#{to_id}", Time.now.to_i, from_id)
9
+ end
10
+ end
11
+
12
+ def unfollow(from_id, to_id)
13
+ Amico.redis.multi do
14
+ Amico.redis.zrem("#{Amico.namespace}:#{Amico.following_key}:#{from_id}", to_id)
15
+ Amico.redis.zrem("#{Amico.namespace}:#{Amico.followers_key}:#{to_id}", from_id)
16
+ end
17
+ end
18
+
19
+ def following_count(id)
20
+ Amico.redis.zcard("#{Amico.namespace}:#{Amico.following_key}:#{id}")
21
+ end
22
+
23
+ def followers_count(id)
24
+ Amico.redis.zcard("#{Amico.namespace}:#{Amico.followers_key}:#{id}")
25
+ end
26
+
27
+ def following?(id, following_id)
28
+ !Amico.redis.zscore("#{Amico.namespace}:#{Amico.following_key}:#{id}", following_id).nil?
29
+ end
30
+
31
+ def follower?(id, follower_id)
32
+ !Amico.redis.zscore("#{Amico.namespace}:#{Amico.followers_key}:#{id}", follower_id).nil?
33
+ end
34
+
35
+ def following(id, options = default_options)
36
+ members("#{Amico.namespace}:#{Amico.following_key}:#{id}", options)
37
+ end
38
+
39
+ def followers(id, options = default_options)
40
+ members("#{Amico.namespace}:#{Amico.followers_key}:#{id}", options)
41
+ end
42
+
43
+ def following_page_count(id, page_size = Amico.page_size)
44
+ total_pages("#{Amico.namespace}:#{Amico.following_key}:#{id}", page_size)
45
+ end
46
+
47
+ def followers_page_count(id, page_size = Amico.page_size)
48
+ total_pages("#{Amico.namespace}:#{Amico.followers_key}:#{id}", page_size)
49
+ end
50
+
51
+ private
52
+
53
+ def default_options
54
+ {:page_size => Amico.page_size, :page => 1}
55
+ end
56
+
57
+ def total_pages(key, page_size)
58
+ (Amico.redis.zcard(key) / page_size.to_f).ceil
59
+ end
60
+
61
+ def members(key, options = default_options)
62
+ options = default_options.dup.merge!(options)
63
+ if options[:page] < 1
64
+ options[:page] = 1
65
+ end
66
+
67
+ if options[:page] > total_pages(key, options[:page_size])
68
+ options[:page] = total_pages(key, options[:page_size])
69
+ end
70
+
71
+ index_for_redis = options[:page] - 1
72
+ starting_offset = (index_for_redis * options[:page_size])
73
+
74
+ if starting_offset < 0
75
+ starting_offset = 0
76
+ end
77
+
78
+ ending_offset = (starting_offset + options[:page_size]) - 1
79
+ Amico.redis.zrevrange(key, starting_offset, ending_offset, :with_scores => false)
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,3 @@
1
+ module Amico
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
3
+ describe Amico::Configuration do
4
+ describe '#configure' do
5
+ it 'should have default attributes' do
6
+ Amico.configure do |configuration|
7
+ configuration.namespace.should eql('amico')
8
+ configuration.following_key.should eql('following')
9
+ configuration.followers_key.should eql('followers')
10
+ configuration.page_size.should be(25)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,136 @@
1
+ require 'spec_helper'
2
+
3
+ describe Amico::Relationships do
4
+ describe '#follow' do
5
+ it 'should allow you to follow' do
6
+ Amico.follow(1, 11)
7
+
8
+ Amico.redis.zcard("#{Amico.namespace}:#{Amico.following_key}:1").should be(1)
9
+ Amico.redis.zcard("#{Amico.namespace}:#{Amico.followers_key}:11").should be(1)
10
+ end
11
+
12
+ it 'should not allow you to follow yourself' do
13
+ Amico.follow(1, 1)
14
+
15
+ Amico.redis.zcard("#{Amico.namespace}:#{Amico.following_key}:1").should be(0)
16
+ Amico.redis.zcard("#{Amico.namespace}:#{Amico.followers_key}:1").should be(0)
17
+ end
18
+ end
19
+
20
+ describe '#unfollow' do
21
+ it 'should allow you to follow' do
22
+ Amico.follow(1, 11)
23
+
24
+ Amico.redis.zcard("#{Amico.namespace}:#{Amico.following_key}:1").should be(1)
25
+ Amico.redis.zcard("#{Amico.namespace}:#{Amico.followers_key}:11").should be(1)
26
+
27
+ Amico.unfollow(1, 11)
28
+
29
+ Amico.redis.zcard("#{Amico.namespace}:#{Amico.following_key}:1").should be(0)
30
+ Amico.redis.zcard("#{Amico.namespace}:#{Amico.followers_key}:11").should be(0)
31
+ end
32
+ end
33
+
34
+ describe '#following_count' do
35
+ it 'should return the correct count' do
36
+ Amico.follow(1, 11)
37
+ Amico.following_count(1).should be(1)
38
+ end
39
+ end
40
+
41
+ describe '#followers_count' do
42
+ it 'should return the correct count' do
43
+ Amico.follow(1, 11)
44
+ Amico.followers_count(11).should be(1)
45
+ end
46
+ end
47
+
48
+ describe '#following?' do
49
+ it 'should return that you are following' do
50
+ Amico.follow(1, 11)
51
+ Amico.following?(1, 11).should be_true
52
+ Amico.following?(11, 1).should be_false
53
+
54
+ Amico.follow(11, 1)
55
+ Amico.following?(11, 1).should be_true
56
+ end
57
+ end
58
+
59
+ describe '#follower?' do
60
+ it 'should return that you are being followed' do
61
+ Amico.follow(1, 11)
62
+ Amico.follower?(11, 1).should be_true
63
+ Amico.follower?(1,11).should be_false
64
+
65
+ Amico.follow(11, 1)
66
+ Amico.follower?(1,11).should be_true
67
+ end
68
+ end
69
+
70
+ describe '#following' do
71
+ it 'should return the correct list' do
72
+ Amico.follow(1, 11)
73
+ Amico.follow(1, 12)
74
+ Amico.following(1).should eql(["12", "11"])
75
+ Amico.following(1, :page => 5).should eql(["12", "11"])
76
+ end
77
+
78
+ it 'should page correctly' do
79
+ add_reciprocal_followers
80
+
81
+ Amico.following(1, :page => 1, :page_size => 5).size.should be(5)
82
+ Amico.following(1, :page => 1, :page_size => 10).size.should be(10)
83
+ Amico.following(1, :page => 1, :page_size => 26).size.should be(25)
84
+ end
85
+ end
86
+
87
+ describe '#followers' do
88
+ it 'should return the correct list' do
89
+ Amico.follow(1, 11)
90
+ Amico.follow(2, 11)
91
+ Amico.followers(11).should eql(["2", "1"])
92
+ Amico.followers(11, :page => 5).should eql(["2", "1"])
93
+ end
94
+
95
+ it 'should page correctly' do
96
+ add_reciprocal_followers
97
+
98
+ Amico.followers(1, :page => 1, :page_size => 5).size.should be(5)
99
+ Amico.followers(1, :page => 1, :page_size => 10).size.should be(10)
100
+ Amico.followers(1, :page => 1, :page_size => 26).size.should be(25)
101
+ end
102
+ end
103
+
104
+ describe '#following_page_count' do
105
+ it 'should return the correct count' do
106
+ add_reciprocal_followers
107
+
108
+ Amico.following_page_count(1).should be(1)
109
+ Amico.following_page_count(1, 10).should be(3)
110
+ Amico.following_page_count(1, 5).should be(5)
111
+ end
112
+ end
113
+
114
+ describe '#followers_page_count' do
115
+ it 'should return the correct count' do
116
+ add_reciprocal_followers
117
+
118
+ Amico.followers_page_count(1).should be(1)
119
+ Amico.followers_page_count(1, 10).should be(3)
120
+ Amico.followers_page_count(1, 5).should be(5)
121
+ end
122
+ end
123
+
124
+ private
125
+
126
+ def add_reciprocal_followers(count = 26)
127
+ 1.upto(count) do |outer_index|
128
+ 1.upto(count) do |inner_index|
129
+ if outer_index != inner_index
130
+ Amico.follow(outer_index, inner_index + 1000)
131
+ Amico.follow(inner_index + 1000, outer_index)
132
+ end
133
+ end
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,14 @@
1
+ require 'rspec'
2
+ require 'amico'
3
+
4
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
5
+
6
+ RSpec.configure do |config|
7
+ config.before(:each) do
8
+ Amico.configure do |configuration|
9
+ redis = Redis.new
10
+ redis.flushall
11
+ configuration.redis = redis
12
+ end
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: amico
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - David Czarnecki
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: redis
16
+ requirement: &2160263060 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2160263060
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &2160261920 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2160261920
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &2160260960 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *2160260960
47
+ description: Relationships (e.g. friendships) backed by Redis
48
+ email:
49
+ - dczarnecki@agoragames.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - .rspec
56
+ - CHANGELOG.md
57
+ - Gemfile
58
+ - LICENSE.txt
59
+ - README.md
60
+ - Rakefile
61
+ - amico.gemspec
62
+ - lib/amico.rb
63
+ - lib/amico/configuration.rb
64
+ - lib/amico/relationships.rb
65
+ - lib/amico/version.rb
66
+ - spec/amico/configuration_spec.rb
67
+ - spec/amico/relationships_spec.rb
68
+ - spec/spec_helper.rb
69
+ homepage: https://github.com/agoragames/amico
70
+ licenses: []
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ segments:
82
+ - 0
83
+ hash: -2423888867059560591
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ segments:
91
+ - 0
92
+ hash: -2423888867059560591
93
+ requirements: []
94
+ rubyforge_project: amico
95
+ rubygems_version: 1.8.10
96
+ signing_key:
97
+ specification_version: 3
98
+ summary: Relationships (e.g. friendships) backed by Redis
99
+ test_files:
100
+ - spec/amico/configuration_spec.rb
101
+ - spec/amico/relationships_spec.rb
102
+ - spec/spec_helper.rb