gilesbowkett-twitter 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. data/History +126 -0
  2. data/License +19 -0
  3. data/Manifest +69 -0
  4. data/README +84 -0
  5. data/Rakefile +42 -0
  6. data/bin/twitter +14 -0
  7. data/examples/blocks.rb +15 -0
  8. data/examples/direct_messages.rb +29 -0
  9. data/examples/favorites.rb +20 -0
  10. data/examples/friends_followers.rb +25 -0
  11. data/examples/friendships.rb +13 -0
  12. data/examples/identica_timeline.rb +7 -0
  13. data/examples/location.rb +8 -0
  14. data/examples/posting.rb +9 -0
  15. data/examples/replies.rb +27 -0
  16. data/examples/search.rb +18 -0
  17. data/examples/sent_messages.rb +27 -0
  18. data/examples/timeline.rb +34 -0
  19. data/examples/twitter.rb +27 -0
  20. data/examples/verify_credentials.rb +13 -0
  21. data/lib/twitter/base.rb +284 -0
  22. data/lib/twitter/cli/config.rb +9 -0
  23. data/lib/twitter/cli/helpers.rb +109 -0
  24. data/lib/twitter/cli/migrations/20080722194500_create_accounts.rb +13 -0
  25. data/lib/twitter/cli/migrations/20080722194508_create_tweets.rb +16 -0
  26. data/lib/twitter/cli/migrations/20080722214605_add_account_id_to_tweets.rb +9 -0
  27. data/lib/twitter/cli/migrations/20080722214606_create_configurations.rb +13 -0
  28. data/lib/twitter/cli/models/account.rb +33 -0
  29. data/lib/twitter/cli/models/configuration.rb +13 -0
  30. data/lib/twitter/cli/models/tweet.rb +20 -0
  31. data/lib/twitter/cli.rb +334 -0
  32. data/lib/twitter/direct_message.rb +30 -0
  33. data/lib/twitter/easy_class_maker.rb +43 -0
  34. data/lib/twitter/rate_limit_status.rb +19 -0
  35. data/lib/twitter/search.rb +101 -0
  36. data/lib/twitter/search_result.rb +83 -0
  37. data/lib/twitter/search_result_info.rb +82 -0
  38. data/lib/twitter/status.rb +22 -0
  39. data/lib/twitter/user.rb +37 -0
  40. data/lib/twitter/version.rb +3 -0
  41. data/lib/twitter.rb +32 -0
  42. data/spec/base_spec.rb +139 -0
  43. data/spec/cli/helper_spec.rb +49 -0
  44. data/spec/direct_message_spec.rb +40 -0
  45. data/spec/fixtures/follower_ids.xml +11 -0
  46. data/spec/fixtures/followers.xml +706 -0
  47. data/spec/fixtures/friend_ids.xml +12 -0
  48. data/spec/fixtures/friends.xml +609 -0
  49. data/spec/fixtures/friends_for.xml +584 -0
  50. data/spec/fixtures/friends_lite.xml +192 -0
  51. data/spec/fixtures/friends_timeline.xml +66 -0
  52. data/spec/fixtures/friendship_already_exists.xml +5 -0
  53. data/spec/fixtures/friendship_created.xml +12 -0
  54. data/spec/fixtures/public_timeline.xml +148 -0
  55. data/spec/fixtures/rate_limit_status.xml +7 -0
  56. data/spec/fixtures/search_result_info.yml +147 -0
  57. data/spec/fixtures/search_results.json +1 -0
  58. data/spec/fixtures/status.xml +25 -0
  59. data/spec/fixtures/user.xml +38 -0
  60. data/spec/fixtures/user_timeline.xml +465 -0
  61. data/spec/search_spec.rb +100 -0
  62. data/spec/spec.opts +1 -0
  63. data/spec/spec_helper.rb +23 -0
  64. data/spec/status_spec.rb +40 -0
  65. data/spec/user_spec.rb +42 -0
  66. data/twitter.gemspec +45 -0
  67. data/website/css/common.css +47 -0
  68. data/website/images/terminal_output.png +0 -0
  69. data/website/index.html +147 -0
  70. metadata +188 -0
@@ -0,0 +1,334 @@
1
+ require 'rubygems'
2
+
3
+ gem 'main', '>= 2.8.2'
4
+ gem 'highline', '>= 1.4.0'
5
+ gem 'activerecord', '= 2.2.2'
6
+ gem 'sqlite3-ruby', '>= 1.2.1'
7
+
8
+ require 'main'
9
+ require 'highline/import'
10
+ require 'activerecord'
11
+ require 'sqlite3'
12
+
13
+ HighLine.track_eof = false
14
+ CLI_ROOT = File.expand_path(File.join(File.dirname(__FILE__), 'cli'))
15
+ require CLI_ROOT + '/config'
16
+ require CLI_ROOT + '/helpers'
17
+ Dir[CLI_ROOT + '/models/*.rb'].each { |m| require m }
18
+
19
+ include Twitter::CLI::Helpers
20
+
21
+ Main {
22
+ def run
23
+ puts "twitter [command] --help for usage instructions."
24
+ puts "The available commands are: \n install, uninstall, add, remove, list, change, post, befriend, defriend, follow, leave, d and timeline."
25
+ end
26
+
27
+ mode 'install' do
28
+ description 'Creates the sqlite3 database and runs the migrations.'
29
+ def run
30
+ migrate
31
+ attempt_import
32
+ say 'Twitter installed.'
33
+ end
34
+ end
35
+
36
+ mode 'uninstall' do
37
+ description 'Removes the sqlite3 database. There is no undo for this.'
38
+ def run
39
+ FileUtils.rm(Twitter::CLI::Config[:database]) if File.exists?(Twitter::CLI::Config[:database])
40
+ say 'Twitter gem uninstalled.'
41
+ end
42
+ end
43
+
44
+ mode 'add' do
45
+ description 'Adds a new twitter account to the database. Prompts for username and password.'
46
+ argument('username', 'u') {
47
+ optional
48
+ description 'optional username'
49
+ }
50
+ argument('password', 'p') {
51
+ optional
52
+ description 'optional password'
53
+ }
54
+
55
+ def run
56
+ account = Hash.new
57
+ say "Add New Account:"
58
+
59
+ # allows optional username arg
60
+ if params['username'].given?
61
+ account[:username] = params['username'].value
62
+ else
63
+ account[:username] = ask('Username: ') do |q|
64
+ q.validate = /\S+/
65
+ end
66
+ end
67
+
68
+ # allows optional password arg
69
+ if params['password'].given?
70
+ account[:password] = params['password'].value
71
+ else
72
+ account[:password] = ask("Password (won't be displayed): ") do |q|
73
+ q.echo = false
74
+ q.validate = /\S+/
75
+ end
76
+ end
77
+
78
+ do_work do
79
+ base(account[:username], account[:password]).verify_credentials
80
+ Account.add(account)
81
+ say 'Account added.'
82
+ end
83
+ end
84
+ end
85
+
86
+ mode 'remove' do
87
+ description 'Removes a twitter account from the database. If username provided it removes that username else it prompts with list and asks for which one you would like to remove.'
88
+ argument( 'username' ) {
89
+ optional
90
+ description 'username of account you would like to remove'
91
+ }
92
+
93
+ def run
94
+ do_work do
95
+ if params['username'].given?
96
+ account = Account.find_by_username(params['username'].value)
97
+ else
98
+ Account.find(:all, :order => 'username').each do |a|
99
+ say "#{a.id}. #{a}"
100
+ end
101
+ account_id = ask 'Account to remove (enter number): ' do |q|
102
+ q.validate = /\d+/
103
+ end
104
+ end
105
+
106
+ begin
107
+ account = account_id ? Account.find(account_id) : account
108
+ account_name = account.username
109
+ account.destroy
110
+ Account.set_current(Account.first) if Account.new_active_needed?
111
+ say "#{account_name} has been removed.\n"
112
+ rescue ActiveRecord::RecordNotFound
113
+ say "ERROR: Account could not be found. Try again. \n"
114
+ end
115
+ end
116
+ end
117
+ end
118
+
119
+ mode 'list' do
120
+ description 'Lists all the accounts that have been added and puts a * by the current one that is used for posting, etc.'
121
+ def run
122
+ do_work do
123
+ if Account.count == 0
124
+ say 'No accounts have been added.'
125
+ else
126
+ say 'Account List'
127
+ Account.find(:all, :order => 'username').each do |a|
128
+ say a
129
+ end
130
+ end
131
+ end
132
+ end
133
+ end
134
+
135
+ mode 'change' do
136
+ description 'Changes the current account being used for posting etc. to the username provided. If no username is provided, a list is presented and you can choose the account from there.'
137
+ argument( 'username' ) {
138
+ optional
139
+ description 'username of account you would like to switched to'
140
+ }
141
+
142
+ def run
143
+ do_work do
144
+ if params['username'].given?
145
+ new_current = Account.find_by_username(params['username'].value)
146
+ else
147
+ Account.find(:all, :order => 'username').each do |a|
148
+ say "#{a.id}. #{a}"
149
+ end
150
+ new_current = ask 'Change current account to (enter number): ' do |q|
151
+ q.validate = /\d+/
152
+ end
153
+ end
154
+
155
+ begin
156
+ current = Account.set_current(new_current)
157
+ say "#{current} is now the current account.\n"
158
+ rescue ActiveRecord::RecordNotFound
159
+ say "ERROR: Account could not be found. Try again. \n"
160
+ end
161
+ end
162
+ end
163
+ end
164
+
165
+ mode 'post' do
166
+ description "Posts a message to twitter using the current account. The following are all valid examples from the command line:
167
+ $ twitter post 'my update'
168
+ $ twitter post my update with quotes
169
+ $ echo 'my update from stdin' | twitter post"
170
+ def run
171
+ do_work do
172
+ post = ARGV.size > 1 ? ARGV.join(" ") : ARGV.shift
173
+ say "Sending twitter update"
174
+ finished, status = false, nil
175
+ progress_thread = Thread.new { until finished; print "."; $stdout.flush; sleep 0.5; end; }
176
+ post_thread = Thread.new(binding()) do |b|
177
+ status = base.post(post, :source => Twitter::SourceName)
178
+ finished = true
179
+ end
180
+ post_thread.join
181
+ progress_thread.join
182
+ say "Got it! New tweet created at: #{status.created_at}\n"
183
+ end
184
+ end
185
+ end
186
+
187
+ mode 'befriend' do
188
+ description "Allows you to add a user as a friend"
189
+ argument('username') {
190
+ required
191
+ description 'username or id of twitterrer to befriend'
192
+ }
193
+
194
+ def run
195
+ do_work do
196
+ username = params['username'].value
197
+ base.create_friendship(username)
198
+ say "#{username} has been added as a friend. follow notifications with 'twitter follow #{username}'"
199
+ end
200
+ end
201
+ end
202
+
203
+ mode 'defriend' do
204
+ description "Allows you to remove a user from being a friend"
205
+ argument('username') {
206
+ required
207
+ description 'username or id of twitterrer to defriend'
208
+ }
209
+
210
+ def run
211
+ do_work do
212
+ username = params['username'].value
213
+ base.destroy_friendship(username)
214
+ say "#{username} has been removed from your friends"
215
+ end
216
+ end
217
+ end
218
+
219
+ mode 'follow' do
220
+ description "Allows you to add notifications for a user (aka Follow Them)"
221
+ argument('username') {
222
+ required
223
+ description 'username or id of twitterrer to follow'
224
+ }
225
+
226
+ def run
227
+ do_work do
228
+ username = params['username'].value
229
+ base.follow(username)
230
+ say "You are now following notifications from #{username}"
231
+ end
232
+ end
233
+ end
234
+
235
+ mode 'leave' do
236
+ description "Allows you to turn off notifications for a user"
237
+ argument('username') {
238
+ required
239
+ description 'username or id of twitterrer to leave'
240
+ }
241
+
242
+ def run
243
+ do_work do
244
+ username = params['username'].value
245
+ base.leave(username)
246
+ say "You are no longer following notifications from #{username}"
247
+ end
248
+ end
249
+ end
250
+
251
+ mode 'd' do
252
+ description "Allows you to direct message a user. The following are all valid examples from the command line:
253
+ $ twitter d jnunemaker 'yo homeboy'
254
+ $ twitter d jnunemaker yo homeboy
255
+ $ echo 'yo homeboy' | twitter d jnunemaker"
256
+ argument('username') {
257
+ required
258
+ description 'username or id of twitterrer to direct message'
259
+ }
260
+
261
+ def run
262
+ do_work do
263
+ username = params['username'].value
264
+ post = ARGV.size > 1 ? ARGV.join(" ") : ARGV.shift
265
+ base.d(username, post)
266
+ say "Direct message sent to #{username}"
267
+ end
268
+ end
269
+ end
270
+
271
+ mode 'timeline' do
272
+ description "Allows you to view your timeline, your friends or the public one"
273
+ argument( 'timeline' ) {
274
+ description 'the timeline you wish to see (friends, public, me)'
275
+ default 'friends'
276
+ }
277
+ option('force', 'f') {
278
+ description "Ignore since_id and show first page of results even if there aren't new ones"
279
+ }
280
+ option('reverse', 'r') {
281
+ description 'Reverse the output so the oldest tweets are at the top'
282
+ }
283
+
284
+ def run
285
+ do_work do
286
+ timeline = params['timeline'].value == 'me' ? 'user' : params['timeline'].value
287
+ options, since_id = {}, Configuration["#{timeline}_since_id"]
288
+ options[:since_id] = since_id if !since_id.nil? && !params['force'].given?
289
+ reverse = params['reverse'].given? ? true : false
290
+ cache = [:friends, :user].include?(timeline)
291
+ collection = base.timeline(timeline.to_sym, options)
292
+ output_tweets(collection, {:cache => cache, :since_prefix => timeline, :reverse => reverse})
293
+ end
294
+ end
295
+ end
296
+
297
+ mode 'replies' do
298
+ description 'Allows you to view all @replies sent to you'
299
+ option('force', 'f') {
300
+ description "Ignore since_id and show first page of replies even if there aren't new ones"
301
+ }
302
+
303
+ def run
304
+ do_work do
305
+ options, since_id = {}, Configuration["replies_since_id"]
306
+ options[:since_id] = since_id if !since_id.nil? && !params['force'].given?
307
+ collection = base.replies(options)
308
+ output_tweets(collection, {:since_prefix => 'replies'})
309
+ end
310
+ end
311
+ end
312
+
313
+ mode 'clear_config' do
314
+ def run
315
+ do_work do
316
+ count = Configuration.count
317
+ Configuration.destroy_all
318
+ say("#{count} configuration entries cleared.")
319
+ end
320
+ end
321
+ end
322
+
323
+ mode 'open' do
324
+ description 'Opens the given twitter user in a browser window'
325
+ argument('username') {
326
+ required
327
+ description "username or id of twitterrer who's page you would like to see"
328
+ }
329
+
330
+ def run
331
+ `open http://twitter.com/#{params['username'].value}`
332
+ end
333
+ end
334
+ }
@@ -0,0 +1,30 @@
1
+ module Twitter
2
+ class DirectMessage
3
+ include EasyClassMaker
4
+
5
+ attributes :id,
6
+ :text,
7
+ :sender_id,
8
+ :recipient_id,
9
+ :created_at,
10
+ :sender_screen_name,
11
+ :recipient_screen_name,
12
+ :sender_profile_image_url
13
+
14
+ class << self
15
+ # Creates a new status from a piece of xml
16
+ def new_from_xml(xml)
17
+ DirectMessage.new do |d|
18
+ d.id = (xml).at('id').innerHTML
19
+ d.text = (xml).get_elements_by_tag_name('text').innerHTML
20
+ d.sender_id = (xml).at('sender_id').innerHTML
21
+ d.recipient_id = (xml).at('recipient_id').innerHTML
22
+ d.created_at = (xml).at('created_at').innerHTML
23
+ d.sender_screen_name = (xml).at('sender_screen_name').innerHTML
24
+ d.recipient_screen_name = (xml).at('recipient_screen_name').innerHTML
25
+ d.sender_profile_image_url = (xml).at('sender').at('profile_image_url').innerHTML
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,43 @@
1
+ # This is pretty much just a macro for creating a class that allows
2
+ # using a block to initialize stuff and to define getters and setters
3
+ # really quickly.
4
+ module Twitter
5
+ module EasyClassMaker
6
+
7
+ def self.included(base)
8
+ base.extend(ClassMethods)
9
+ end
10
+
11
+ module ClassMethods
12
+ # creates the attributes class variable and creates each attribute's accessor methods
13
+ def attributes(*attrs)
14
+ @@attributes = attrs
15
+ @@attributes.each { |a| attr_accessor a }
16
+ end
17
+
18
+ # read method for attributes class variable
19
+ def self.attributes; @@attributes end
20
+ end
21
+
22
+ # allows for any class that includes this to use a block to initialize
23
+ # variables instead of assigning each one seperately
24
+ #
25
+ # Example:
26
+ #
27
+ # instead of...
28
+ #
29
+ # s = Status.new
30
+ # s.foo = 'thing'
31
+ # s.bar = 'another thing'
32
+ #
33
+ # you can ...
34
+ #
35
+ # Status.new do |s|
36
+ # s.foo = 'thing'
37
+ # s.bar = 'another thing'
38
+ # end
39
+ def initialize
40
+ yield self if block_given?
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,19 @@
1
+ module Twitter
2
+ class RateLimitStatus
3
+ include EasyClassMaker
4
+
5
+ attributes :reset_time_in_seconds, :reset_time, :remaining_hits, :hourly_limit
6
+
7
+ class << self
8
+ # Creates a new rate limi status from a piece of xml
9
+ def new_from_xml(xml)
10
+ RateLimitStatus.new do |s|
11
+ s.reset_time_in_seconds = xml.at('reset-time-in-seconds').inner_html.to_i
12
+ s.reset_time = Time.parse xml.at('reset-time').inner_html
13
+ s.remaining_hits = xml.at('remaining-hits').inner_html.to_i
14
+ s.hourly_limit = xml.at('hourly-limit').inner_html.to_i
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,101 @@
1
+ gem 'httparty'
2
+ require 'httparty'
3
+
4
+ module Twitter
5
+ class Search
6
+ include HTTParty
7
+ include Enumerable
8
+ base_uri 'search.twitter.com'
9
+
10
+ attr_reader :result, :query
11
+
12
+ def initialize(q=nil)
13
+ clear
14
+ containing(q) if q && q.strip != ''
15
+ end
16
+
17
+ def from(user)
18
+ @query[:q] << "from:#{user}"
19
+ self
20
+ end
21
+
22
+ def to(user)
23
+ @query[:q] << "to:#{user}"
24
+ self
25
+ end
26
+
27
+ def referencing(user)
28
+ @query[:q] << "@#{user}"
29
+ self
30
+ end
31
+ alias :references :referencing
32
+ alias :ref :referencing
33
+
34
+ def containing(word)
35
+ @query[:q] << "#{word}"
36
+ self
37
+ end
38
+ alias :contains :containing
39
+
40
+ # adds filtering based on hash tag ie: #twitter
41
+ def hashed(tag)
42
+ @query[:q] << "##{tag}"
43
+ self
44
+ end
45
+
46
+ # lang must be ISO 639-1 code ie: en, fr, de, ja, etc.
47
+ #
48
+ # when I tried en it limited my results a lot and took
49
+ # out several tweets that were english so i'd avoid
50
+ # this unless you really want it
51
+ def lang(lang)
52
+ @query[:lang] = lang
53
+ self
54
+ end
55
+
56
+ # Limits the number of results per page
57
+ def per_page(num)
58
+ @query[:rpp] = num
59
+ self
60
+ end
61
+
62
+ # Which page of results to fetch
63
+ def page(num)
64
+ @query[:page] = num
65
+ self
66
+ end
67
+
68
+ # Only searches tweets since a given id.
69
+ # Recommended to use this when possible.
70
+ def since(since_id)
71
+ @query[:since_id] = since_id
72
+ self
73
+ end
74
+
75
+ # Search tweets by longitude, latitude and a given range.
76
+ # Ranges like 25km and 50mi work.
77
+ def geocode(long, lat, range)
78
+ @query[:geocode] = [long, lat, range].join(',')
79
+ self
80
+ end
81
+
82
+ # Clears all the query filters to make a new search
83
+ def clear
84
+ @query = {}
85
+ @query[:q] = []
86
+ self
87
+ end
88
+
89
+ # If you want to get results do something other than iterate over them.
90
+ def fetch
91
+ @query[:q] = @query[:q].join(' ')
92
+ SearchResultInfo.new_from_hash(self.class.get('/search.json', {:query => @query}))
93
+ end
94
+
95
+ def each
96
+ @result = fetch()
97
+ @result['results'].each { |r| yield r }
98
+ end
99
+ end
100
+ end
101
+
@@ -0,0 +1,83 @@
1
+ module Twitter
2
+ class SearchResult < Hash
3
+
4
+ # Creates an easier to work with hash from
5
+ # one with string-based keys
6
+ def self.new_from_hash(hash)
7
+ new.merge!(hash)
8
+ end
9
+
10
+ def created_at
11
+ self['created_at']
12
+ end
13
+
14
+ def created_at=(val)
15
+ self['created_at'] = val
16
+ end
17
+
18
+ def from_user
19
+ self['from_user']
20
+ end
21
+
22
+ def from_user=(val)
23
+ self['from_user'] = val
24
+ end
25
+
26
+ def from_user_id
27
+ self['from_user_id']
28
+ end
29
+
30
+ def from_user_id=(val)
31
+ self['from_user_id'] = val
32
+ end
33
+
34
+ def id
35
+ self['id']
36
+ end
37
+
38
+ def id=(val)
39
+ self['id'] = val
40
+ end
41
+
42
+ def iso_language_code
43
+ self['iso_language_code']
44
+ end
45
+
46
+ def iso_language_code=(val)
47
+ self['iso_language_code'] = val
48
+ end
49
+
50
+ def profile_image_url
51
+ self['profile_image_url']
52
+ end
53
+
54
+ def profile_image_url=(val)
55
+ self['profile_image_url'] = val
56
+ end
57
+
58
+ def text
59
+ self['text']
60
+ end
61
+
62
+ def text=(val)
63
+ self['text'] = val
64
+ end
65
+
66
+ def to_user
67
+ self['to_user']
68
+ end
69
+
70
+ def to_user=(val)
71
+ self['to_user'] = val
72
+ end
73
+
74
+ def to_user_id
75
+ self['to_user_id']
76
+ end
77
+
78
+ def to_user_id=(val)
79
+ self['to_user_id'] = val
80
+ end
81
+
82
+ end
83
+ end
@@ -0,0 +1,82 @@
1
+ module Twitter
2
+ class SearchResultInfo < Hash
3
+
4
+ # Creates an easier to work with hash from
5
+ # one with string-based keys
6
+ def self.new_from_hash(hash)
7
+ i = new
8
+ i.merge!(hash)
9
+ search_results = []
10
+ i.results.each do |r|
11
+ search_results << SearchResult.new_from_hash(r)
12
+ end
13
+ i.results = search_results
14
+ i
15
+ end
16
+
17
+ def completed_in
18
+ self['completed_in']
19
+ end
20
+
21
+ def completed_in=(val)
22
+ self['completed_in'] = val
23
+ end
24
+
25
+ def max_id
26
+ self['max_id']
27
+ end
28
+
29
+ def max_id=(val)
30
+ self['max_id'] = val
31
+ end
32
+
33
+ def next_page
34
+ self['next_page']
35
+ end
36
+
37
+ def next_page=(val)
38
+ self['next_page'] = val
39
+ end
40
+
41
+ def page
42
+ self['page']
43
+ end
44
+
45
+ def page=(val)
46
+ self['page'] = val
47
+ end
48
+
49
+ def refresh_url
50
+ self['refresh_url']
51
+ end
52
+
53
+ def refresh_url=(val)
54
+ self['refresh_url'] = val
55
+ end
56
+
57
+ def results_per_page
58
+ self['results_per_page']
59
+ end
60
+
61
+ def results_per_page=(val)
62
+ self['results_per_page'] = val
63
+ end
64
+
65
+ def since_id
66
+ self['since_id']
67
+ end
68
+
69
+ def since_id=(val)
70
+ self['since_id'] = val
71
+ end
72
+
73
+ def results
74
+ self['results']
75
+ end
76
+
77
+ def results=(val)
78
+ self['results'] = val
79
+ end
80
+
81
+ end
82
+ end