twitter_hashtag 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ .DS_Store
2
+ pkg/*
3
+ *.gem
4
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source :rubygems
2
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,44 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ twitter_hashtag (0.0.1)
5
+ json (~> 1.4.6)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ aruba (0.2.6)
11
+ background_process
12
+ cucumber (~> 0.9.4)
13
+ background_process (1.2)
14
+ builder (2.1.2)
15
+ cucumber (0.9.4)
16
+ builder (~> 2.1.2)
17
+ diff-lcs (~> 1.1.2)
18
+ gherkin (~> 2.2.9)
19
+ json (~> 1.4.6)
20
+ term-ansicolor (~> 1.0.5)
21
+ diff-lcs (1.1.2)
22
+ gherkin (2.2.9)
23
+ json (~> 1.4.6)
24
+ term-ansicolor (~> 1.0.5)
25
+ json (1.4.6)
26
+ rspec (2.2.0)
27
+ rspec-core (~> 2.2)
28
+ rspec-expectations (~> 2.2)
29
+ rspec-mocks (~> 2.2)
30
+ rspec-core (2.2.1)
31
+ rspec-expectations (2.2.0)
32
+ diff-lcs (~> 1.1.2)
33
+ rspec-mocks (2.2.0)
34
+ term-ansicolor (1.0.5)
35
+
36
+ PLATFORMS
37
+ ruby
38
+
39
+ DEPENDENCIES
40
+ aruba (~> 0.2.6)
41
+ cucumber (~> 0.9.4)
42
+ json (~> 1.4.6)
43
+ rspec (~> 2.2.0)
44
+ twitter_hashtag!
data/README.markdown ADDED
@@ -0,0 +1,3 @@
1
+ `gem install twitter_hashtag`
2
+
3
+ `twitter_hashtag help`
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'twitter_hashtag/cli'
4
+
5
+ args = ARGV.dup
6
+ ARGV.clear
7
+ hashtag = args.shift.strip rescue 'help'
8
+
9
+ TwitterHashtag::CLI.new(hashtag, args).process
@@ -0,0 +1,43 @@
1
+ Feature: Run twitter_hashtag
2
+
3
+ twitter_hashtag is a command line tool that allows you to
4
+ view tweets that contain the given hashtag.
5
+
6
+ Scenario: View tweets that contain the given hashtag
7
+ When I run "twitter_hashtag dog"
8
+ Then the output should contain:
9
+ """
10
+ #dog
11
+ http://twitter.com
12
+ """
13
+
14
+ Scenario: Pass a hashtag with a pound symbol
15
+ When I run "twitter_hashtag #dog"
16
+ Then the output should contain "#dog"
17
+
18
+ Scenario: Pass a hashtag with a rpp (limit) option of 5
19
+ When I run "twitter_hashtag dog --rpp 5"
20
+ Then the output should contain 5 tweets
21
+
22
+ Scenario: Pass a hashtag with a rpp (limit) option of 1
23
+ When I run "twitter_hashtag #dog --rpp 1"
24
+ Then the output should contain 1 tweet
25
+
26
+ Scenario: Pass a hashtag that returns zero tweets
27
+ When I run "twitter_hashtag thisdoesnotexistnopenosir"
28
+ Then the output should contain "None found."
29
+
30
+ Scenario: Run twitter_hashtag help
31
+ When I run "twitter_hashtag help"
32
+ Then the output should contain exactly:
33
+ """
34
+ Pass a hashtag (with or without #) as an argument to twitter_hashtag.
35
+ Example: twitter_hashtag #dog OR twitter_hashtag dog
36
+ You may pass --rpp to limit the number of returned tweets.
37
+ Enjoy!
38
+
39
+ """
40
+
41
+ Scenario: Forget to pass a hashtag
42
+ When I run "twitter_hashtag"
43
+ Then the output should contain "Pass a hashtag"
@@ -0,0 +1,5 @@
1
+ require 'aruba'
2
+
3
+ Then /^the output should contain (.+) tweets?$/ do |num|
4
+ all_output.scan(/(~)\s+/).size.should eq(num.to_i)
5
+ end
@@ -0,0 +1,34 @@
1
+ require 'twitter_hashtag/help'
2
+ require 'twitter_hashtag/request'
3
+ require 'twitter_hashtag/formatter'
4
+
5
+ module TwitterHashtag
6
+ class CLI
7
+
8
+ def initialize(hashtag, args)
9
+ @hashtag = hashtag
10
+ @args = args
11
+ end
12
+
13
+ attr_reader :hashtag, :args
14
+
15
+ def process
16
+ hashtag == 'help' ? render_help : search
17
+ end
18
+
19
+ private
20
+
21
+ def search
22
+ format TwitterHashtag::Request.new(self).issue
23
+ end
24
+
25
+ def format(response)
26
+ TwitterHashtag::Formatter.new(response).format
27
+ end
28
+
29
+ def render_help
30
+ TwitterHashtag::Help.output
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,21 @@
1
+ require 'twitter_hashtag/tweet'
2
+
3
+ module TwitterHashtag
4
+ class Formatter
5
+
6
+ def initialize(response)
7
+ @results = response['results']
8
+ end
9
+
10
+ def format
11
+ if @results.empty?
12
+ puts 'None found.'
13
+ exit 1
14
+ else
15
+ puts 'Results:'
16
+ TwitterHashtag::Tweet.output(@results)
17
+ end
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,14 @@
1
+ module TwitterHashtag
2
+ class Help
3
+
4
+ def self.output
5
+ puts <<-STR
6
+ Pass a hashtag (with or without #) as an argument to twitter_hashtag.
7
+ Example: twitter_hashtag #dog OR twitter_hashtag dog
8
+ You may pass --rpp to limit the number of returned tweets.
9
+ Enjoy!
10
+ STR
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,48 @@
1
+ require 'net/http'
2
+ require 'cgi'
3
+ require 'json'
4
+
5
+ module TwitterHashtag
6
+ class Request
7
+
8
+ SEARCH_URI = 'search.twitter.com'
9
+
10
+ def initialize(cli)
11
+ @cli = cli
12
+ end
13
+
14
+ attr_reader :cli
15
+
16
+ def issue
17
+ JSON.parse Net::HTTP.get(SEARCH_URI, path)
18
+ end
19
+
20
+ private
21
+
22
+ def path
23
+ "/search.json?#{params}"
24
+ end
25
+
26
+ def params
27
+ to_url_params default_params.merge(clean_args_hash)
28
+ end
29
+
30
+ def default_params
31
+ {'q' => formatted_hashtag, 'rpp' => 100}
32
+ end
33
+
34
+ def clean_args_hash
35
+ Hash[*cli.args.map {|a| a.delete('--')}]
36
+ end
37
+
38
+ def formatted_hashtag
39
+ CGI.escape '#'.concat(cli.hashtag.delete('#'))
40
+ end
41
+
42
+ def to_url_params(params_hash)
43
+ params_hash.to_a.map {|key, value|
44
+ "#{key}=#{value}"
45
+ }.join('&')
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,39 @@
1
+ module TwitterHashtag
2
+ class Tweet
3
+
4
+ SEPARATOR = '~'*80
5
+
6
+ def self.output(results)
7
+ results.each do |result_line|
8
+ new(result_line).output
9
+ end
10
+ end
11
+
12
+ def initialize(tweet_hash)
13
+ @tweet_hash = tweet_hash
14
+ end
15
+
16
+ def output
17
+ puts [SEPARATOR, user, text, link].join("\n")
18
+ end
19
+
20
+ private
21
+
22
+ def user
23
+ @tweet_hash['from_user']
24
+ end
25
+
26
+ def text
27
+ @tweet_hash['text']
28
+ end
29
+
30
+ def id
31
+ @tweet_hash['id_str']
32
+ end
33
+
34
+ def link
35
+ "http://twitter.com/#{user}/status/#{id}"
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,3 @@
1
+ module TwitterHashtag
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1 @@
1
+ module TwitterHashtag end
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "twitter_hashtag/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "twitter_hashtag"
7
+ s.version = TwitterHashtag::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Justin Ko"]
10
+ s.email = ["jko170@gmail.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{Search twitter hashtags from the command line}
13
+ s.description = %q{Search twitter hashtags from the command line}
14
+
15
+ s.rubyforge_project = "twitter_hashtag"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {spec,features}/*`.split("\n")
19
+ s.executables = %w(twitter_hashtag)
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_runtime_dependency 'json', '~> 1.4.6'
23
+
24
+ s.add_development_dependency 'rspec', '~> 2.2.0'
25
+ s.add_development_dependency 'cucumber', '~> 0.9.4'
26
+ s.add_development_dependency 'aruba', '~> 0.2.6'
27
+ end
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: twitter_hashtag
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Justin Ko
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-12-07 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: json
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 11
30
+ segments:
31
+ - 1
32
+ - 4
33
+ - 6
34
+ version: 1.4.6
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: rspec
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ hash: 7
46
+ segments:
47
+ - 2
48
+ - 2
49
+ - 0
50
+ version: 2.2.0
51
+ type: :development
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: cucumber
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ hash: 51
62
+ segments:
63
+ - 0
64
+ - 9
65
+ - 4
66
+ version: 0.9.4
67
+ type: :development
68
+ version_requirements: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ name: aruba
71
+ prerelease: false
72
+ requirement: &id004 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ hash: 27
78
+ segments:
79
+ - 0
80
+ - 2
81
+ - 6
82
+ version: 0.2.6
83
+ type: :development
84
+ version_requirements: *id004
85
+ description: Search twitter hashtags from the command line
86
+ email:
87
+ - jko170@gmail.com
88
+ executables:
89
+ - twitter_hashtag
90
+ extensions: []
91
+
92
+ extra_rdoc_files: []
93
+
94
+ files:
95
+ - .gitignore
96
+ - Gemfile
97
+ - Gemfile.lock
98
+ - README.markdown
99
+ - Rakefile
100
+ - bin/twitter_hashtag
101
+ - features/run_twitter_hashtag.feature
102
+ - features/step_definitions/aruba.rb
103
+ - lib/twitter_hashtag.rb
104
+ - lib/twitter_hashtag/cli.rb
105
+ - lib/twitter_hashtag/formatter.rb
106
+ - lib/twitter_hashtag/help.rb
107
+ - lib/twitter_hashtag/request.rb
108
+ - lib/twitter_hashtag/tweet.rb
109
+ - lib/twitter_hashtag/version.rb
110
+ - twitter_hashtag.gemspec
111
+ has_rdoc: true
112
+ homepage: ""
113
+ licenses: []
114
+
115
+ post_install_message:
116
+ rdoc_options: []
117
+
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ hash: 3
126
+ segments:
127
+ - 0
128
+ version: "0"
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ none: false
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ hash: 3
135
+ segments:
136
+ - 0
137
+ version: "0"
138
+ requirements: []
139
+
140
+ rubyforge_project: twitter_hashtag
141
+ rubygems_version: 1.3.7
142
+ signing_key:
143
+ specification_version: 3
144
+ summary: Search twitter hashtags from the command line
145
+ test_files:
146
+ - features/run_twitter_hashtag.feature
147
+ - features/step_definitions/aruba.rb