dancroak-twitter-search 0.5.2 → 0.5.3
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.textile +7 -0
- data/README.markdown +17 -5
- data/Rakefile +8 -8
- data/lib/trends.rb +36 -0
- data/lib/tweets.rb +45 -0
- data/lib/twitter_search.rb +26 -43
- metadata +13 -5
data/CHANGELOG.textile
ADDED
data/README.markdown
CHANGED
@@ -14,8 +14,10 @@ Require the gem.
|
|
14
14
|
|
15
15
|
Set up a TwitterSearch::Client. Name your client (a.k.a. 'user agent') to something meaningful, such as your app's name. This helps Twitter Search answer any questions about your use of the API.
|
16
16
|
|
17
|
-
|
18
|
-
|
17
|
+
@client = TwitterSearch::Client.new 'politweets'
|
18
|
+
|
19
|
+
### Search
|
20
|
+
|
19
21
|
Request tweets by calling the query method of your client. It takes either a String or a Hash of arguments.
|
20
22
|
|
21
23
|
@tweets = @client.query 'twitter search'
|
@@ -26,6 +28,16 @@ The String form uses the default Twitter Search behavior, which in this example
|
|
26
28
|
|
27
29
|
Use the Twitter Search API's query operators with the :q key to access a variety of behavior.
|
28
30
|
|
31
|
+
### Trends
|
32
|
+
|
33
|
+
Request the current trending topics by calling the trends method of your client. It takes an optional Hash of arguments.
|
34
|
+
|
35
|
+
@trends = @client.trends
|
36
|
+
|
37
|
+
The only supported option currently is exclude_hashtags to return trends that are not hashtags only.
|
38
|
+
|
39
|
+
@trends = @client.trends :exclude_hashtags => true
|
40
|
+
|
29
41
|
## Search Operators
|
30
42
|
|
31
43
|
The following operator examples find tweets...
|
@@ -66,9 +78,9 @@ Alter the number of Tweets returned per page with the :rpp key. Stick with 10, 1
|
|
66
78
|
|
67
79
|
* Searching for a positive attitude :) returns tweets containing the text :), =), :D, and :-)
|
68
80
|
|
69
|
-
##
|
81
|
+
## Contributors
|
70
82
|
|
71
|
-
|
83
|
+
Dustin Sallings, Dan Croak, Luke Francl, Matt Sanford, Alejandro Crosa, Danny Burkes, Don Brown, & HotFusionMan.
|
72
84
|
|
73
85
|
## Resources
|
74
86
|
|
@@ -76,4 +88,4 @@ Written by Dustin Sallings (dustin@spy.net), forked by Dan Croak (dcroak@thought
|
|
76
88
|
|
77
89
|
## License
|
78
90
|
|
79
|
-
MIT License, same terms as Ruby.
|
91
|
+
MIT License, same terms as Ruby.
|
data/Rakefile
CHANGED
@@ -1,24 +1,24 @@
|
|
1
1
|
require 'rake'
|
2
2
|
require 'rake/testtask'
|
3
|
-
|
4
|
-
test_files_pattern = 'test/
|
3
|
+
|
4
|
+
test_files_pattern = 'test/twitter_*_test.rb'
|
5
5
|
Rake::TestTask.new do |t|
|
6
6
|
t.libs << 'lib'
|
7
7
|
t.pattern = test_files_pattern
|
8
8
|
t.verbose = false
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
desc "Run the test suite"
|
12
12
|
task :default => :test
|
13
|
-
|
13
|
+
|
14
14
|
gem_spec = Gem::Specification.new do |gem_spec|
|
15
15
|
gem_spec.name = "twitter-search"
|
16
|
-
gem_spec.version = "0.5.
|
16
|
+
gem_spec.version = "0.5.3"
|
17
17
|
gem_spec.summary = "Ruby client for Twitter Search."
|
18
18
|
gem_spec.email = "dcroak@thoughtbot.com"
|
19
19
|
gem_spec.homepage = "http://github.com/dancroak/twitter-search"
|
20
20
|
gem_spec.description = "Ruby client for Twitter Search."
|
21
|
-
gem_spec.authors = ["Dustin Sallings", "Dan Croak"]
|
21
|
+
gem_spec.authors = ["Dustin Sallings", "Dan Croak", "Luke Francl", "Matt Sanford", "Alejandro Crosa", "Danny Burkes", "Don Brown", "HotFusionMan"]
|
22
22
|
gem_spec.files = FileList["[A-Z]*", "{generators,lib,shoulda_macros,rails}/**/*"]
|
23
23
|
gem_spec.add_dependency('json', '>= 1.1.2')
|
24
24
|
end
|
@@ -36,7 +36,7 @@ require 'yaml'
|
|
36
36
|
|
37
37
|
namespace :yaml do
|
38
38
|
desc "Write Twitter Search results to yaml file so API is not hit every test."
|
39
|
-
task :write do
|
39
|
+
task :write do
|
40
40
|
write_yaml :tweets => 'Obama', :file => 'obama'
|
41
41
|
write_yaml :tweets => 'twitter search', :file => 'twitter_search'
|
42
42
|
write_yaml :tweets => {:q => 'twitter search'}, :file => 'twitter_search_and'
|
@@ -67,4 +67,4 @@ def write_yaml(opts = {})
|
|
67
67
|
File.open(File.join(File.dirname(__FILE__), 'test', 'yaml', "#{opts[:file]}.yaml"), 'w+') do |file|
|
68
68
|
file.puts tweets.to_yaml
|
69
69
|
end
|
70
|
-
end
|
70
|
+
end
|
data/lib/trends.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
module TwitterSearch
|
2
|
+
class Trend
|
3
|
+
VARS = [ :query, :name ]
|
4
|
+
attr_reader *VARS
|
5
|
+
attr_reader :exclude_hashtags
|
6
|
+
|
7
|
+
def initialize(opts)
|
8
|
+
@exclude_hashtags = !!opts['exclude_hashtags']
|
9
|
+
VARS.each { |each| instance_variable_set "@#{each}", opts[each.to_s] }
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class Trends
|
14
|
+
VARS = [:date]
|
15
|
+
attr_reader *VARS
|
16
|
+
|
17
|
+
include Enumerable
|
18
|
+
|
19
|
+
def initialize(opts)
|
20
|
+
@trends = opts['trends'].first.collect { |each| Trend.new(each) }
|
21
|
+
VARS.each { |each| instance_variable_set "@#{each}", opts[each.to_s] }
|
22
|
+
end
|
23
|
+
|
24
|
+
def each(&block)
|
25
|
+
@trends.each(&block)
|
26
|
+
end
|
27
|
+
|
28
|
+
def size
|
29
|
+
@trends.size
|
30
|
+
end
|
31
|
+
|
32
|
+
def [](index)
|
33
|
+
@trends[index]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/lib/tweets.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
module TwitterSearch
|
2
|
+
class Tweet
|
3
|
+
VARS = [:text, :from_user, :to_user, :to_user_id, :id, :iso_language_code, :from_user_id, :created_at, :profile_image_url, :source ]
|
4
|
+
attr_reader *VARS
|
5
|
+
attr_reader :language
|
6
|
+
|
7
|
+
def initialize(opts)
|
8
|
+
@language = opts['iso_language_code']
|
9
|
+
VARS.each { |each| instance_variable_set "@#{each}", opts[each.to_s] }
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class Tweets
|
14
|
+
VARS = [:since_id, :max_id, :results_per_page, :page, :query, :next_page]
|
15
|
+
attr_reader *VARS
|
16
|
+
|
17
|
+
include Enumerable
|
18
|
+
|
19
|
+
def initialize(opts)
|
20
|
+
@results = opts['results'].collect { |each| Tweet.new(each) }
|
21
|
+
VARS.each { |each| instance_variable_set "@#{each}", opts[each.to_s] }
|
22
|
+
end
|
23
|
+
|
24
|
+
def each(&block)
|
25
|
+
@results.each(&block)
|
26
|
+
end
|
27
|
+
|
28
|
+
def size
|
29
|
+
@results.size
|
30
|
+
end
|
31
|
+
|
32
|
+
def [](index)
|
33
|
+
@results[index]
|
34
|
+
end
|
35
|
+
|
36
|
+
def has_next_page?
|
37
|
+
! @next_page.nil?
|
38
|
+
end
|
39
|
+
|
40
|
+
def get_next_page
|
41
|
+
client = Client.new
|
42
|
+
return client.query( CGI.parse( @next_page[1..-1] ) )
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/lib/twitter_search.rb
CHANGED
@@ -3,51 +3,22 @@ require 'net/http'
|
|
3
3
|
require 'json'
|
4
4
|
require 'cgi'
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
class Tweet
|
9
|
-
VARS = [:text, :from_user, :to_user, :to_user_id, :id, :iso_language_code, :from_user_id, :created_at, :profile_image_url ]
|
10
|
-
attr_reader *VARS
|
11
|
-
attr_reader :language
|
12
|
-
|
13
|
-
def initialize(opts)
|
14
|
-
@language = opts['iso_language_code']
|
15
|
-
VARS.each { |each| instance_variable_set "@#{each}", opts[each.to_s] }
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
class Tweets
|
20
|
-
VARS = [:since_id, :max_id, :results_per_page, :page, :query, :next_page]
|
21
|
-
attr_reader *VARS
|
22
|
-
|
23
|
-
include Enumerable
|
24
|
-
|
25
|
-
def initialize(opts)
|
26
|
-
@results = opts['results'].collect { |each| Tweet.new(each) }
|
27
|
-
VARS.each { |each| instance_variable_set "@#{each}", opts[each.to_s] }
|
28
|
-
end
|
29
|
-
|
30
|
-
def each(&block)
|
31
|
-
@results.each(&block)
|
32
|
-
end
|
6
|
+
require File.join(File.dirname(__FILE__), 'tweets')
|
7
|
+
require File.join(File.dirname(__FILE__), 'trends')
|
33
8
|
|
34
|
-
|
35
|
-
@results.size
|
36
|
-
end
|
37
|
-
|
38
|
-
def [](index)
|
39
|
-
@results[index]
|
40
|
-
end
|
41
|
-
end
|
9
|
+
module TwitterSearch
|
42
10
|
|
43
11
|
class Client
|
44
|
-
|
12
|
+
TWITTER_SEARCH_API_URL = 'http://search.twitter.com/search.json'
|
13
|
+
TWITTER_TRENDS_API_URL = 'http://search.twitter.com/trends/current.json'
|
45
14
|
TWITTER_API_DEFAULT_TIMEOUT = 5
|
46
15
|
|
47
16
|
attr_accessor :agent
|
17
|
+
attr_accessor :timeout
|
48
18
|
|
49
|
-
def initialize(agent = 'twitter-search')
|
19
|
+
def initialize(agent = 'twitter-search', timeout = TWITTER_API_DEFAULT_TIMEOUT)
|
50
20
|
@agent = agent
|
21
|
+
@timeout = timeout
|
51
22
|
end
|
52
23
|
|
53
24
|
def headers
|
@@ -55,14 +26,10 @@ module TwitterSearch
|
|
55
26
|
"User-Agent" => @agent }
|
56
27
|
end
|
57
28
|
|
58
|
-
def timeout
|
59
|
-
TWITTER_API_DEFAULT_TIMEOUT
|
60
|
-
end
|
61
|
-
|
62
29
|
def query(opts = {})
|
63
|
-
url = URI.parse(
|
30
|
+
url = URI.parse(TWITTER_SEARCH_API_URL)
|
64
31
|
url.query = sanitize_query(opts)
|
65
|
-
|
32
|
+
|
66
33
|
req = Net::HTTP::Get.new(url.path)
|
67
34
|
http = Net::HTTP.new(url.host, url.port)
|
68
35
|
http.read_timeout = timeout
|
@@ -72,6 +39,22 @@ module TwitterSearch
|
|
72
39
|
}.body
|
73
40
|
Tweets.new JSON.parse(json)
|
74
41
|
end
|
42
|
+
|
43
|
+
def trends(opts = {})
|
44
|
+
url = URI.parse(TWITTER_TRENDS_API_URL)
|
45
|
+
if opts['exclude_hashtags']
|
46
|
+
url.query = sanitize_query_hash({ :exclude_hashtags => opts['exclude_hashtags'] })
|
47
|
+
end
|
48
|
+
|
49
|
+
req = Net::HTTP::Get.new(url.path)
|
50
|
+
http = Net::HTTP.new(url.host, url.port)
|
51
|
+
http.read_timeout = timeout
|
52
|
+
|
53
|
+
json = http.start { |http|
|
54
|
+
http.get("#{url.path}?#{url.query}", headers)
|
55
|
+
}.body
|
56
|
+
Trends.new JSON.parse(json)
|
57
|
+
end
|
75
58
|
|
76
59
|
private
|
77
60
|
|
metadata
CHANGED
@@ -1,16 +1,22 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dancroak-twitter-search
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dustin Sallings
|
8
8
|
- Dan Croak
|
9
|
+
- Luke Francl
|
10
|
+
- Matt Sanford
|
11
|
+
- Alejandro Crosa
|
12
|
+
- Danny Burkes
|
13
|
+
- Don Brown
|
14
|
+
- HotFusionMan
|
9
15
|
autorequire:
|
10
16
|
bindir: bin
|
11
17
|
cert_chain: []
|
12
18
|
|
13
|
-
date: 2009-
|
19
|
+
date: 2009-05-14 21:00:00 -07:00
|
14
20
|
default_executable:
|
15
21
|
dependencies:
|
16
22
|
- !ruby/object:Gem::Dependency
|
@@ -32,12 +38,14 @@ extensions: []
|
|
32
38
|
extra_rdoc_files: []
|
33
39
|
|
34
40
|
files:
|
41
|
+
- CHANGELOG.textile
|
35
42
|
- Rakefile
|
36
43
|
- README.markdown
|
37
44
|
- TODO.markdown
|
38
|
-
-
|
45
|
+
- lib/trends.rb
|
46
|
+
- lib/tweets.rb
|
39
47
|
- lib/twitter_search.rb
|
40
|
-
has_rdoc:
|
48
|
+
has_rdoc: true
|
41
49
|
homepage: http://github.com/dancroak/twitter-search
|
42
50
|
post_install_message:
|
43
51
|
rdoc_options: []
|
@@ -61,7 +69,7 @@ requirements: []
|
|
61
69
|
rubyforge_project:
|
62
70
|
rubygems_version: 1.2.0
|
63
71
|
signing_key:
|
64
|
-
specification_version:
|
72
|
+
specification_version: 3
|
65
73
|
summary: Ruby client for Twitter Search.
|
66
74
|
test_files: []
|
67
75
|
|