fcoury-octopi 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +7 -3
- data/VERSION.yml +1 -1
- data/lib/octopi.rb +38 -13
- data/lib/octopi/branch.rb +17 -0
- data/lib/octopi/error.rb +8 -0
- data/lib/octopi/key.rb +18 -0
- data/lib/octopi/repository.rb +9 -0
- data/lib/octopi/user.rb +20 -0
- metadata +4 -2
data/README.rdoc
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
= octopi
|
2
2
|
|
3
|
-
Octopi is a Ruby interface to GitHub API v2 (http://develop.github.com).
|
3
|
+
Octopi is a Ruby interface to GitHub API v2 (http://develop.github.com).
|
4
|
+
|
5
|
+
To install it as a Gem, just run:
|
6
|
+
|
7
|
+
$ sudo gem install fcoury-octopi --source http://gems.github.com
|
4
8
|
|
5
9
|
== Authenticated Usage
|
6
10
|
|
@@ -45,7 +49,7 @@ Use the following format:
|
|
45
49
|
# curl - same as true, but in addition will output the curl equivalent of each command (for debugging)
|
46
50
|
trace: curl
|
47
51
|
|
48
|
-
|
52
|
+
And change the way you connect to:
|
49
53
|
|
50
54
|
authenticated_with :config => "github.yml" do |g|
|
51
55
|
(...)
|
@@ -53,7 +57,7 @@ This changes the way you connect to:
|
|
53
57
|
|
54
58
|
== Anonymous Usage
|
55
59
|
|
56
|
-
This reflects the usage of the API to retrieve information
|
60
|
+
This reflects the usage of the API to retrieve information on a read-only fashion, where the user doesn't have to be authenticated.
|
57
61
|
|
58
62
|
=== Users API
|
59
63
|
|
data/VERSION.yml
CHANGED
data/lib/octopi.rb
CHANGED
@@ -65,6 +65,9 @@ module Octopi
|
|
65
65
|
'json' => 'application/json',
|
66
66
|
'xml' => 'application/sml'
|
67
67
|
}
|
68
|
+
RETRYABLE_STATUS = [403]
|
69
|
+
MAX_RETRIES = 10
|
70
|
+
|
68
71
|
base_uri "http://github.com/api/v2"
|
69
72
|
|
70
73
|
attr_accessor :format, :login, :token, :trace_level, :read_only
|
@@ -77,12 +80,17 @@ module Octopi
|
|
77
80
|
@login = login
|
78
81
|
@token = token
|
79
82
|
@read_only = false
|
83
|
+
self.class.default_params :login => login, :token => token
|
80
84
|
end
|
81
85
|
end
|
82
|
-
|
83
|
-
|
86
|
+
|
87
|
+
def read_only?
|
88
|
+
read_only
|
89
|
+
end
|
90
|
+
|
91
|
+
{:keys => 'public_keys', :emails => 'emails'}.each_pair do |action, key|
|
84
92
|
define_method("#{action}") do
|
85
|
-
get("/user/#{action}")
|
93
|
+
get("/user/#{action}")[key]
|
86
94
|
end
|
87
95
|
end
|
88
96
|
|
@@ -121,6 +129,25 @@ module Octopi
|
|
121
129
|
get(path, params, 'plain')
|
122
130
|
end
|
123
131
|
|
132
|
+
def get(path, params = {}, format = "yaml")
|
133
|
+
@@retries = 0
|
134
|
+
begin
|
135
|
+
trace "GET [#{format}]", "/#{format}#{path}", params
|
136
|
+
submit(path, params, format) do |path, params, format|
|
137
|
+
self.class.get "/#{format}#{path}"
|
138
|
+
end
|
139
|
+
rescue RetryableAPIError => e
|
140
|
+
if @@retries < MAX_RETRIES
|
141
|
+
$stderr.puts e.message
|
142
|
+
@@retries += 1
|
143
|
+
retry
|
144
|
+
else
|
145
|
+
raise APIError, "GitHub returned status #{e.code}, despite" +
|
146
|
+
" repeating the request #{MAX_RETRIES} times. Giving up."
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
124
151
|
def post(path, params = {}, format = "yaml")
|
125
152
|
trace "POST", "/#{format}#{path}", params
|
126
153
|
submit(path, params, format) do |path, params, format|
|
@@ -140,6 +167,12 @@ module Octopi
|
|
140
167
|
query = login ? { :login => login, :token => token } : {}
|
141
168
|
query.merge!(params)
|
142
169
|
|
170
|
+
begin
|
171
|
+
resp = yield(path, query.merge(params), format)
|
172
|
+
rescue Net::HTTPBadResponse
|
173
|
+
raise RetryableAPIError
|
174
|
+
end
|
175
|
+
|
143
176
|
if @trace_level
|
144
177
|
case @trace_level
|
145
178
|
when "curl"
|
@@ -150,8 +183,7 @@ module Octopi
|
|
150
183
|
puts "===================="
|
151
184
|
end
|
152
185
|
end
|
153
|
-
|
154
|
-
resp = yield(path, query, format)
|
186
|
+
raise RetryableAPIError, resp.code.to_i if RETRYABLE_STATUS.include? resp.code.to_i
|
155
187
|
raise APIError,
|
156
188
|
"GitHub returned status #{resp.code}" unless resp.code.to_i == 200
|
157
189
|
# FIXME: This fails for showing raw Git data because that call returns
|
@@ -165,13 +197,6 @@ module Octopi
|
|
165
197
|
resp
|
166
198
|
end
|
167
199
|
|
168
|
-
def get(path, params = {}, format = "yaml")
|
169
|
-
trace "GET [#{format}]", "/#{format}#{path}", params
|
170
|
-
submit(path, params, format) do |path, params, format|
|
171
|
-
self.class.get "/#{format}#{path}"
|
172
|
-
end
|
173
|
-
end
|
174
|
-
|
175
200
|
def trace(oper, url, params)
|
176
201
|
return unless trace_level
|
177
202
|
par_str = " params: " + params.map { |p| "#{p[0]}=#{p[1]}" }.join(", ") if params and !params.empty?
|
@@ -179,7 +204,7 @@ module Octopi
|
|
179
204
|
end
|
180
205
|
end
|
181
206
|
|
182
|
-
%w{error base resource user tag repository issue file_object blob commit}.
|
207
|
+
%w{error base resource user tag repository issue file_object blob key commit branch}.
|
183
208
|
each{|f| require "#{File.dirname(__FILE__)}/octopi/#{f}"}
|
184
209
|
|
185
210
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Octopi
|
2
|
+
class Branch < Base
|
3
|
+
include Resource
|
4
|
+
set_resource_name "branch", "branches"
|
5
|
+
|
6
|
+
resource_path "/repos/show/:id"
|
7
|
+
|
8
|
+
def self.find(user, repo)
|
9
|
+
user = user.login if user.is_a? User
|
10
|
+
repo = repo.name if repo.is_a? Repository
|
11
|
+
self.validate_args(user => :user, repo => :repo)
|
12
|
+
find_plural([user,repo,'branches'], :resource){
|
13
|
+
|i| {:name => i.first, :hash => i.last }
|
14
|
+
}
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/octopi/error.rb
CHANGED
@@ -12,4 +12,12 @@ module Octopi
|
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
|
+
class RetryableAPIError < RuntimeError
|
16
|
+
attr_reader :code
|
17
|
+
def initialize(code=nil)
|
18
|
+
@code = code.nil? ? '???' : code
|
19
|
+
@message = "GitHub returned status #{@code}. Retrying request."
|
20
|
+
super @message
|
21
|
+
end
|
22
|
+
end
|
15
23
|
end
|
data/lib/octopi/key.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
module Octopi
|
2
|
+
class Key < Base
|
3
|
+
include Resource
|
4
|
+
|
5
|
+
attr_reader :user
|
6
|
+
|
7
|
+
def initialize(api, data, user = nil)
|
8
|
+
super api, data
|
9
|
+
@user = user
|
10
|
+
end
|
11
|
+
|
12
|
+
def remove!
|
13
|
+
result = @api.post "/user/key/remove", :id => id
|
14
|
+
keys = result["public_keys"].select { |k| puts "#{title} #{k["title"]} #{k["title"] == title}"; k["title"] == title }
|
15
|
+
keys.empty?
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/octopi/repository.rb
CHANGED
@@ -5,6 +5,10 @@ module Octopi
|
|
5
5
|
|
6
6
|
find_path "/repos/search/:query"
|
7
7
|
resource_path "/repos/show/:id"
|
8
|
+
|
9
|
+
def branches
|
10
|
+
Branch.find(self.owner, self.name)
|
11
|
+
end
|
8
12
|
|
9
13
|
def tags
|
10
14
|
Tag.find(self.owner, self.name)
|
@@ -66,5 +70,10 @@ module Octopi
|
|
66
70
|
def issue(number)
|
67
71
|
Issue.find(self, number)
|
68
72
|
end
|
73
|
+
|
74
|
+
def collaborators
|
75
|
+
property('collaborators', [self.owner,self.name].join('/')).values
|
76
|
+
end
|
77
|
+
|
69
78
|
end
|
70
79
|
end
|
data/lib/octopi/user.rb
CHANGED
@@ -24,6 +24,26 @@ module Octopi
|
|
24
24
|
Repository.find(login, name)
|
25
25
|
end
|
26
26
|
|
27
|
+
def add_key(title, key)
|
28
|
+
raise APIError,
|
29
|
+
"To add a key, you must be authenticated" if @api.read_only?
|
30
|
+
|
31
|
+
result = @api.post("/user/key/add", :title => title, :key => key)
|
32
|
+
return if !result["public_keys"]
|
33
|
+
key_params = result["public_keys"].select { |k| puts "#{title} #{k["title"]} #{k["title"] == title}"; k["title"] == title }
|
34
|
+
return if !key_params or key_params.empty?
|
35
|
+
Key.new(@api, key_params.first, self)
|
36
|
+
end
|
37
|
+
|
38
|
+
def keys
|
39
|
+
raise APIError,
|
40
|
+
"To add a key, you must be authenticated" if @api.read_only?
|
41
|
+
|
42
|
+
result = @api.get("/user/keys")
|
43
|
+
return unless result and result["public_keys"]
|
44
|
+
result["public_keys"].inject([]) { |result, element| result << Key.new(@api, element) }
|
45
|
+
end
|
46
|
+
|
27
47
|
# takes one param, deep that indicates if returns
|
28
48
|
# only the user login or an user object
|
29
49
|
%w[followers following].each do |method|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fcoury-octopi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Felipe Coury
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-04-
|
12
|
+
date: 2009-04-26 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -28,10 +28,12 @@ files:
|
|
28
28
|
- lib/octopi
|
29
29
|
- lib/octopi/base.rb
|
30
30
|
- lib/octopi/blob.rb
|
31
|
+
- lib/octopi/branch.rb
|
31
32
|
- lib/octopi/commit.rb
|
32
33
|
- lib/octopi/error.rb
|
33
34
|
- lib/octopi/file_object.rb
|
34
35
|
- lib/octopi/issue.rb
|
36
|
+
- lib/octopi/key.rb
|
35
37
|
- lib/octopi/repository.rb
|
36
38
|
- lib/octopi/resource.rb
|
37
39
|
- lib/octopi/tag.rb
|