friend_collection 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1 @@
1
+ USE THIS CODE HOWEVER YOU WANT
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = friend_collection
2
+
3
+ YES!
data/Rakefile ADDED
@@ -0,0 +1,52 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "friend_collection"
8
+ gem.summary = %Q{A way to pull all of a given users friends/followers}
9
+ gem.description = %Q{A way to pull all of a given users friends/followers}
10
+ gem.email = "matt.swasey@gmail.com"
11
+ gem.homepage = "http://github.com/mig/friend_collection"
12
+ gem.authors = ["Matt Swasey"]
13
+ gem.add_development_dependency "shoulda", ">= 0"
14
+ end
15
+ Jeweler::GemcutterTasks.new
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/**/test_*.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+ task :test => :check_dependencies
41
+
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
47
+
48
+ rdoc.rdoc_dir = 'rdoc'
49
+ rdoc.title = "friend_collection #{version}"
50
+ rdoc.rdoc_files.include('README*')
51
+ rdoc.rdoc_files.include('lib/**/*.rb')
52
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,38 @@
1
+ class ConfigStore
2
+ attr_reader :file
3
+
4
+ def initialize(file)
5
+ @file = file
6
+ end
7
+
8
+ def load
9
+ @config ||= YAML::load(open(file))
10
+ self
11
+ end
12
+
13
+ def [](key)
14
+ load
15
+ @config[key]
16
+ end
17
+
18
+ def []=(key, value)
19
+ @config[key] = value
20
+ end
21
+
22
+ def delete(*keys)
23
+ keys.each { |key| @config.delete(key) }
24
+ save
25
+ self
26
+ end
27
+
28
+ def update(c={})
29
+ @config.merge!(c)
30
+ save
31
+ self
32
+ end
33
+
34
+ def save
35
+ File.open(file, 'w') { |f| f.write(YAML.dump(@config)) }
36
+ self
37
+ end
38
+ end
@@ -0,0 +1,37 @@
1
+ require File.dirname(__FILE__) + '/config_store'
2
+
3
+ class Connect
4
+ attr_reader :client
5
+
6
+ def self.go!
7
+ new.client
8
+ end
9
+
10
+ def initialize
11
+ config = ConfigStore.new("#{ENV['HOME']}/.twitter")
12
+ oauth = Twitter::OAuth.new(config['token'], config['secret'])
13
+
14
+ if config['atoken'] && config['asecret']
15
+ oauth.authorize_from_access(config['atoken'], config['asecret'])
16
+ @client = Twitter::Base.new(oauth)
17
+
18
+ elsif config['rtoken'] && config['rsecret']
19
+ oauth.authorize_from_request(config['rtoken'], config['rsecret'], config['pin'])
20
+ @client = Twitter::Base.new(oauth)
21
+
22
+ config.update({
23
+ 'atoken' => oauth.access_token.token,
24
+ 'asecret' => oauth.access_token.secret,
25
+ }).delete('rtoken', 'rsecret')
26
+ else
27
+ config.update({
28
+ 'rtoken' => oauth.request_token.token,
29
+ 'rsecret' => oauth.request_token.secret,
30
+ })
31
+
32
+ # authorize in browser
33
+ %x(open #{oauth.request_token.authorize_url})
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'twitter'
3
+
4
+ require 'connect'
5
+ require File.dirname(__FILE__) + '/../lib/friend_collection'
6
+
7
+ client = Connect.go!
8
+ fc = Twitter::FriendCollection.new(client)
9
+ fc.fetch_all
10
+ puts fc.collection
11
+ puts fc.collection.length
@@ -0,0 +1,60 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{friend_collection}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Matt Swasey"]
12
+ s.date = %q{2010-03-17}
13
+ s.description = %q{A way to pull all of a given users friends/followers}
14
+ s.email = %q{matt.swasey@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "examples/config_store.rb",
27
+ "examples/connect.rb",
28
+ "examples/friends.rb",
29
+ "friend_collection.gemspec",
30
+ "lib/friend_collection.rb",
31
+ "test/helper.rb",
32
+ "test/test_friend_collection.rb"
33
+ ]
34
+ s.homepage = %q{http://github.com/mig/friend_collection}
35
+ s.rdoc_options = ["--charset=UTF-8"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.3.6}
38
+ s.summary = %q{A way to pull all of a given users friends/followers}
39
+ s.test_files = [
40
+ "test/helper.rb",
41
+ "test/test_friend_collection.rb",
42
+ "examples/config_store.rb",
43
+ "examples/connect.rb",
44
+ "examples/friends.rb"
45
+ ]
46
+
47
+ if s.respond_to? :specification_version then
48
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
49
+ s.specification_version = 3
50
+
51
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
52
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
53
+ else
54
+ s.add_dependency(%q<shoulda>, [">= 0"])
55
+ end
56
+ else
57
+ s.add_dependency(%q<shoulda>, [">= 0"])
58
+ end
59
+ end
60
+
@@ -0,0 +1,58 @@
1
+ require 'forwardable'
2
+
3
+ module Twitter
4
+ class FriendCollection
5
+ extend Forwardable
6
+
7
+ attr_accessor :client,
8
+ :relationship_type,
9
+ :page,
10
+ :collection,
11
+ :limit
12
+
13
+ def_delegators :client, :friends, :followers
14
+
15
+ def initialize(client, relationship_type = :friends, limit = 500)
16
+ @client = client
17
+ @relationship_type = relationship_type.to_sym
18
+ @collection = []
19
+ @limit = limit
20
+ end
21
+
22
+ def first_cursor
23
+ -1
24
+ end
25
+
26
+ def next_cursor
27
+ page['next_cursor']
28
+ end
29
+
30
+ def first_or_next_cursor
31
+ if page.nil?
32
+ first_cursor
33
+ else
34
+ next_cursor
35
+ end
36
+ end
37
+
38
+ def no_more_cursors
39
+ first_or_next_cursor == 0
40
+ end
41
+
42
+ def over_limit?
43
+ collection.length >= limit
44
+ end
45
+
46
+ def fetch
47
+ self.page = send(relationship_type, {:cursor => first_or_next_cursor})
48
+ end
49
+
50
+ def fetch_all
51
+ until no_more_cursors || over_limit?
52
+ fetch
53
+ collection << page['users']
54
+ end
55
+ collection.flatten!
56
+ end
57
+ end
58
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'mocha'
5
+
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
8
+ require 'friend_collection'
9
+
10
+ class Test::Unit::TestCase
11
+ end
@@ -0,0 +1,81 @@
1
+ require 'helper'
2
+
3
+ class TestFriendCollection < Test::Unit::TestCase
4
+ context "an instance of FriendCollection" do
5
+ setup do
6
+ @client = stub('client')
7
+ @friend_collection = Twitter::FriendCollection.new(@client)
8
+ end
9
+
10
+ should "call friends on client for fetch when friends is relationship type" do
11
+ @client.expects(:friends)
12
+ @friend_collection.stubs(:relationship_type).returns(:friends)
13
+ @friend_collection.fetch
14
+ end
15
+
16
+ should "call followers on client for fetch when followers is relationship type" do
17
+ @client.expects(:followers)
18
+ @friend_collection.stubs(:relationship_type).returns(:followers)
19
+ @friend_collection.fetch
20
+ end
21
+
22
+ should "set page to be result of relationship_type called on client" do
23
+ @client.expects(:friends).returns('result')
24
+ @friend_collection.fetch
25
+ assert_equal 'result', @friend_collection.page
26
+ end
27
+
28
+ should "use first_or_next_cursor value when calling relation_type on client" do
29
+ @client.expects(:friends).with(:cursor => "12345")
30
+ @friend_collection.stubs(:first_or_next_cursor).returns("12345")
31
+ @friend_collection.fetch
32
+ end
33
+
34
+ should "use -1 as the first cursor" do
35
+ assert_equal -1, @friend_collection.first_cursor
36
+ end
37
+
38
+ should "use value in page as next cursor" do
39
+ @friend_collection.expects(:page).returns('next_cursor' => '1234')
40
+ assert_equal '1234', @friend_collection.next_cursor
41
+ end
42
+
43
+ should "set initial cursor to -1" do
44
+ assert_equal -1, @friend_collection.first_or_next_cursor
45
+ end
46
+
47
+ should "set next cursor to next_cursor value in page" do
48
+ @friend_collection.page = {'next_cursor' => '12345'}
49
+ assert_equal "12345", @friend_collection.first_or_next_cursor
50
+ end
51
+
52
+ should "determine if there are no more cursors by next_cursor" do
53
+ @friend_collection.expects(:first_or_next_cursor).returns(1)
54
+ assert_equal false, @friend_collection.no_more_cursors
55
+ end
56
+
57
+ should "determine if we are over friend limits" do
58
+ @friend_collection.limit = 2
59
+
60
+ @friend_collection.collection << "1"
61
+ assert_equal false, @friend_collection.over_limit?
62
+
63
+ @friend_collection.collection << "2"
64
+ assert_equal true, @friend_collection.over_limit?
65
+ end
66
+
67
+ should "fetch pages of results till there are no more cursors" do
68
+ @friend_collection.expects(:no_more_cursors).times(3).returns(false, false, true)
69
+ @friend_collection.expects(:first_or_next_cursor).times(2).returns(-1, 12345)
70
+ @client.expects(:friends).with({:cursor => -1}).returns('users' => ['friends'])
71
+ @client.expects(:friends).with({:cursor => 12345}).returns('users' => [])
72
+ @friend_collection.fetch_all
73
+ end
74
+
75
+ should "fetch page of results till over limit" do
76
+ @friend_collection.limit = 1
77
+ @client.expects(:friends).returns({'users' => ['1']}, {'users' => []})
78
+ @friend_collection.fetch_all
79
+ end
80
+ end
81
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: friend_collection
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Matt Swasey
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-03-17 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: shoulda
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :development
31
+ version_requirements: *id001
32
+ description: A way to pull all of a given users friends/followers
33
+ email: matt.swasey@gmail.com
34
+ executables: []
35
+
36
+ extensions: []
37
+
38
+ extra_rdoc_files:
39
+ - LICENSE
40
+ - README.rdoc
41
+ files:
42
+ - .document
43
+ - .gitignore
44
+ - LICENSE
45
+ - README.rdoc
46
+ - Rakefile
47
+ - VERSION
48
+ - examples/config_store.rb
49
+ - examples/connect.rb
50
+ - examples/friends.rb
51
+ - friend_collection.gemspec
52
+ - lib/friend_collection.rb
53
+ - test/helper.rb
54
+ - test/test_friend_collection.rb
55
+ has_rdoc: true
56
+ homepage: http://github.com/mig/friend_collection
57
+ licenses: []
58
+
59
+ post_install_message:
60
+ rdoc_options:
61
+ - --charset=UTF-8
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ segments:
69
+ - 0
70
+ version: "0"
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ requirements: []
79
+
80
+ rubyforge_project:
81
+ rubygems_version: 1.3.6
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: A way to pull all of a given users friends/followers
85
+ test_files:
86
+ - test/helper.rb
87
+ - test/test_friend_collection.rb
88
+ - examples/config_store.rb
89
+ - examples/connect.rb
90
+ - examples/friends.rb