cicloid-twitter 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,19 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'twitter')
2
+ require File.join(File.dirname(__FILE__), 'helpers', 'config_store')
3
+ require 'pp'
4
+
5
+ config = ConfigStore.new("#{ENV['HOME']}/.twitter")
6
+
7
+ oauth = Twitter::OAuth.new(config['token'], config['secret'])
8
+ oauth.authorize_from_access(config['atoken'], config['asecret'])
9
+
10
+ client = Twitter::Base.new(oauth)
11
+
12
+ pp client.friends_timeline
13
+ puts '*'*50
14
+
15
+ pp client.user_timeline
16
+ puts '*'*50
17
+
18
+ pp client.replies
19
+ puts '*'*50
@@ -0,0 +1,11 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'twitter')
2
+ require File.join(File.dirname(__FILE__), 'helpers', 'config_store')
3
+ require 'pp'
4
+
5
+ config = ConfigStore.new("#{ENV['HOME']}/.twitter")
6
+
7
+ oauth = Twitter::OAuth.new(config['token'], config['secret'])
8
+ oauth.authorize_from_access(config['atoken'], config['asecret'])
9
+
10
+ client = Twitter::Base.new(oauth)
11
+ pp client.update('This is an update from the twitter gem')
@@ -0,0 +1,165 @@
1
+ module Twitter
2
+ class Base
3
+ extend Forwardable
4
+
5
+ def_delegators :client, :get, :post
6
+
7
+ attr_reader :client
8
+
9
+ def initialize(client)
10
+ @client = client
11
+ end
12
+
13
+ # Options: since_id, max_id, count, page, since
14
+ def friends_timeline(query={})
15
+ perform_get('/statuses/friends_timeline.json', :query => query)
16
+ end
17
+
18
+ # Options: id, user_id, screen_name, since_id, max_id, page, since
19
+ def user_timeline(query={})
20
+ perform_get('/statuses/user_timeline.json', :query => query)
21
+ end
22
+
23
+ def status(id)
24
+ perform_get("/statuses/show/#{id}.json")
25
+ end
26
+
27
+ # Options: in_reply_to_status_id
28
+ def update(status, query={})
29
+ perform_post("/statuses/update.json", :body => {:status => status}.merge(query))
30
+ end
31
+
32
+ # Options: since_id, max_id, since, page
33
+ def replies(query={})
34
+ perform_get('/statuses/replies.json', :query => query)
35
+ end
36
+
37
+ def status_destroy(id)
38
+ perform_post("/statuses/destroy/#{id}.json")
39
+ end
40
+
41
+ # Options: id, user_id, screen_name, page
42
+ def friends(query={})
43
+ perform_get('/statuses/friends.json', :query => query)
44
+ end
45
+
46
+ # Options: id, user_id, screen_name, page
47
+ def followers(query={})
48
+ perform_get('/statuses/followers.json', :query => query)
49
+ end
50
+
51
+ def user(id)
52
+ perform_get("/users/show/#{id}.json")
53
+ end
54
+
55
+ # Options: since, since_id, page
56
+ def direct_messages(query={})
57
+ perform_get("/direct_messages.json", :query => query)
58
+ end
59
+
60
+ # Options: since, since_id, page
61
+ def direct_messages_sent(query={})
62
+ perform_get("/direct_messages/sent.json", :query => query)
63
+ end
64
+
65
+ def direct_message_create(user, text)
66
+ perform_post("/direct_messages/new.json", :body => {:user => user, :text => text})
67
+ end
68
+
69
+ def direct_message_destroy(id)
70
+ perform_post("/direct_messages/destroy/#{id}.json")
71
+ end
72
+
73
+ def friendship_create(id, follow=false)
74
+ body = {}
75
+ body.merge!(:follow => follow) if follow
76
+ perform_post("/friendships/create/#{id}.json", :body => body)
77
+ end
78
+
79
+ def friendship_destroy(id)
80
+ perform_post("/friendships/destroy/#{id}.json")
81
+ end
82
+
83
+ def friendship_exists?(a, b)
84
+ perform_get("/friendships/exists.json", :query => {:user_a => a, :user_b => b})
85
+ end
86
+
87
+ # Options: id, user_id, screen_name
88
+ def friend_ids(query={})
89
+ perform_get("/friends/ids.json", :query => query)
90
+ end
91
+
92
+ # Options: id, user_id, screen_name
93
+ def follower_ids(query={})
94
+ perform_get("/followers/ids.json", :query => query)
95
+ end
96
+
97
+ def verify_credentials
98
+ perform_get("/account/verify_credentials.json")
99
+ end
100
+
101
+ # Device must be sms, im or none
102
+ def update_delivery_device(device)
103
+ perform_post('/account/update_delivery_device.json', :body => {:device => device})
104
+ end
105
+
106
+ # One or more of the following must be present:
107
+ # profile_background_color, profile_text_color, profile_link_color,
108
+ # profile_sidebar_fill_color, profile_sidebar_border_color
109
+ def update_profile_colors(colors={})
110
+ perform_post('/account/update_profile_colors.json', :body => colors)
111
+ end
112
+
113
+ def rate_limit_status
114
+ perform_get('/account/rate_limit_status.json')
115
+ end
116
+
117
+ # One or more of the following must be present:
118
+ # name, email, url, location, description
119
+ def update_profile(body={})
120
+ perform_post('/account/update_profile.json', :body => body)
121
+ end
122
+
123
+ # Options: id, page
124
+ def favorites(query={})
125
+ perform_get('/favorites.json', :query => query)
126
+ end
127
+
128
+ def favorite_create(id)
129
+ perform_post("/favorites/create/#{id}.json")
130
+ end
131
+
132
+ def favorite_destroy(id)
133
+ perform_post("/favorites/destroy/#{id}.json")
134
+ end
135
+
136
+ def enable_notifications(id)
137
+ perform_post("/notifications/follow/#{id}.json")
138
+ end
139
+
140
+ def disable_notifications(id)
141
+ perform_post("/notifications/leave/#{id}.json")
142
+ end
143
+
144
+ def block(id)
145
+ perform_post("/blocks/create/#{id}.json")
146
+ end
147
+
148
+ def unblock(id)
149
+ perform_post("/blocks/destroy/#{id}.json")
150
+ end
151
+
152
+ def help
153
+ perform_get('/help/test.json')
154
+ end
155
+
156
+ private
157
+ def perform_get(path, options={})
158
+ Twitter::Request.get(self, path, options)
159
+ end
160
+
161
+ def perform_post(path, options={})
162
+ Twitter::Request.post(self, path, options)
163
+ end
164
+ end
165
+ end
@@ -0,0 +1,26 @@
1
+ module Twitter
2
+ class HTTPAuth
3
+ include HTTParty
4
+ base_uri 'http://twitter.com'
5
+ format :plain
6
+
7
+ attr_reader :username, :password
8
+
9
+ def initialize(username, password)
10
+ @username, @password = username, password
11
+ end
12
+
13
+ def get(uri, headers={})
14
+ self.class.get(uri, :headers => headers, :basic_auth => basic_auth)
15
+ end
16
+
17
+ def post(uri, body={}, headers={})
18
+ self.class.post(uri, :body => body, :headers => headers, :basic_auth => basic_auth)
19
+ end
20
+
21
+ private
22
+ def basic_auth
23
+ @basic_auth ||= {:username => @username, :password => @password}
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,34 @@
1
+ module Twitter
2
+ class OAuth
3
+ extend Forwardable
4
+ def_delegators :access_token, :get, :post
5
+
6
+ attr_reader :ctoken, :csecret
7
+
8
+ def initialize(ctoken, csecret)
9
+ @ctoken, @csecret = ctoken, csecret
10
+ end
11
+
12
+ def consumer
13
+ @consumer ||= ::OAuth::Consumer.new(@ctoken, @csecret, {:site => 'http://twitter.com'})
14
+ end
15
+
16
+ def request_token
17
+ @request_token ||= consumer.get_request_token
18
+ end
19
+
20
+ def authorize_from_request(rtoken, rsecret)
21
+ request_token = ::OAuth::RequestToken.new(consumer, rtoken, rsecret)
22
+ access_token = request_token.get_access_token
23
+ @atoken, @asecret = access_token.token, access_token.secret
24
+ end
25
+
26
+ def access_token
27
+ @access_token ||= ::OAuth::AccessToken.new(consumer, @atoken, @asecret)
28
+ end
29
+
30
+ def authorize_from_access(atoken, asecret)
31
+ @atoken, @asecret = atoken, asecret
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,101 @@
1
+ module Twitter
2
+ class Request
3
+ extend Forwardable
4
+
5
+ def self.get(client, path, options={})
6
+ new(client, :get, path, options).perform
7
+ end
8
+
9
+ def self.post(client, path, options={})
10
+ new(client, :post, path, options).perform
11
+ end
12
+
13
+ attr_reader :client, :method, :path, :options
14
+
15
+ def_delegators :client, :get, :post
16
+
17
+ def initialize(client, method, path, options={})
18
+ @client, @method, @path, @options = client, method, path, options
19
+ end
20
+
21
+ def uri
22
+ @uri ||= begin
23
+ uri = URI.parse(path)
24
+
25
+ if options[:query] && options[:query] != {}
26
+ uri.query = to_query(options[:query])
27
+ end
28
+
29
+ uri.to_s
30
+ end
31
+ end
32
+
33
+ def perform
34
+ make_friendly(send("perform_#{method}"))
35
+ end
36
+
37
+ private
38
+ def perform_get
39
+ send(:get, uri, options[:headers])
40
+ end
41
+
42
+ def perform_post
43
+ send(:post, uri, options[:body], options[:headers])
44
+ end
45
+
46
+ def make_friendly(response)
47
+ raise_errors(response)
48
+ mash(parse(response))
49
+ end
50
+
51
+ def raise_errors(response)
52
+ case response.code.to_i
53
+ when 400
54
+ data = parse(response)
55
+ raise RateLimitExceeded.new(data), "(#{response.code}): #{response.message} - #{data['error'] if data}"
56
+ when 401
57
+ data = parse(response)
58
+ raise Unauthorized.new(data), "(#{response.code}): #{response.message} - #{data['error'] if data}"
59
+ when 403
60
+ data = parse(response)
61
+ raise General.new(data), "(#{response.code}): #{response.message} - #{data['error'] if data}"
62
+ when 404
63
+ raise NotFound, "(#{response.code}): #{response.message}"
64
+ when 500
65
+ raise InformTwitter, "Twitter had an internal error. Please let them know in the group. (#{response.code}): #{response.message}"
66
+ when 502..503
67
+ raise Unavailable, "(#{response.code}): #{response.message}"
68
+ end
69
+ end
70
+
71
+ def parse(response)
72
+ Crack::JSON.parse(response.body)
73
+ end
74
+
75
+ def mash(obj)
76
+ if obj.is_a?(Array)
77
+ obj.map { |item| make_mash_with_consistent_hash(item) }
78
+ elsif obj.is_a?(Hash)
79
+ make_mash_with_consistent_hash(obj)
80
+ else
81
+ obj
82
+ end
83
+ end
84
+
85
+ # Lame workaround for the fact that mash doesn't hash correctly
86
+ def make_mash_with_consistent_hash(obj)
87
+ m = Mash.new(obj)
88
+ def m.hash
89
+ inspect.hash
90
+ end
91
+ return m
92
+ end
93
+
94
+ def to_query(options)
95
+ options.inject([]) do |collection, opt|
96
+ collection << "#{opt[0]}=#{opt[1]}"
97
+ collection
98
+ end * '&'
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,101 @@
1
+ module Twitter
2
+ class Search
3
+ include HTTParty
4
+ include Enumerable
5
+
6
+ attr_reader :result, :query
7
+
8
+ def initialize(q=nil)
9
+ clear
10
+ containing(q) if q && q.strip != ''
11
+ end
12
+
13
+ def from(user)
14
+ @query[:q] << "from:#{user}"
15
+ self
16
+ end
17
+
18
+ def to(user)
19
+ @query[:q] << "to:#{user}"
20
+ self
21
+ end
22
+
23
+ def referencing(user)
24
+ @query[:q] << "@#{user}"
25
+ self
26
+ end
27
+ alias :references :referencing
28
+ alias :ref :referencing
29
+
30
+ def containing(word)
31
+ @query[:q] << "#{word}"
32
+ self
33
+ end
34
+ alias :contains :containing
35
+
36
+ # adds filtering based on hash tag ie: #twitter
37
+ def hashed(tag)
38
+ @query[:q] << "##{tag}"
39
+ self
40
+ end
41
+
42
+ # lang must be ISO 639-1 code ie: en, fr, de, ja, etc.
43
+ #
44
+ # when I tried en it limited my results a lot and took
45
+ # out several tweets that were english so i'd avoid
46
+ # this unless you really want it
47
+ def lang(lang)
48
+ @query[:lang] = lang
49
+ self
50
+ end
51
+
52
+ # Limits the number of results per page
53
+ def per_page(num)
54
+ @query[:rpp] = num
55
+ self
56
+ end
57
+
58
+ # Which page of results to fetch
59
+ def page(num)
60
+ @query[:page] = num
61
+ self
62
+ end
63
+
64
+ # Only searches tweets since a given id.
65
+ # Recommended to use this when possible.
66
+ def since(since_id)
67
+ @query[:since_id] = since_id
68
+ self
69
+ end
70
+
71
+ # Search tweets by longitude, latitude and a given range.
72
+ # Ranges like 25km and 50mi work.
73
+ def geocode(long, lat, range)
74
+ @query[:geocode] = [long, lat, range].join(',')
75
+ self
76
+ end
77
+
78
+ # Clears all the query filters to make a new search
79
+ def clear
80
+ @fetch = nil
81
+ @query = {}
82
+ @query[:q] = []
83
+ self
84
+ end
85
+
86
+ def fetch(force=false)
87
+ if @fetch.nil? || force
88
+ query = @query.dup
89
+ query[:q] = query[:q].join(' ')
90
+ response = self.class.get('http://search.twitter.com/search.json', :query => query, :format => :json)
91
+ @fetch = Mash.new(response)
92
+ end
93
+
94
+ @fetch
95
+ end
96
+
97
+ def each
98
+ fetch()['results'].each { |r| yield r }
99
+ end
100
+ end
101
+ end