rb_hash_tag 0.0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 005dae2b14d1b9b9d25a6474dcdd89f9aacfd1dc
4
+ data.tar.gz: e384a937f17733271f90f44b4b4819078bc286fa
5
+ SHA512:
6
+ metadata.gz: a2bfe72a03990401cfa9b67b8ac0d8e78b7abec2b945605b34ba12842b4d7b33de58538300016ab62e7e87355659af23fa3ac6f20d7f32442ded3b83871410e0
7
+ data.tar.gz: bf7965d9f705e70f0f1bfefd646f860e830cdeb00db0d30f0e7f236a6aec3a41f8ef3229a19536dd950aea4af04a1b93b9e6458ca6c2bc2a8040cf19d8ffdd7d
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ rb_hash_tag
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rb_hash_tag.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Richard
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # RbHashTag
2
+
3
+ This gem will allow you to output recent tweets for a hash tag.
4
+
5
+ Answers to a few questions:
6
+ * Why does this exist?
7
+ + I wanted to write a gem that had an executable.
8
+ * Is this useful?
9
+ + Probably not.
10
+
11
+
12
+ ## Installation
13
+
14
+ Install it yourself as:
15
+
16
+ $ gem install rb_hash_tag
17
+
18
+ ## Usage
19
+ To get recent tweets with the hash tag #ruby_on_rails simply type:
20
+
21
+ $ rb_hash_tag ruby_on_rails
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/rb_hash_tag ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Load Gem
4
+ lib = File.expand_path(File.dirname(__FILE__) + '/../lib/rb_hash_tag')
5
+ require lib
6
+
7
+ args = ARGV
8
+ if args.empty?
9
+ puts 'No search term provided.'
10
+ puts 'USAGE: rb_hash_tag SEARCH_TERM'
11
+ end
12
+
13
+ RbHashTag.tweets(args.first)
@@ -0,0 +1,29 @@
1
+ class RbHashTag::TweetFormatter
2
+
3
+ attr_reader :tweets, :output
4
+
5
+ def initialize(tweets = [], output = $stdout)
6
+ @tweets = tweets
7
+ @output = output
8
+ end
9
+
10
+ def exec
11
+ tweets.each do |tweet|
12
+ output.write(blue("** #{tweet.user}\n"))
13
+ output.write(" - #{formatted_tweet_text(tweet.text)}\n")
14
+ output.write("\n")
15
+ end
16
+ end
17
+
18
+ def formatted_tweet_text(tweet_text)
19
+ tweet_text.gsub(/\#(\w|\d)+/) { |hash_tag| green(hash_tag) }
20
+ end
21
+
22
+ def blue(msg)
23
+ "\e[34m#{msg}\e[0m"
24
+ end
25
+
26
+ def green(msg)
27
+ "\e[32m#{msg}\e[0m"
28
+ end
29
+ end
@@ -0,0 +1,50 @@
1
+ require 'open-uri'
2
+ require 'uri'
3
+ require 'nokogiri'
4
+
5
+ class RbHashTag::TweetReader
6
+
7
+ attr_reader :search_term
8
+
9
+ def initialize(opts = {})
10
+ @search_term = opts[:search_term]
11
+ end
12
+
13
+ Tweet = Struct.new(:text, :user)
14
+ def exec
15
+ raw_tweet_data.inject([]) do |memo, tweet_data|
16
+ memo << Tweet.new(text(tweet_data), user(tweet_data))
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def raw_tweet_data
23
+ return [] unless valid?
24
+ page_data = Nokogiri::HTML(open(url))
25
+ page_data.css('.js-stream-item.stream-item.expanding-stream-item')
26
+ rescue Exception => e
27
+ return []
28
+ end
29
+
30
+ def valid?
31
+ search_term && !search_term.empty?
32
+ end
33
+
34
+ def text(tweet)
35
+ tweet.at_css('.tweet-text').text.strip
36
+ end
37
+
38
+ def user(tweet)
39
+ tweet.at_css('.username.js-action-profile-name').text.strip
40
+ end
41
+
42
+ def url
43
+ "https://twitter.com/search?q=#{encoded_search_term}&src=typd"
44
+ end
45
+
46
+ def encoded_search_term
47
+ URI.encode("##{search_term}")
48
+ end
49
+
50
+ end
@@ -0,0 +1,3 @@
1
+ module RbHashTag
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,19 @@
1
+ require "rb_hash_tag/version"
2
+ require 'rb_hash_tag/tweet_reader'
3
+ require 'rb_hash_tag/tweet_formatter'
4
+ module RbHashTag
5
+
6
+ def self.tweets(hash_tag)
7
+ formatted_tweets(tweets_for_hash_tag(hash_tag))
8
+ end
9
+
10
+ private
11
+
12
+ def self.tweets_for_hash_tag(hash_tag)
13
+ RbHashTag::TweetReader.new(search_term: hash_tag).exec
14
+ end
15
+
16
+ def self.formatted_tweets(tweets)
17
+ RbHashTag::TweetFormatter.new(tweets).exec
18
+ end
19
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rb_hash_tag/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rb_hash_tag"
8
+ spec.version = RbHashTag::VERSION
9
+ spec.authors = ["Richard"]
10
+ spec.email = ["me@richardhalldev.com"]
11
+ spec.description = %q{Get recent tweets for a specific hash tag from the command line.}
12
+ spec.summary = %q{Get recent tweets for a specific hash tag from the command line.}
13
+ spec.homepage = "https://github.com/richardardrichard/rb_hash_tag"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency 'rspec', '~> 2.14'
24
+ spec.add_development_dependency 'mocha', '~> 0.14'
25
+
26
+ spec.add_dependency 'nokogiri', '~> 1.6'
27
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe RbHashTag::TweetFormatter do
4
+
5
+ let!(:output_stream) do
6
+ result = mock('output_stream')
7
+ result.stubs(:write)
8
+ result
9
+ end
10
+
11
+ describe 'when there are no tweets' do
12
+ it 'should not write anything to the output stream' do
13
+ output_stream.expects(:write).never
14
+ RbHashTag::TweetFormatter.new([], output_stream).exec
15
+ end
16
+ end
17
+
18
+ describe 'when there are tweets' do
19
+ let(:tweet) do
20
+ result = mock('tweet')
21
+ result.stubs(:text).returns('this is a tweet')
22
+ result.stubs(:user).returns('twitter_user')
23
+ result
24
+ end
25
+
26
+ it 'should write the tweet user' do
27
+ output_stream.expects(:write).with(regexp_matches(/twitter_user/))
28
+ RbHashTag::TweetFormatter.new([tweet], output_stream).exec
29
+ end
30
+
31
+ it 'should write the tweet text' do
32
+ output_stream.expects(:write).with(regexp_matches(/this\sis\sa\stweet/))
33
+ RbHashTag::TweetFormatter.new([tweet], output_stream).exec
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ describe RbHashTag::TweetReader do
4
+ let(:html) do
5
+ <<-HTML
6
+ <div class="js-stream-item stream-item expanding-stream-item">
7
+ <div class="username js-action-profile-name">
8
+ @ruby_test
9
+ </div>
10
+ <div class="tweet-text">
11
+ Whats up guys?
12
+ </div>
13
+ </div>
14
+ HTML
15
+ end
16
+
17
+ describe '#feed' do
18
+ describe 'when the contet is found' do
19
+ before(:each) do
20
+ RbHashTag::TweetReader.any_instance.stubs(:open).returns(html)
21
+ end
22
+
23
+ let!(:data) { RbHashTag::TweetReader.new(search_term: 'RoR').exec }
24
+
25
+ it 'should have the proper username' do
26
+ data.first.text.should == 'Whats up guys?'
27
+ end
28
+
29
+ it 'should have the proper text' do
30
+ data.first.user.should == '@ruby_test'
31
+ end
32
+ end
33
+
34
+ describe 'when the correct content can not be found' do
35
+ before(:each) do
36
+ RbHashTag::TweetReader.any_instance.stubs(:open).returns("<!--#{html}-->")
37
+ end
38
+
39
+ it 'should return empty' do
40
+ RbHashTag::TweetReader.new(search_term: 'RoR').exec.empty?.should be_true
41
+ end
42
+ end
43
+
44
+ describe 'when there is no search term' do
45
+ it 'should return empty' do
46
+ RbHashTag::TweetReader.new.exec.empty?.should be_true
47
+ end
48
+ end
49
+
50
+ describe 'when there is a connection error' do
51
+ before(:each) { RbHashTag::TweetReader.any_instance.stubs(:open).throws(Exception) }
52
+
53
+ it 'should return empty' do
54
+ RbHashTag::TweetReader.new(search_term: 'RoR').exec.empty?.should be_true
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,7 @@
1
+ Dir['./lib/**/*.rb'].map { |f| require f }
2
+
3
+ require 'rubygems'
4
+
5
+ RSpec.configure do |config|
6
+ config.mock_with :mocha
7
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rb_hash_tag
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Richard
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '2.14'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '2.14'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mocha
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '0.14'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '0.14'
69
+ - !ruby/object:Gem::Dependency
70
+ name: nokogiri
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '1.6'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '1.6'
83
+ description: Get recent tweets for a specific hash tag from the command line.
84
+ email:
85
+ - me@richardhalldev.com
86
+ executables:
87
+ - rb_hash_tag
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - .gitignore
92
+ - .rspec
93
+ - .ruby-gemset
94
+ - .ruby-version
95
+ - Gemfile
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - bin/rb_hash_tag
100
+ - lib/rb_hash_tag.rb
101
+ - lib/rb_hash_tag/tweet_formatter.rb
102
+ - lib/rb_hash_tag/tweet_reader.rb
103
+ - lib/rb_hash_tag/version.rb
104
+ - rb_hash_tag.gemspec
105
+ - spec/rb_hash_tag/tweet_formatter_spec.rb
106
+ - spec/rb_hash_tag/tweet_reader_spec.rb
107
+ - spec/spec_helper.rb
108
+ homepage: https://github.com/richardardrichard/rb_hash_tag
109
+ licenses:
110
+ - MIT
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 2.0.3
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: Get recent tweets for a specific hash tag from the command line.
132
+ test_files:
133
+ - spec/rb_hash_tag/tweet_formatter_spec.rb
134
+ - spec/rb_hash_tag/tweet_reader_spec.rb
135
+ - spec/spec_helper.rb