forked from redbooth/teambox-ruby-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTODO.example.rb
327 lines (246 loc) · 7.8 KB
/
TODO.example.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# This are methods that we didn't implement on Teambox, but sound like good ideas
# or reference methods
def friendship_destroy(id)
perform_post("/friendships/destroy/#{id}.json")
end
def verify_credentials
perform_get("/account/verify_credentials.json")
end
def users(*ids_or_usernames)
ids, usernames = [], []
ids_or_usernames.each do |id_or_username|
if id_or_username.is_a?(Integer)
ids << id_or_username
elsif id_or_username.is_a?(String)
usernames << id_or_username
end
end
query = {}
query[:user_id] = ids.join(",") unless ids.empty?
query[:screen_name] = usernames.join(",") unless usernames.empty?
perform_get("/users/lookup.json", :query => query)
end
def direct_message_destroy(id)
perform_post("/direct_messages/destroy/#{id}.json")
end
# Options: id, user_id, screen_name
# Could be adapted to Teambox as "people in my projects"
def friend_ids(query={})
perform_get("/friends/ids.json", :query => query)
end
# Options: in_reply_to_status_id
def update(status, query={})
perform_post("/statuses/update.json", :body => {:status => status}.merge(query))
end
# Options: since_id, max_id, count, page
# options: count, page, ids_only
# Options: id, user_id, screen_name, page
# :per_page = max number of statues to get at once
# :page = which page of tweets you wish to get
def list_timeline(list_owner_username, slug, query = {})
perform_get("/#{list_owner_username}/lists/#{slug}/statuses.json", :query => query)
end
def list_create(list_owner_username, options)
perform_post("/#{list_owner_username}/lists.json", :body => {:user => list_owner_username}.merge(options))
end
def enable_notifications(id)
perform_post("/notifications/follow/#{id}.json")
end
def disable_notifications(id)
perform_post("/notifications/leave/#{id}.json")
end
def rate_limit_status
perform_get("/account/rate_limit_status.json")
end
# One or more of the following must be present:
# name, email, url, location, description
def update_profile(body={})
perform_post("/account/update_profile.json", :body => body)
end
require 'pp'
module Teambox
class Search
include HTTParty
include Enumerable
base_uri "search.teambox.com/search"
format :json
attr_reader :result, :query
def initialize(q=nil, options={})
@options = options
clear
containing(q) if q && q.strip != ""
endpoint_url = options[:api_endpoint]
endpoint_url = "#{endpoint_url}/search" if endpoint_url && !endpoint_url.include?("/search")
self.class.base_uri(endpoint_url) if endpoint_url
end
def user_agent
@options[:user_agent] || "Ruby Teambox Gem"
end
def from(user, exclude=false)
@query[:q] << "#{exclude ? "-" : ""}from:#{user}"
self
end
def to(user, exclude=false)
@query[:q] << "#{exclude ? "-" : ""}to:#{user}"
self
end
def referencing(user, exclude=false)
@query[:q] << "#{exclude ? "-" : ""}@#{user}"
self
end
alias :references :referencing
alias :ref :referencing
def containing(word, exclude=false)
@query[:q] << "#{exclude ? "-" : ""}#{word}"
self
end
alias :contains :containing
# adds filtering based on hash tag ie: #teambox
def hashed(tag, exclude=false)
@query[:q] << "#{exclude ? "-" : ""}\##{tag}"
self
end
# Search for a phrase instead of a group of words
def phrase(phrase)
@query[:phrase] = phrase
self
end
# lang must be ISO 639-1 code ie: en, fr, de, ja, etc.
#
# when I tried en it limited my results a lot and took
# out several tweets that were english so i'd avoid
# this unless you really want it
def lang(lang)
@query[:lang] = lang
self
end
# popular|recent
def result_type(result_type)
@query[:result_type] = result_type
self
end
# Limits the number of results per page
def per_page(num)
@query[:rpp] = num
self
end
# Which page of results to fetch
def page(num)
@query[:page] = num
self
end
# Only searches tweets since a given id.
# Recommended to use this when possible.
def since(since_id)
@query[:since_id] = since_id
self
end
# From the advanced search form, not documented in the API
# Format YYYY-MM-DD
def since_date(since_date)
@query[:since] = since_date
self
end
# From the advanced search form, not documented in the API
# Format YYYY-MM-DD
def until_date(until_date)
@query[:until] = until_date
self
end
# Ranges like 25km and 50mi work.
def geocode(lat, long, range)
@query[:geocode] = [lat, long, range].join(",")
self
end
def max(id)
@query[:max_id] = id
self
end
# Clears all the query filters to make a new search
def clear
@fetch = nil
@query = {}
@query[:q] = []
self
end
def fetch(force=false)
if @fetch.nil? || force
query = @query.dup
query[:q] = query[:q].join(" ")
perform_get(query)
end
@fetch
end
def each
results = fetch()['results']
return if results.nil?
results.each {|r| yield r}
end
def next_page?
!!fetch()["next_page"]
end
def fetch_next_page
if next_page?
s = Search.new(nil, :user_agent => user_agent)
s.perform_get(fetch()["next_page"][1..-1])
s
end
end
protected
def perform_get(query)
response = self.class.get("#{self.class.base_uri}.json", :query => query, :format => :json, :headers => {"User-Agent" => user_agent})
@fetch = Teambox.mash(response)
end
end
end
module Teambox
class OAuth
extend Forwardable
def_delegators :access_token, :get, :post, :put, :delete
attr_reader :ctoken, :csecret, :consumer_options, :api_endpoint, :signing_endpoint
# Options
# :sign_in => true to just sign in with teambox instead of doing oauth authorization
# (http://apiwiki.teambox.com/Sign-in-with-Teambox)
def initialize(ctoken, csecret, options={})
@ctoken, @csecret, @consumer_options = ctoken, csecret, {}
@api_endpoint = options[:api_endpoint] || 'http://api.teambox.com'
@signing_endpoint = options[:signing_endpoint] || 'http://api.teambox.com'
if options[:sign_in]
@consumer_options[:authorize_path] = '/oauth/authenticate'
end
end
def consumer
@consumer ||= ::OAuth::Consumer.new(@ctoken, @csecret, {:site => api_endpoint}.merge(consumer_options))
end
def signing_consumer
@signing_consumer ||= ::OAuth::Consumer.new(@ctoken, @csecret, {:site => signing_endpoint, :request_endpoint => api_endpoint }.merge(consumer_options))
end
def set_callback_url(url)
clear_request_token
request_token(:oauth_callback => url)
end
# Note: If using oauth with a web app, be sure to provide :oauth_callback.
# Options:
# :oauth_callback => String, url that teambox should redirect to
def request_token(options={})
@request_token ||= signing_consumer.get_request_token(options)
end
# For web apps use params[:oauth_verifier], for desktop apps,
# use the verifier is the pin that teambox gives users.
def authorize_from_request(rtoken, rsecret, verifier_or_pin)
request_token = ::OAuth::RequestToken.new(signing_consumer, rtoken, rsecret)
access_token = request_token.get_access_token(:oauth_verifier => verifier_or_pin)
@atoken, @asecret = access_token.token, access_token.secret
end
def access_token
@access_token ||= ::OAuth::AccessToken.new(signing_consumer, @atoken, @asecret)
end
def authorize_from_access(atoken, asecret)
@atoken, @asecret = atoken, asecret
end
private
def clear_request_token
@request_token = nil
end
end
end