twords 0.1.0

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: 14f8d19e5e919775262f1711702693d2d9c1bd04
4
+ data.tar.gz: 997c565ad48498bac50db80fde8e56ed2a1d4ff8
5
+ SHA512:
6
+ metadata.gz: 535d6f11f585e85465fd42f03e068f126935678b597c1f689e37149e797c5149718f629bb0cdaa448dfe9b397b3a2f84ab66d5c02452c5d8981c8cca59ceed8e
7
+ data.tar.gz: 5c6eb12f69e02413868c931b8f85bb8d74b58846e1ba0d02c91b2d72d3014bca115c443bbcac5eeb28c6f4f3d1b570294eaa19264806e8dd7810d3444073e86e
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 M. Simon Borg
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # Twords
2
+
3
+ ## Twitter word clouds
4
+
5
+ Count the occurrences of words in a tweeter's tweets.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'twords'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install twords
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ Twords.config do |config|
27
+ config.throw_aways = %w[the for and a i of if]
28
+ config.range = 14
29
+ config.up_to { Time.now } # A time object to be lazy evaluated. The range is counted backward from here.
30
+
31
+ config.twitter_client do |twitter|
32
+ twitter.consumer_key = YOUR_TWITTER_CONSUMER_KEY
33
+ twitter.consumer_secret = YOUR_TWITTER_CONSUMER_SECRET
34
+ twitter.access_token = YOUR_TWITTER_ACCESS_TOKEN
35
+ twitter.access_token_secret = YOUR_TWITTER_ACCESS_TOKEN_SECRET
36
+ end
37
+ end
38
+
39
+ twords = Twords.new 'user_one', 'user_two'
40
+
41
+ twords.audit
42
+ # => true
43
+
44
+ twords.words
45
+ # => { "butts"=>35, "poo"=>32, "pups"=>28, ... }
46
+ ```
47
+
48
+ ## Development
49
+
50
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
51
+
52
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
53
+
54
+ ## Contributing
55
+
56
+ Bug reports and pull requests are welcome on GitHub at https://github.com/msimonborg/twords.
57
+
58
+
59
+ ## License
60
+
61
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
62
+
data/lib/twords.rb ADDED
@@ -0,0 +1,140 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'date'
4
+ require 'twitter'
5
+
6
+ require 'twords/version'
7
+
8
+ # Twords.config do |config|
9
+ # config.throw_aways = %w[the for and a i of if]
10
+ # config.max_age = 14
11
+ # config.up_to { Time.now }
12
+ #
13
+ # config.twitter_client do |twitter|
14
+ # twitter.consumer_key = YOUR_TWITTER_CONSUMER_KEY
15
+ # twitter.consumer_secret = YOUR_TWITTER_CONSUMER_SECRET
16
+ # twitter.access_token = YOUR_TWITTER_ACCESS_TOKEN
17
+ # twitter.access_token_secret = YOUR_TWITTER_ACCESS_TOKEN_SECRET
18
+ # end
19
+ # end
20
+ #
21
+ # twords = Twords.new 'user_one', 'user_two'
22
+ #
23
+ # twords.audit
24
+ # # => true
25
+ #
26
+ # twords.words
27
+ # # => { "butts"=>35, "poo"=>32, "pups"=>28, ... }
28
+ class Twords
29
+ class << self
30
+ attr_reader :throw_aways, :range, :client, :up_to_block
31
+
32
+ def config(&block)
33
+ class_eval(&block)
34
+ end
35
+
36
+ def twitter_client(&block)
37
+ @client = Twitter::REST::Client.new(&block)
38
+ end
39
+
40
+ def throw_aways=(*args)
41
+ @throw_aways = args.flatten
42
+ end
43
+
44
+ def range=(integer)
45
+ @range = integer
46
+ end
47
+
48
+ def up_to(&time_block)
49
+ raise ArgumentError, 'object must respond to #call' unless time_block.respond_to?(:call)
50
+ @up_to_block = time_block
51
+ end
52
+ end
53
+
54
+ attr_reader :screen_names, :words, :requests, :client
55
+
56
+ def initialize(*screen_names)
57
+ @screen_names = screen_names
58
+ @words = {}
59
+ @requests = 0
60
+ end
61
+
62
+ def client
63
+ @_client ||= self.class.client
64
+ end
65
+
66
+ def range
67
+ @_range ||= self.class.range
68
+ end
69
+
70
+ def audited?
71
+ @audited
72
+ end
73
+
74
+ def sort_words
75
+ words.sort { |a, b| b.last <=> a.last }
76
+ end
77
+
78
+ def timeline
79
+ @_timeline ||= screen_names.map { |name| fetch_timeline(name) }.flatten
80
+ end
81
+
82
+ # Make two cursored API calls to fetch the 400 most recent tweets
83
+ def fetch_timeline(screen_name)
84
+ return [] if screen_name.to_s.empty?
85
+ @requests += 1
86
+ timeline = client.user_timeline(screen_name, count: 200)
87
+ return timeline if timeline.empty?
88
+ fetch_older_tweets(timeline, screen_name)
89
+ end
90
+
91
+ def fetch_older_tweets(timeline, screen_name)
92
+ return timeline if age_of_tweet_in_days(timeline.last) > range
93
+ @requests += 1
94
+ timeline += client.user_timeline(
95
+ screen_name,
96
+ max_id: timeline.last.id - 1,
97
+ count: 200
98
+ )
99
+ fetch_older_tweets(timeline, screen_name)
100
+ end
101
+
102
+ def recent_tweets
103
+ @_recent_tweets ||= timeline.each_with_object([]) do |tweet, memo|
104
+ memo << tweet if age_of_tweet_in_days(tweet) <= range
105
+ end.sort { |a, b| b.created_at <=> a.created_at }
106
+ end
107
+
108
+ def age_of_tweet_in_days(tweet)
109
+ (self.class.up_to_block.call - tweet.created_at) / 60 / 60 / 24
110
+ end
111
+
112
+ def count_words
113
+ recent_tweets.each do |tweet|
114
+ tweet_with_full_text = fetch_tweet_with_full_text(tweet)
115
+ words_array = tweet_with_full_text.attrs[:full_text].downcase.split(' ')
116
+ words_array.each do |word|
117
+ next if self.class.throw_aways.include?(word)
118
+ if words.has_key?(word)
119
+ words[word] += 1
120
+ else
121
+ words[word] = 1
122
+ end
123
+ end
124
+ end
125
+ end
126
+
127
+ def fetch_tweet_with_full_text(tweet)
128
+ @requests += 1
129
+ client.status(tweet.id, tweet_mode: 'extended')
130
+ end
131
+
132
+ def audit
133
+ count_words unless audited?
134
+ @audited = true
135
+ end
136
+
137
+ def recent_tweets_count
138
+ @_recent_tweets_count ||= recent_tweets.count
139
+ end
140
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Twords
4
+ VERSION = '0.1.0'
5
+ end
data/twords.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ lib = File.expand_path('../lib', __FILE__)
5
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
+ require 'twords/version'
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.name = 'twords'
10
+ spec.version = Twords::VERSION
11
+ spec.authors = ['M. Simon Borg']
12
+ spec.email = ['msimonborg@gmail.com']
13
+
14
+ spec.summary = 'Twitter word clouds'
15
+ spec.description = 'Twitter word clouds'
16
+ spec.homepage = 'https://github.com/msimonborg/twords'
17
+ spec.license = 'MIT'
18
+
19
+ spec.files = `git ls-files -z lib LICENSE.txt README.md twords.gemspec`.split("\0")
20
+
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_dependency 'twitter', '~> 6.1.0'
26
+
27
+ spec.add_development_dependency 'bundler', '~> 1.14'
28
+ spec.add_development_dependency 'rake', '~> 10.0'
29
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: twords
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - M. Simon Borg
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-06-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: twitter
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 6.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 6.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.14'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.14'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: Twitter word clouds
56
+ email:
57
+ - msimonborg@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - LICENSE.txt
63
+ - README.md
64
+ - lib/twords.rb
65
+ - lib/twords/version.rb
66
+ - twords.gemspec
67
+ homepage: https://github.com/msimonborg/twords
68
+ licenses:
69
+ - MIT
70
+ metadata: {}
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubyforge_project:
87
+ rubygems_version: 2.6.11
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: Twitter word clouds
91
+ test_files: []