octopussy 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,5 +1,5 @@
1
- require 'rubygems'
2
1
  require 'rake'
2
+ require 'shoulda/tasks'
3
3
 
4
4
  begin
5
5
  require 'jeweler'
@@ -14,9 +14,9 @@ begin
14
14
  gem.add_dependency('hashie', '~> 0.1.3')
15
15
  gem.add_dependency('httparty', '~> 0.4.5')
16
16
 
17
- gem.add_development_dependency('thoughtbot-shoulda', '>= 2.10.1')
17
+ gem.add_development_dependency('shoulda', '>= 2.10.1')
18
18
  gem.add_development_dependency('jnunemaker-matchy', '0.4.0')
19
- gem.add_development_dependency('mocha', '0.9.4')
19
+ gem.add_development_dependency('mocha', '>= 0.9.4')
20
20
  gem.add_development_dependency('fakeweb', '>= 1.2.5')
21
21
  end
22
22
  Jeweler::GemcutterTasks.new
@@ -26,9 +26,9 @@ end
26
26
 
27
27
  require 'rake/testtask'
28
28
  Rake::TestTask.new(:test) do |test|
29
+ test.ruby_opts = ['-rubygems'] if defined? Gem
29
30
  test.libs << 'lib' << 'test'
30
- test.pattern = 'test/**/test_*.rb'
31
- test.verbose = true
31
+ test.pattern = 'test/**/*_test.rb'
32
32
  end
33
33
 
34
34
  begin
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.4
1
+ 0.2.0
data/changelog.markdown CHANGED
@@ -1,12 +1,25 @@
1
1
  # Changelog
2
+
3
+ ## 0.2.0
4
+ * Commits API courtesy of @enricob
5
+
2
6
  ## 0.1.4
7
+
3
8
  * Preserved links array and content for events parsed from feeds
9
+
4
10
  ## 0.1.3
5
- * Added Download event
11
+
12
+ * Added Download event
13
+
6
14
  ## 0.1.2
7
- * Added Delete event type
8
- * Added Public event type
15
+
16
+ * Added Delete event type
17
+ * Added Public event type
18
+
9
19
  ## 0.1.1
10
- * Added Comment event type
20
+
21
+ * Added Comment event type
22
+
11
23
  ## 0.0.1 Initial version
12
- * GitHub v2 API complete
24
+
25
+ * GitHub v2 API complete
data/lib/octopussy.rb CHANGED
@@ -1,17 +1,19 @@
1
- require 'rubygems'
1
+ require 'forwardable'
2
2
 
3
- gem 'hashie', '~> 0.1.3'
4
- require 'hashie'
5
-
6
- gem 'httparty', '~> 0.4.5'
7
3
  require 'httparty'
8
-
9
- directory = File.expand_path(File.dirname(__FILE__))
10
-
4
+ require 'hashie'
11
5
  Hash.send :include, Hashie::HashExtensions
12
6
 
7
+ libdir = File.dirname(__FILE__)
8
+ $LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
9
+
10
+ require 'octopussy/repo'
11
+ require 'octopussy/event'
12
+ require 'octopussy/client'
13
13
 
14
14
  module Octopussy
15
+ extend SingleForwardable
16
+
15
17
  class OctopussyError < StandardError
16
18
  attr_reader :data
17
19
 
@@ -21,115 +23,35 @@ module Octopussy
21
23
  end
22
24
  end
23
25
 
24
- class RateLimitExceeded < StandardError; end
25
- class Unauthorized < StandardError; end
26
- class General < OctopussyError; end
26
+ class ClientError < StandardError; end
27
+ class ServerError < OctopussyError; end
28
+ class General < OctopussyError; end
29
+
30
+ class RateLimitExceeded < ClientError; end
31
+ class Unauthorized < ClientError; end
32
+ class NotFound < ClientError; end
27
33
 
28
34
  class Unavailable < StandardError; end
29
35
  class InformOctopussy < StandardError; end
30
- class NotFound < StandardError; end
31
-
32
-
33
- def self.search_users(q)
34
- Client.new.search_users(q)
35
- end
36
-
37
- def self.user(login)
38
- Client.new.user(login)
39
- end
40
-
41
- def self.followers(login)
42
- Client.new.followers(login)
43
- end
44
-
45
- def self.following(login)
46
- Client.new.following(login)
47
- end
48
-
49
- def self.follows?(username, target)
50
- Client.new.follows?(username, target)
51
- end
52
-
53
- def self.watched(login)
54
- Client.new.watched(login)
55
- end
56
-
36
+
37
+ def self.client; Client.new end
38
+
39
+ # Users
40
+ def_delegators :client, :search_users, :user, :followers, :following, :follows?, :watched
41
+
57
42
  # Issues
58
-
59
- def self.search_issues(repo, state, q)
60
- Client.new.search_issues(repo, state, q)
61
- end
62
-
63
- # repo, state
64
- def self.issues(repo, state)
65
- Client.new.issues(repo, state)
66
- end
67
-
68
- # repo, id
69
- def self.issue(repo, id)
70
- Client.new.issue(repo, id)
71
- end
72
-
43
+ def_delegators :client, :search_issues, :issues, :issue
44
+
73
45
  # Repos
74
-
75
- def self.search_repos(q)
76
- Client.new.search_repos(q)
77
- end
78
-
79
- def self.repo(repo)
80
- Client.new.repo(repo)
81
- end
82
-
83
- def self.list_repos(username)
84
- Client.new.list_repos(username)
85
- end
86
-
87
- def self.collaborators(repo)
88
- Client.new.collaborators(repo)
89
- end
90
-
91
- def self.network(repo)
92
- Client.new.network(repo)
93
- end
94
-
95
- def self.languages(repo)
96
- Client.new.languages(repo)
97
- end
98
-
99
- def self.tags(repo)
100
- Client.new.tags(repo)
101
- end
102
-
103
- def self.branches(repo)
104
- Client.new.branches(repo)
105
- end
106
-
46
+ def_delegators :client, :branches, :collaborators, :languages, :list_repos,
47
+ :network, :repo, :search_repos, :tags
48
+
107
49
  # Network Meta
50
+ def_delegators :client, :network_meta, :network_data
108
51
 
109
- def self.network_meta(repo)
110
- Client.new.network_meta(repo)
111
- end
112
-
113
- def self.network_data(repo, nethash)
114
- Client.new.network_data(repo, nethash)
115
- end
116
-
117
52
  # Trees
118
-
119
- def self.tree(repo, sha)
120
- Client.new.tree(repo, sha)
121
- end
122
-
123
- def self.blob(repo, sha, path)
124
- Client.new.blob(repo, sha, path)
125
- end
126
-
127
- def self.raw(repo, sha)
128
- Client.new.raw(repo, sha)
129
- end
53
+ def_delegators :client, :tree, :blob, :raw
130
54
 
55
+ # Commits
56
+ def_delegators :client, :list_commits, :commit
131
57
  end
132
-
133
- require File.join(directory, 'octopussy', 'repo')
134
- require File.join(directory, 'octopussy', 'event')
135
- require File.join(directory, 'octopussy', 'client')
@@ -16,44 +16,37 @@ module Octopussy
16
16
  def search_users(q)
17
17
  q = CGI.escape(q)
18
18
  response = self.class.get("/user/search/#{q}")
19
- handle_response(response)
20
19
  Hashie::Mash.new(response).users
21
20
  end
22
21
 
23
22
  def user(login=self.login)
24
23
  response = self.class.get("/user/show/#{login}", :query => auth_params)
25
- handle_response(response)
26
24
  Hashie::Mash.new(response).user
27
25
  end
28
26
 
29
27
 
30
28
  def update_user(values={})
31
29
  response = self.class.post("/user/show/#{self.login}", :query => auth_params, :body => {:values => values})
32
- handle_response(response)
33
30
  Hashie::Mash.new(response).user
34
31
  end
35
32
 
36
33
  def followers(login=self.login)
37
34
  response = self.class.get("/user/show/#{login}/followers")
38
- handle_response(response)
39
35
  Hashie::Mash.new(response).users
40
36
  end
41
37
 
42
38
  def following(login=self.login)
43
39
  response = self.class.get("/user/show/#{login}/following")
44
- handle_response(response)
45
40
  Hashie::Mash.new(response).users
46
41
  end
47
42
 
48
43
  def follow!(username)
49
44
  response = self.class.post("/user/follow/#{username}", :query => auth_params)
50
- handle_response(response)
51
45
  Hashie::Mash.new(response).users
52
46
  end
53
47
 
54
48
  def unfollow!(username)
55
49
  response = self.class.post("/user/unfollow/#{username}", :query => auth_params)
56
- handle_response(response)
57
50
  Hashie::Mash.new(response).users
58
51
  end
59
52
 
@@ -67,43 +60,36 @@ module Octopussy
67
60
 
68
61
  def watched(login=self.login)
69
62
  response = self.class.get("/repos/watched/#{login}")
70
- handle_response(response)
71
63
  Hashie::Mash.new(response).repositories
72
64
  end
73
65
 
74
66
  def emails
75
67
  response = self.class.get("/user/emails", :query => auth_params)
76
- handle_response(response)
77
68
  Hashie::Mash.new(response).emails
78
69
  end
79
70
 
80
71
  def add_email(email)
81
72
  response = self.class.post("/user/email/add", :query => auth_params, :body => {:email => email})
82
- handle_response(response)
83
73
  Hashie::Mash.new(response).emails
84
74
  end
85
75
 
86
76
  def remove_email(email)
87
77
  response = self.class.post("/user/email/remove", :query => auth_params, :body => {:email => email})
88
- handle_response(response)
89
78
  Hashie::Mash.new(response).emails
90
79
  end
91
80
 
92
81
  def keys
93
82
  response = self.class.get("/user/keys", :query => auth_params)
94
- handle_response(response)
95
83
  Hashie::Mash.new(response).public_keys
96
84
  end
97
85
 
98
86
  def add_key(title, key)
99
87
  response = self.class.post("/user/key/add", :query => auth_params, :body => {:title => title, :key => key})
100
- handle_response(response)
101
88
  Hashie::Mash.new(response).public_keys
102
89
  end
103
90
 
104
91
  def remove_key(id)
105
92
  response = self.class.post("/user/key/remove", :query => auth_params, :body => {:id => id})
106
- handle_response(response)
107
93
  Hashie::Mash.new(response).public_keys
108
94
  end
109
95
 
@@ -112,77 +98,66 @@ module Octopussy
112
98
  def search_issues(repo, state, q)
113
99
  repo = Repo.new(repo)
114
100
  response = self.class.get("/issues/search/#{repo.username}/#{repo.name}/#{state}/#{q}")
115
- handle_response(response)
116
101
  Hashie::Mash.new(response).issues
117
102
  end
118
103
 
119
104
  def issues(repo, state)
120
105
  repo = Repo.new(repo)
121
106
  response = self.class.get("/issues/list/#{repo.username}/#{repo.name}/#{state}")
122
- handle_response(response)
123
107
  Hashie::Mash.new(response).issues
124
108
  end
125
109
 
126
110
  def issue(repo, id)
127
111
  repo = Repo.new(repo)
128
112
  response = self.class.get("/issues/show/#{repo.username}/#{repo.name}/#{id}")
129
- handle_response(response)
130
113
  Hashie::Mash.new(response).issue
131
114
  end
132
115
 
133
116
  def open_issue(repo, title, body)
134
117
  repo = Repo.new(repo)
135
118
  response = self.class.post("/issues/open/#{repo.username}/#{repo.name}", :body => {:title => title, :body => body})
136
- handle_response(response)
137
119
  Hashie::Mash.new(response).issue
138
120
  end
139
121
 
140
122
  def close_issue(repo, number)
141
123
  repo = Repo.new(repo)
142
124
  response = self.class.post("/issues/close/#{repo.username}/#{repo.name}/#{number}")
143
- handle_response(response)
144
125
  Hashie::Mash.new(response).issue
145
126
  end
146
127
 
147
128
  def reopen_issue(repo, number)
148
129
  repo = Repo.new(repo)
149
130
  response = self.class.post("/issues/reopen/#{repo.username}/#{repo.name}/#{number}")
150
- handle_response(response)
151
131
  Hashie::Mash.new(response).issue
152
132
  end
153
133
 
154
134
  def update_issue(repo, number, title, body)
155
135
  repo = Repo.new(repo)
156
136
  response = self.class.post("/issues/edit/#{repo.username}/#{repo.name}/#{number}", :body => {:title => title, :body => body})
157
- handle_response(response)
158
137
  Hashie::Mash.new(response).issue
159
138
  end
160
139
 
161
140
  def labels(repo)
162
141
  repo = Repo.new(repo)
163
142
  response = self.class.get("/issues/labels/#{repo.username}/#{repo.name}")
164
- handle_response(response)
165
143
  Hashie::Mash.new(response).labels
166
144
  end
167
145
 
168
146
  def add_label(repo, number, label)
169
147
  repo = Repo.new(repo)
170
148
  response = self.class.post("/issues/label/add/#{repo.username}/#{repo.name}/#{label}/#{number}")
171
- handle_response(response)
172
149
  Hashie::Mash.new(response).labels
173
150
  end
174
151
 
175
152
  def remove_label(repo, number, label)
176
153
  repo = Repo.new(repo)
177
154
  response = self.class.post("/issues/label/remove/#{repo.username}/#{repo.name}/#{label}/#{number}")
178
- handle_response(response)
179
155
  Hashie::Mash.new(response).labels
180
156
  end
181
157
 
182
158
  def add_comment(repo, number, comment)
183
159
  repo = Repo.new(repo)
184
160
  response = self.class.post("/issues/comment/#{repo.username}/#{repo.name}/#{number}", :body => {:comment => comment})
185
- handle_response(response)
186
161
  Hashie::Mash.new(response).comment
187
162
  end
188
163
 
@@ -191,42 +166,36 @@ module Octopussy
191
166
  def search_repos(q)
192
167
  q = CGI.escape(q)
193
168
  response = self.class.get("/repos/search/#{q}")
194
- handle_response(response)
195
169
  Hashie::Mash.new(response).repositories
196
170
  end
197
171
 
198
172
  def watch(repo)
199
173
  repo = Repo.new(repo)
200
174
  response = self.class.post("/repos/watch/#{repo.username}/#{repo.name}", :query => auth_params)
201
- handle_response(response)
202
175
  Hashie::Mash.new(response).repository
203
176
  end
204
177
 
205
178
  def unwatch(repo)
206
179
  repo = Repo.new(repo)
207
180
  response = self.class.post("/repos/unwatch/#{repo.username}/#{repo.name}", :query => auth_params)
208
- handle_response(response)
209
181
  Hashie::Mash.new(response).repository
210
182
  end
211
183
 
212
184
  def fork(repo)
213
185
  repo = Repo.new(repo)
214
186
  response = self.class.post("/repos/fork/#{repo.username}/#{repo.name}", :query => auth_params)
215
- handle_response(response)
216
187
  Hashie::Mash.new(response).repository
217
188
  end
218
189
 
219
190
  # :name, :description, :homepage, :public
220
191
  def create(options)
221
192
  response = self.class.post("/repos/create", :query => auth_params, :body => options)
222
- handle_response(response)
223
193
  Hashie::Mash.new(response).repository
224
194
  end
225
195
 
226
196
  def delete(repo, delete_token={})
227
197
  repo = Repo.new(repo)
228
198
  response = self.class.post("/repos/delete/#{repo.name}", :query => auth_params, :body => {:delete_token => delete_token})
229
- handle_response(response)
230
199
  Hashie::Mash.new(response).repository
231
200
  end
232
201
 
@@ -237,97 +206,83 @@ module Octopussy
237
206
  def set_private(repo)
238
207
  repo = Repo.new(repo)
239
208
  response = self.class.post("/repos/set/private/#{repo.name}", :query => auth_params)
240
- handle_response(response)
241
209
  Hashie::Mash.new(response).repository
242
210
  end
243
211
 
244
212
  def set_public(repo)
245
213
  repo = Repo.new(repo)
246
214
  response = self.class.post("/repos/set/public/#{repo.name}", :query => auth_params)
247
- handle_response(response)
248
215
  Hashie::Mash.new(response).repository
249
216
  end
250
217
 
251
218
  def deploy_keys(repo)
252
219
  repo = Repo.new(repo)
253
220
  response = self.class.get("/repos/keys/#{repo.name}", :query => auth_params)
254
- handle_response(response)
255
221
  Hashie::Mash.new(response).public_keys
256
222
  end
257
223
 
258
224
  def add_deploy_key(repo, key, title='')
259
225
  repo = Repo.new(repo)
260
226
  response = self.class.post("/repos/key/#{repo.name}/add", :query => auth_params, :body => {:title => title, :key => key})
261
- handle_response(response)
262
227
  Hashie::Mash.new(response).public_keys
263
228
  end
264
229
 
265
230
  def remove_deploy_key(repo, id)
266
231
  repo = Repo.new(repo)
267
232
  response = self.class.post("/repos/key/#{repo.name}/remove", :query => auth_params, :body => {:id => id})
268
- handle_response(response)
269
233
  Hashie::Mash.new(response).public_keys
270
234
  end
271
235
 
272
236
  def collaborators(repo)
273
237
  repo = Repo.new(repo)
274
238
  response = self.class.post("/repos/show/#{repo.username}/#{repo.name}/collaborators", :query => auth_params)
275
- handle_response(response)
276
239
  Hashie::Mash.new(response).collaborators
277
240
  end
278
241
 
279
242
  def repo(repo)
280
243
  repo = Repo.new(repo)
281
244
  response = self.class.get("/repos/show/#{repo.username}/#{repo.name}")
282
- handle_response(response)
283
245
  Hashie::Mash.new(response).repository
284
246
  end
285
247
 
286
248
  def list_repos(username)
287
249
  response = self.class.get("/repos/show/#{username}")
288
- handle_response(response)
289
250
  Hashie::Mash.new(response).repositories
290
251
  end
291
252
 
292
253
  def add_collaborator(repo, collaborator)
293
254
  repo = Repo.new(repo)
294
255
  response = self.class.post("/repos/collaborators/#{repo.name}/add/#{collaborator}", :query => auth_params)
295
- handle_response(response)
296
256
  Hashie::Mash.new(response).collaborators
297
257
  end
298
258
 
299
259
  def remove_collaborator(repo, collaborator)
300
260
  repo = Repo.new(repo)
301
261
  response = self.class.post("/repos/collaborators/#{repo.name}/remove/#{collaborator}", :query => auth_params)
302
- handle_response(response)
303
262
  Hashie::Mash.new(response).collaborators
304
263
  end
305
264
 
306
265
  def network(repo)
307
266
  repo = Repo.new(repo)
308
267
  response = self.class.get("/repos/show/#{repo.username}/#{repo.name}/network")
309
- handle_response(response)
310
268
  Hashie::Mash.new(response).network
311
269
  end
312
270
 
313
271
  def languages(repo)
314
272
  repo = Repo.new(repo)
315
273
  response = self.class.get("/repos/show/#{repo.username}/#{repo.name}/languages")
316
- handle_response(response)
317
274
  Hashie::Mash.new(response).languages
318
275
  end
319
276
 
320
277
  def tags(repo)
321
278
  repo = Repo.new(repo)
322
279
  response = self.class.get("/repos/show/#{repo.username}/#{repo.name}/tags")
323
- handle_response(response)
324
280
  Hashie::Mash.new(response).tags
325
281
  end
326
282
 
327
283
  def branches(repo)
328
284
  repo = Repo.new(repo)
329
285
  response = self.class.get("/repos/show/#{repo.username}/#{repo.name}/branches")
330
- handle_response(response)
331
286
  Hashie::Mash.new(response).branches
332
287
  end
333
288
 
@@ -336,14 +291,12 @@ module Octopussy
336
291
  def network_meta(repo)
337
292
  repo = Repo.new(repo)
338
293
  response = self.class.get("http://github.com/#{repo.username}/#{repo.name}/network_meta")
339
- handle_response(response)
340
294
  Hashie::Mash.new(response)
341
295
  end
342
296
 
343
297
  def network_data(repo, nethash)
344
298
  repo = Repo.new(repo)
345
299
  response = self.class.get("http://github.com/#{repo.username}/#{repo.name}/network_data_chunk", :query => {:nethash => nethash})
346
- handle_response(response)
347
300
  Hashie::Mash.new(response).commits
348
301
  end
349
302
 
@@ -352,42 +305,54 @@ module Octopussy
352
305
  def tree(repo, sha)
353
306
  repo = Repo.new(repo)
354
307
  response = self.class.get("http://github.com/api/v2/json/tree/show/#{repo.username}/#{repo.name}/#{sha}")
355
- handle_response(response)
356
308
  Hashie::Mash.new(response).tree
357
309
  end
358
310
 
359
311
  def blob(repo, sha, path)
360
312
  repo = Repo.new(repo)
361
313
  response = self.class.get("http://github.com/api/v2/json/blob/show/#{repo.username}/#{repo.name}/#{sha}/#{path}")
362
- handle_response(response)
363
314
  Hashie::Mash.new(response).blob
364
315
  end
365
316
 
366
317
  def raw(repo, sha)
367
318
  repo = Repo.new(repo)
368
319
  response = self.class.get("http://github.com/api/v2/yaml/blob/show/#{repo.username}/#{repo.name}/#{sha}")
369
- handle_response(response)
370
320
  response.body
371
321
  end
372
322
 
373
- private
323
+ # Commits
374
324
 
375
- def auth_params
376
- @login.nil? ? {} : {:login => @login, :token => @token}
377
- end
325
+ def list_commits(repo, branch="master")
326
+ repo = Repo.new(repo)
327
+ response = self.class.get("http://github.com/api/v2/json/commits/list/#{repo.username}/#{repo.name}/#{branch}")
328
+ Hashie::Mash.new(response).commits
329
+ end
330
+
331
+ def commit(repo, sha)
332
+ repo = Repo.new(repo)
333
+ response = self.class.get("http://github.com/api/v2/json/commits/show/#{repo.username}/#{repo.name}/#{sha}")
334
+ Hashie::Mash.new(response).commit
335
+ end
336
+
337
+ private
378
338
 
379
- def handle_response(response)
380
- case response.code
381
- when 403
382
- raise RateLimitExceeded.new
383
- when 401
384
- raise Unauthorized.new
385
- when 404
386
- raise NotFound.new
387
- when 500
388
- raise OctopussyError.new(500)
389
- end
339
+ def auth_params
340
+ @login.nil? ? {} : {:login => @login, :token => @token}
341
+ end
342
+
343
+ def self.get(*args); handle_response super end
344
+ def self.post(*args); handle_response super end
345
+
346
+ def self.handle_response(response)
347
+ case response.code
348
+ when 401; raise Unauthorized.new
349
+ when 403; raise RateLimitExceeded.new
350
+ when 404; raise NotFound.new
351
+ when 400...500; raise ClientError.new
352
+ when 500...600; raise ServerError.new(response.code)
353
+ else; response
390
354
  end
355
+ end
391
356
 
392
357
  end
393
- end
358
+ end