bmabey-faketwitter 0.1.1

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.
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,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Ben Mabey
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.
data/README.textile ADDED
@@ -0,0 +1,18 @@
1
+ h1. faketwitter
2
+
3
+ Factory goodness for twitter search responses wrapped in FakeWeb awesomeness.
4
+
5
+ <pre>
6
+ irb(main):001:0> require 'faketwitter'
7
+ => true
8
+ irb(main):002:0> require 'twitter_search'
9
+ => true
10
+ irb(main):003:0> FakeTwitter.register_search("#cheese", {:results => [{:text => "#cheese is good"}]})
11
+ => [#<FakeWeb::Responder:0x19792c0 @times=1, @uri="http://search.twitter.com/search.json?q=%23cheese", @options={:body=>"{"results":[{"text":"#cheese is good","from_user":"jojo","to_user_id":null,"id":1,"from_user_id":1,"iso_language_code":"en","source":"&lt;a href=&quot;http:\\/\\/twitter.com\\/&quot;&gt;web&lt;\\/a&gt;","created_at":"Fri, 21 Aug 2009 09:31:27 +0000","profile_image_url":"http:\\/\\/s3.amazonaws.com\\/twitter_production\\/profile_images\\/1\\/photo.jpg"}],"since_id":0,"max_id":1,"results_per_page":15,"completed_in":0.008646,"page":1,"query":"%23cheese"}"}, method:get]
12
+ irb(main):004:0> TwitterSearch::Client.new('').query('#cheese')
13
+ => [#<TwitterSearch::Tweet:0x196cef8 @id=1, @text="#cheese is good", @created_at="Fri, 21 Aug 2009 09:31:27 +0000", @to_user_id=nil, @from_user_id=1, @to_user=nil, @source="&lt;a href=&quot;http://twitter.com/&quot;&gt;web&lt;/a&gt;", @iso_language_code="en", @from_user="jojo", @language="en", @profile_image_url="http://s3.amazonaws.com/twitter_production/profile_images/1/photo.jpg">]
14
+ irb(main):005:0>
15
+
16
+ </pre>
17
+
18
+ For more information look at the specs.
data/Rakefile ADDED
@@ -0,0 +1,48 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "faketwitter"
8
+ gem.summary = %Q{Easily stub out Twitter searches}
9
+ gem.description = %Q{FakeWeb wrapper for Twitter and factories to build tweets and search responses}
10
+ gem.email = "ben@benmabey.com"
11
+ gem.homepage = "http://github.com/bmabey/faketwitter"
12
+ gem.authors = ["Ben Mabey"]
13
+ gem.add_development_dependency "rspec"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
+ end
19
+
20
+ require 'spec/rake/spectask'
21
+ Spec::Rake::SpecTask.new(:spec) do |spec|
22
+ spec.libs << 'lib' << 'spec'
23
+ spec.spec_files = FileList['spec/**/*_spec.rb']
24
+ end
25
+
26
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
27
+ spec.libs << 'lib' << 'spec'
28
+ spec.pattern = 'spec/**/*_spec.rb'
29
+ spec.rcov = true
30
+ end
31
+
32
+ task :spec => :check_dependencies
33
+
34
+ task :default => :spec
35
+
36
+ require 'rake/rdoctask'
37
+ Rake::RDocTask.new do |rdoc|
38
+ if File.exist?('VERSION')
39
+ version = File.read('VERSION')
40
+ else
41
+ version = ""
42
+ end
43
+
44
+ rdoc.rdoc_dir = 'rdoc'
45
+ rdoc.title = "faketwitter #{version}"
46
+ rdoc.rdoc_files.include('README*')
47
+ rdoc.rdoc_files.include('lib/**/*.rb')
48
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
@@ -0,0 +1,55 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{faketwitter}
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Ben Mabey"]
12
+ s.date = %q{2009-08-23}
13
+ s.description = %q{FakeWeb wrapper for Twitter and factories to build tweets and search responses}
14
+ s.email = %q{ben@benmabey.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.textile"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.textile",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "faketwitter.gemspec",
27
+ "lib/core_ext/hash.rb",
28
+ "lib/faketwitter.rb",
29
+ "spec/faketwitter_spec.rb",
30
+ "spec/spec.opts",
31
+ "spec/spec_helper.rb"
32
+ ]
33
+ s.homepage = %q{http://github.com/bmabey/faketwitter}
34
+ s.rdoc_options = ["--charset=UTF-8"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.3.3}
37
+ s.summary = %q{Easily stub out Twitter searches}
38
+ s.test_files = [
39
+ "spec/faketwitter_spec.rb",
40
+ "spec/spec_helper.rb"
41
+ ]
42
+
43
+ if s.respond_to? :specification_version then
44
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
45
+ s.specification_version = 3
46
+
47
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
48
+ s.add_development_dependency(%q<rspec>, [">= 0"])
49
+ else
50
+ s.add_dependency(%q<rspec>, [">= 0"])
51
+ end
52
+ else
53
+ s.add_dependency(%q<rspec>, [">= 0"])
54
+ end
55
+ end
@@ -0,0 +1,10 @@
1
+ class Hash
2
+ # stolen from ActiveSupport
3
+ def stringify_keys
4
+ inject({}) do |options, (key, value)|
5
+ options[key.to_s] = value
6
+ options
7
+ end
8
+ end
9
+ end
10
+
@@ -0,0 +1,117 @@
1
+ require 'fakeweb'
2
+ require 'cgi'
3
+ require File.dirname(__FILE__) + "/core_ext/hash"
4
+
5
+ module FakeTwitter
6
+
7
+ class << self
8
+
9
+ def register_search(query, search_options = {})
10
+ escaped_query = CGI.escape(query)
11
+ search_options['query'] = escaped_query
12
+ FakeWeb.register_uri(
13
+ :get,
14
+ "http://search.twitter.com/search.json?q=#{escaped_query}",
15
+ :body => search_response(search_options).to_json
16
+ )
17
+ end
18
+
19
+ def new_tweet(attributes)
20
+ TweetFactory.create(attributes)
21
+ end
22
+
23
+ def search_response(options={})
24
+ SearchResponseFactory.create(options)
25
+ end
26
+
27
+ end
28
+
29
+
30
+ class SearchResponseFactory
31
+ DEFAULTS = {
32
+ 'results' => [],
33
+ 'since_id' => 0,
34
+ 'max_id' => -1,
35
+ 'results_per_page' => 15,
36
+ 'completed_in' => 0.008646,
37
+ 'page' => 1,
38
+ 'query' => ''
39
+ }
40
+
41
+ class << self
42
+
43
+ def create(attributes)
44
+ # TODO: remove duplication between factories.
45
+ response = DEFAULTS.merge(attributes.stringify_keys)
46
+ response['results'] = create_tweets(response['results'].dup)
47
+
48
+ unless response['results'].empty?
49
+ response['max_id'] = response['results'].map { |t| t['id'] }.max
50
+ end
51
+
52
+ response
53
+ end
54
+
55
+
56
+ private
57
+ def create_tweets(tweets)
58
+ now = Time.now
59
+ tweets.sort! do |tweet_a, tweet_b|
60
+ (tweet_b['created_at'] || now) <=> (tweet_a['created_at'] || now)
61
+ end
62
+ # We do the reverses so that the id's are auto-incremented correctly
63
+ tweets.reverse!.map! do |tweet_hash|
64
+ tweet = FakeTwitter.new_tweet(tweet_hash)
65
+ # hardcoding +0000 because %z was giving -0700 for UTC for some reason (leopard MRI)..
66
+ tweet['created_at'] = tweet['created_at'].utc.strftime("%a, %d %b %Y %H:%M:%S +0000")
67
+ tweet['source'] = CGI.escapeHTML(tweet['source'])
68
+ tweet
69
+ end
70
+
71
+ tweets.reverse!
72
+ end
73
+
74
+ end
75
+
76
+ end
77
+
78
+ class TweetFactory
79
+ DEFAULTS = {
80
+ "text"=>"just some tweet",
81
+ "from_user"=>"jojo",
82
+ "to_user_id"=>nil,
83
+ "iso_language_code"=>"en",
84
+ "source"=>'<a href="http://twitter.com/">web</a>',
85
+ }
86
+
87
+ class << self
88
+
89
+ def reset
90
+ @counter = nil
91
+ @users = nil
92
+ end
93
+
94
+ def create(attributes)
95
+ tweet = DEFAULTS.merge(attributes.stringify_keys)
96
+ tweet['id'] ||= counter(:id)
97
+ tweet['from_user_id'] ||= user_id_for(tweet['from_user'])
98
+ tweet['profile_image_url'] ||= "http://s3.amazonaws.com/twitter_production/profile_images/#{tweet['from_user_id']}/photo.jpg"
99
+ tweet['created_at'] ||= Time.now
100
+
101
+ tweet
102
+ end
103
+
104
+ def counter(counter_type)
105
+ @counter ||= Hash.new(0)
106
+ @counter[counter_type] += 1
107
+ end
108
+
109
+ def user_id_for(user)
110
+ @users ||= {}
111
+ @users[user] ||= counter(:user_id)
112
+ end
113
+
114
+ end
115
+ end
116
+
117
+ end
@@ -0,0 +1,134 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require 'activesupport'
3
+
4
+ describe FakeTwitter do
5
+ before(:each) do
6
+ FakeTwitter::TweetFactory.reset
7
+ end
8
+
9
+ describe '::register_search' do
10
+ it "registers the twitter search url and the specified query with FakeWeb" do
11
+ # expect
12
+ FakeWeb.should_receive(:register_uri).with(:get, "http://search.twitter.com/search.json?q=%23foo+%22some+string%22", anything)
13
+ # when
14
+ FakeTwitter.register_search('#foo "some string"', {})
15
+ end
16
+
17
+ it "uses the provided tweets for the stubbed response" do
18
+ # given
19
+ created_at = Time.parse("Thu, 20 Aug 2009 23:23:09 +0000")
20
+ tweets = [
21
+ {:from_user => "bmabey", :id => 1000, :from_user_id => 100,
22
+ :created_at => created_at, :text => 'making FakeTwitter'}
23
+ ]
24
+
25
+ # expect
26
+ expected_json_tweet = <<-EOT
27
+ {
28
+ "text": "making FakeTwitter",
29
+ "to_user_id": null,
30
+ "from_user": "bmabey",
31
+ "id": 1000,
32
+ "from_user_id": 100,
33
+ "iso_language_code": "en",
34
+ "source": "&lt;a href=&quot;http:\/\/twitter.com\/&quot;&gt;web&lt;\/a&gt;",
35
+ "profile_image_url": "http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/100\/photo.jpg",
36
+ "created_at": "Thu, 20 Aug 2009 23:23:09 +0000"
37
+ }
38
+ EOT
39
+
40
+ FakeWeb.should_receive(:register_uri).with do |_, _, fakeweb_return_options|
41
+ json = fakeweb_return_options[:body]
42
+ JSON.parse(json)['results'].first.should == JSON.parse(expected_json_tweet)
43
+ end
44
+
45
+ # when
46
+ FakeTwitter.register_search('foo', :results => tweets)
47
+ end
48
+
49
+ it "includes the escaped query in the response" do
50
+ FakeWeb.should_receive(:register_uri).with do |_, _, fakeweb_return_options|
51
+ json = fakeweb_return_options[:body]
52
+ JSON.parse(json)['query'].should == "%23foo"
53
+ end
54
+
55
+ # when
56
+ FakeTwitter.register_search('#foo')
57
+ end
58
+
59
+ end
60
+
61
+
62
+ describe '::search_response' do
63
+ it "has sane defaults for the other response values" do
64
+ response = FakeTwitter.search_response()
65
+ response.slice(*%w[since_id max_id results_per_page completed_in page query]).should == {
66
+ 'since_id' => 0,
67
+ 'max_id' => -1,
68
+ 'results_per_page' => 15,
69
+ 'completed_in' => 0.008646,
70
+ 'page' => 1,
71
+ 'query' => ''
72
+ }
73
+ end
74
+
75
+ it "sets the max_id based on the highest tweet id in the result set" do
76
+ response = FakeTwitter.search_response(:results => [{:id => 1}, {:id => 100}])
77
+ response['max_id'].should == 100
78
+ end
79
+
80
+ it "creates the tweets based on the defined created_at times if possible" do
81
+ response = FakeTwitter.search_response(:results => [
82
+ {'created_at' => Time.now - 100, :text => 'first tweet'},
83
+ {'created_at' => Time.now, :text => 'most recent tweet'}
84
+ ])
85
+ response['results'].first['text'].should == 'most recent tweet'
86
+ response['results'].first['id'].should > response['results'].last['id']
87
+ end
88
+
89
+
90
+ end
91
+
92
+
93
+
94
+ describe '::new_tweet' do
95
+
96
+ it "returns hash of an API compliant tweet with sane defaults" do
97
+ tweet = FakeTwitter.new_tweet({})
98
+ tweet.slice(*%w[text from_user to_user_id iso_language_code source]).should == {
99
+ "text"=>"just some tweet",
100
+ "from_user"=>"jojo",
101
+ "to_user_id"=>nil,
102
+ "iso_language_code"=>"en",
103
+ "source"=>'<a href="http://twitter.com/">web</a>',
104
+ }
105
+ end
106
+
107
+ it "auto-increments the tweet ids when they are not provided" do
108
+ FakeTwitter.new_tweet({})['id'].should == 1
109
+ FakeTwitter.new_tweet({})['id'].should == 2
110
+ end
111
+
112
+ it "auto-increments user ids when none are provided" do
113
+ FakeTwitter.new_tweet({'from_user' => 'jojo'})['from_user_id'].should == 1
114
+ FakeTwitter.new_tweet({'from_user' => 'krusty'})['from_user_id'].should == 2
115
+ end
116
+
117
+ it "resuses the same user id for tweets made for the same user" do
118
+ FakeTwitter.new_tweet({'from_user' => 'jojo'})['from_user_id'].should == 1
119
+ FakeTwitter.new_tweet({'from_user' => 'jojo'})['from_user_id'].should == 1
120
+ end
121
+
122
+ it "defaults create_at to Time.now" do
123
+ Time.stub!(:now => 'current time')
124
+ FakeTwitter.new_tweet({})['created_at'].should == 'current time'
125
+ end
126
+
127
+ it "bases the default profile_image_url off of the user id" do
128
+ FakeTwitter.new_tweet('from_user_id' => 123)['profile_image_url'].
129
+ should == "http://s3.amazonaws.com/twitter_production/profile_images/123/photo.jpg"
130
+ end
131
+
132
+ end
133
+
134
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,4 @@
1
+ -c
2
+ -f
3
+ nested
4
+ --diff
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'rubygems'
4
+ require 'faketwitter'
5
+ require 'spec'
6
+ require 'spec/autorun'
7
+
8
+ Spec::Runner.configure do |config|
9
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bmabey-faketwitter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Ben Mabey
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-08-23 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: FakeWeb wrapper for Twitter and factories to build tweets and search responses
26
+ email: ben@benmabey.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.textile
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.textile
39
+ - Rakefile
40
+ - VERSION
41
+ - faketwitter.gemspec
42
+ - lib/core_ext/hash.rb
43
+ - lib/faketwitter.rb
44
+ - spec/faketwitter_spec.rb
45
+ - spec/spec.opts
46
+ - spec/spec_helper.rb
47
+ has_rdoc: false
48
+ homepage: http://github.com/bmabey/faketwitter
49
+ licenses:
50
+ post_install_message:
51
+ rdoc_options:
52
+ - --charset=UTF-8
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ requirements: []
68
+
69
+ rubyforge_project:
70
+ rubygems_version: 1.3.5
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: Easily stub out Twitter searches
74
+ test_files:
75
+ - spec/faketwitter_spec.rb
76
+ - spec/spec_helper.rb