neufelry-twitter 0.4.2

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.
Files changed (68) hide show
  1. data/History +122 -0
  2. data/License +19 -0
  3. data/Manifest +66 -0
  4. data/README +84 -0
  5. data/Rakefile +40 -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.rb +42 -0
  22. data/lib/twitter/base.rb +270 -0
  23. data/lib/twitter/cli.rb +334 -0
  24. data/lib/twitter/cli/config.rb +9 -0
  25. data/lib/twitter/cli/helpers.rb +109 -0
  26. data/lib/twitter/cli/migrations/20080722194500_create_accounts.rb +13 -0
  27. data/lib/twitter/cli/migrations/20080722194508_create_tweets.rb +16 -0
  28. data/lib/twitter/cli/migrations/20080722214605_add_account_id_to_tweets.rb +9 -0
  29. data/lib/twitter/cli/migrations/20080722214606_create_configurations.rb +13 -0
  30. data/lib/twitter/cli/models/account.rb +33 -0
  31. data/lib/twitter/cli/models/configuration.rb +13 -0
  32. data/lib/twitter/cli/models/tweet.rb +20 -0
  33. data/lib/twitter/direct_message.rb +22 -0
  34. data/lib/twitter/easy_class_maker.rb +43 -0
  35. data/lib/twitter/rate_limit_status.rb +19 -0
  36. data/lib/twitter/search.rb +101 -0
  37. data/lib/twitter/search_result.rb +83 -0
  38. data/lib/twitter/search_result_info.rb +82 -0
  39. data/lib/twitter/status.rb +22 -0
  40. data/lib/twitter/user.rb +37 -0
  41. data/lib/twitter/version.rb +3 -0
  42. data/spec/base_spec.rb +127 -0
  43. data/spec/cli/helper_spec.rb +49 -0
  44. data/spec/direct_message_spec.rb +35 -0
  45. data/spec/fixtures/followers.xml +706 -0
  46. data/spec/fixtures/friends.xml +609 -0
  47. data/spec/fixtures/friends_for.xml +584 -0
  48. data/spec/fixtures/friends_lite.xml +192 -0
  49. data/spec/fixtures/friends_timeline.xml +66 -0
  50. data/spec/fixtures/friendship_already_exists.xml +5 -0
  51. data/spec/fixtures/friendship_created.xml +12 -0
  52. data/spec/fixtures/public_timeline.xml +148 -0
  53. data/spec/fixtures/rate_limit_status.xml +7 -0
  54. data/spec/fixtures/search_result_info.yml +147 -0
  55. data/spec/fixtures/search_results.json +1 -0
  56. data/spec/fixtures/status.xml +25 -0
  57. data/spec/fixtures/user.xml +38 -0
  58. data/spec/fixtures/user_timeline.xml +465 -0
  59. data/spec/search_spec.rb +100 -0
  60. data/spec/spec.opts +1 -0
  61. data/spec/spec_helper.rb +23 -0
  62. data/spec/status_spec.rb +40 -0
  63. data/spec/user_spec.rb +42 -0
  64. data/twitter.gemspec +45 -0
  65. data/website/css/common.css +47 -0
  66. data/website/images/terminal_output.png +0 -0
  67. data/website/index.html +159 -0
  68. metadata +181 -0
@@ -0,0 +1,13 @@
1
+ class CreateAccounts < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :accounts do |t|
4
+ t.string :username, :password
5
+ t.boolean :current
6
+ t.timestamps
7
+ end
8
+ end
9
+
10
+ def self.down
11
+ drop_table :accounts
12
+ end
13
+ end
@@ -0,0 +1,16 @@
1
+ class CreateTweets < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :tweets do |t|
4
+ t.datetime :occurred_at
5
+ t.boolean :truncated, :favorited, :user_protected, :default => false
6
+ t.integer :twitter_id, :user_id, :in_reply_to_status_id, :in_reply_to_user_id, :user_followers_count
7
+ t.text :body
8
+ t.string :source, :user_name, :user_screen_name, :user_location, :user_description, :user_profile_image_url, :user_url
9
+ t.timestamps
10
+ end
11
+ end
12
+
13
+ def self.down
14
+ drop_table :tweets
15
+ end
16
+ end
@@ -0,0 +1,9 @@
1
+ class AddAccountIdToTweets < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :tweets, :account_id, :integer
4
+ end
5
+
6
+ def self.down
7
+ remove_column :tweets, :account_id
8
+ end
9
+ end
@@ -0,0 +1,13 @@
1
+ class CreateConfigurations < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :configurations do |t|
4
+ t.string :key
5
+ t.text :data
6
+ t.timestamps
7
+ end
8
+ end
9
+
10
+ def self.down
11
+ drop_table :accounts
12
+ end
13
+ end
@@ -0,0 +1,33 @@
1
+ class Account < ActiveRecord::Base
2
+ named_scope :current, :conditions => {:current => true}
3
+
4
+ has_many :tweets, :dependent => :destroy
5
+
6
+ def self.add(hash)
7
+ username = hash.delete(:username)
8
+ account = find_or_initialize_by_username(username)
9
+ account.attributes = hash
10
+ account.save
11
+ set_current(account) if new_active_needed?
12
+ end
13
+
14
+ def self.active
15
+ current.first
16
+ end
17
+
18
+ def self.set_current(account_or_id)
19
+ account = account_or_id.is_a?(Account) ? account_or_id : find(account_or_id)
20
+ account.update_attribute :current, true
21
+ Account.update_all "current = 0", "id != #{account.id}"
22
+ account
23
+ end
24
+
25
+ def self.new_active_needed?
26
+ self.current.count == 0 && self.count > 0
27
+ end
28
+
29
+ def to_s
30
+ "#{current? ? '*' : ' '} #{username}"
31
+ end
32
+ alias to_str to_s
33
+ end
@@ -0,0 +1,13 @@
1
+ class Configuration < ActiveRecord::Base
2
+ serialize :data
3
+
4
+ def self.[](key)
5
+ key = find_by_key(key.to_s)
6
+ key.nil? ? nil : key.data
7
+ end
8
+
9
+ def self.[]=(key, data)
10
+ c = find_or_create_by_key(key.to_s)
11
+ c.update_attribute :data, data
12
+ end
13
+ end
@@ -0,0 +1,20 @@
1
+ class Tweet < ActiveRecord::Base
2
+ belongs_to :account
3
+
4
+ def self.create_from_tweet(account, s)
5
+ tweet = account.tweets.find_or_initialize_by_twitter_id(s.id)
6
+ tweet.body = s.text
7
+ tweet.occurred_at = s.created_at
8
+
9
+ %w[truncated favorited in_reply_to_status_id in_reply_to_user_id source].each do |m|
10
+ tweet.send("#{m}=", s.send(m))
11
+ end
12
+
13
+ %w[id followers_count name screen_name location description
14
+ profile_image_url url protected].each do |m|
15
+ tweet.send("user_#{m}=", s.user.send(m))
16
+ end
17
+ tweet.save!
18
+ tweet
19
+ end
20
+ end
@@ -0,0 +1,22 @@
1
+ module Twitter
2
+ class DirectMessage
3
+ include EasyClassMaker
4
+
5
+ attributes :id, :text, :sender_id, :recipient_id, :created_at, :sender_screen_name, :recipient_screen_name
6
+
7
+ class << self
8
+ # Creates a new status from a piece of xml
9
+ def new_from_xml(xml)
10
+ DirectMessage.new do |d|
11
+ d.id = (xml).at('id').innerHTML
12
+ d.text = (xml).get_elements_by_tag_name('text').innerHTML
13
+ d.sender_id = (xml).at('sender_id').innerHTML
14
+ d.recipient_id = (xml).at('recipient_id').innerHTML
15
+ d.created_at = (xml).at('created_at').innerHTML
16
+ d.sender_screen_name = (xml).at('sender_screen_name').innerHTML
17
+ d.recipient_screen_name = (xml).at('recipient_screen_name').innerHTML
18
+ end
19
+ end
20
+ end
21
+ end
22
+ 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