Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

First-pass functionality

* Added basic configuration settings and specs
* Added common friendship methods and specs
* Basic spec helper
  • Loading branch information...
commit ebf7e9988e064d9fe29ea9d8d9d757758f7cf107 1 parent b444dd4
@czarneckid czarneckid authored
View
2  .rspec
@@ -0,0 +1,2 @@
+--color
+--format nested
View
2  amico.gemspec
@@ -1,6 +1,6 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
-require "amico/version"
+require 'amico/version'
Gem::Specification.new do |s|
s.name = "amico"
View
7 lib/amico.rb
@@ -1,5 +1,8 @@
-require "amico/version"
+require 'amico/version'
+require 'amico/configuration'
+require 'amico/friendships'
module Amico
- # Your code goes here...
+ extend Configuration
+ extend Friendships
end
View
24 lib/amico/configuration.rb
@@ -0,0 +1,24 @@
+module Amico
+ module Configuration
+ attr_accessor :redis
+ attr_accessor :namespace
+ attr_accessor :following_key
+ attr_accessor :followers_key
+
+ def configure
+ yield self
+ end
+
+ def namespace
+ @namespace ||= 'amico'
+ end
+
+ def following_key
+ @following_key ||= 'following'
+ end
+
+ def followers_key
+ @followers_key ||= 'followers'
+ end
+ end
+end
View
33 lib/amico/friendships.rb
@@ -0,0 +1,33 @@
+module Amico
+ module Friendships
+ def follow(from_id, to_id)
+ Amico.redis.multi do
+ Amico.redis.sadd("#{Amico.namespace}:#{Amico.following_key}:#{from_id}", to_id)
+ Amico.redis.sadd("#{Amico.namespace}:#{Amico.followers_key}:#{to_id}", from_id)
+ end
+ end
+
+ def unfollow(from_id, to_id)
+ Amico.redis.multi do
+ Amico.redis.srem("#{Amico.namespace}:#{Amico.following_key}:#{from_id}", to_id)
+ Amico.redis.srem("#{Amico.namespace}:#{Amico.followers_key}:#{to_id}", from_id)
+ end
+ end
+
+ def following_count(id)
+ Amico.redis.scard("#{Amico.namespace}:#{Amico.following_key}:#{id}")
+ end
+
+ def followers_count(id)
+ Amico.redis.scard("#{Amico.namespace}:#{Amico.followers_key}:#{id}")
+ end
+
+ def following?(id, following_id)
+ Amico.redis.sismember("#{Amico.namespace}:#{Amico.following_key}:#{id}", following_id)
+ end
+
+ def follower?(id, follower_id)
+ Amico.redis.sismember("#{Amico.namespace}:#{Amico.followers_key}:#{id}", follower_id)
+ end
+ end
+end
View
2  lib/amico/version.rb
@@ -1,3 +1,3 @@
module Amico
- VERSION = "1.0.0"
+ VERSION = '1.0.0'
end
View
13 spec/amico/configuration_spec.rb
@@ -0,0 +1,13 @@
+require 'spec_helper'
+
+describe Amico::Configuration do
+ describe '#configure' do
+ it 'should have default attributes' do
+ Amico.configure do |configuration|
+ configuration.namespace.should eql('amico')
+ configuration.following_key.should eql('following')
+ configuration.followers_key.should eql('followers')
+ end
+ end
+ end
+end
View
62 spec/amico/friendships_spec.rb
@@ -0,0 +1,62 @@
+require 'spec_helper'
+
+describe Amico::Friendships do
+ describe '#follow' do
+ it 'should allow you to follow' do
+ Amico.follow(1, 11)
+
+ Amico.redis.scard("#{Amico.namespace}:#{Amico.following_key}:1").should be(1)
+ Amico.redis.scard("#{Amico.namespace}:#{Amico.followers_key}:11").should be(1)
+ end
+ end
+
+ describe '#unfollow' do
+ it 'should allow you to follow' do
+ Amico.follow(1, 11)
+
+ Amico.redis.scard("#{Amico.namespace}:#{Amico.following_key}:1").should be(1)
+ Amico.redis.scard("#{Amico.namespace}:#{Amico.followers_key}:11").should be(1)
+
+ Amico.unfollow(1, 11)
+
+ Amico.redis.scard("#{Amico.namespace}:#{Amico.following_key}:1").should be(0)
+ Amico.redis.scard("#{Amico.namespace}:#{Amico.followers_key}:11").should be(0)
+ end
+ end
+
+ describe '#following_count' do
+ it 'should return the correct count' do
+ Amico.follow(1, 11)
+ Amico.following_count(1).should be(1)
+ end
+ end
+
+ describe '#followers_count' do
+ it 'should return the correct count' do
+ Amico.follow(1, 11)
+ Amico.followers_count(11).should be(1)
+ end
+ end
+
+ describe '#following?' do
+ it 'should return that you are following' do
+ Amico.follow(1, 11)
+ Amico.following?(1, 11).should be_true
+ Amico.following?(11, 1).should be_false
+
+ Amico.follow(11, 1)
+ Amico.following?(11, 1).should be_true
+ end
+ end
+
+ describe '#follower?' do
+ it 'should return that you are being followed' do
+ Amico.follow(1, 11)
+ Amico.follower?(11, 1).should be_true
+ Amico.follower?(1,11).should be_false
+
+ Amico.follow(11, 1)
+ Amico.follower?(1,11).should be_true
+ end
+ end
+end
View
15 spec/spec_helper.rb
@@ -0,0 +1,15 @@
+require 'rspec'
+require 'redis'
+require 'amico'
+
+Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
+
+RSpec.configure do |config|
+ config.before(:each) do
+ Amico.configure do |configuration|
+ redis = Redis.new
+ redis.flushdb
+ configuration.redis = redis
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.