github_api 0.4.9 → 0.4.10
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +4 -2
- data/lib/github_api.rb +1 -0
- data/lib/github_api/core_ext/ordered_hash.rb +107 -0
- data/lib/github_api/repos.rb +6 -6
- data/lib/github_api/repos/downloads.rb +7 -45
- data/lib/github_api/response/xmlize.rb +26 -0
- data/lib/github_api/s3_uploader.rb +57 -0
- data/lib/github_api/version.rb +1 -1
- data/spec/github/repos/downloads_spec.rb +11 -27
- data/spec/github/repos_spec.rb +2 -2
- data/spec/github/s3_uploader_spec.rb +50 -0
- metadata +6 -2
data/README.md
CHANGED
@@ -162,7 +162,8 @@ end
|
|
162
162
|
|
163
163
|
In order to authenticate the user through OAuth2 on GitHub you need to
|
164
164
|
|
165
|
-
* visit https://github.com/
|
165
|
+
* visit https://github.com/settings/applications/new and register your app
|
166
|
+
You will need to be logged in to initially register the application.
|
166
167
|
|
167
168
|
* authorize your credentials https://github.com/login/oauth/authorize
|
168
169
|
You can use convenience methods to help you achieve this that come with this gem:
|
@@ -205,7 +206,8 @@ In order to pass a mime type with your request do
|
|
205
206
|
## Configuration
|
206
207
|
|
207
208
|
Certain methods require authentication. To get your GitHub OAuth v2 credentials,
|
208
|
-
register an app at https://github.com/
|
209
|
+
register an app at https://github.com/settings/applications/
|
210
|
+
You will need to be logged in to register the application.
|
209
211
|
|
210
212
|
```ruby
|
211
213
|
Github.configure do |config|
|
data/lib/github_api.rb
CHANGED
@@ -0,0 +1,107 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Github
|
4
|
+
module CoreExt #:nodoc:
|
5
|
+
|
6
|
+
if RUBY_VERSION >= '1.9'
|
7
|
+
class OrderedHash < ::Hash; end
|
8
|
+
else
|
9
|
+
class OrderedHash < ::Hash
|
10
|
+
attr_accessor :order
|
11
|
+
|
12
|
+
class << self
|
13
|
+
def [](*args)
|
14
|
+
hsh = OrderedHash.new
|
15
|
+
if Hash == args[0]
|
16
|
+
hsh.replace args[0]
|
17
|
+
elsif (args.size % 2) != 0
|
18
|
+
pp args
|
19
|
+
raise ArgumentError, "odd number of elements for Hash"
|
20
|
+
else
|
21
|
+
0.step(args.size - 1, 2) do |a|
|
22
|
+
b = a + 1
|
23
|
+
hsh[args[a]] = args[b]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
hsh
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def initialize(*args, &block)
|
31
|
+
super
|
32
|
+
@order = []
|
33
|
+
end
|
34
|
+
|
35
|
+
def []=(key, value)
|
36
|
+
@order.push key unless member?(key)
|
37
|
+
super key, value
|
38
|
+
end
|
39
|
+
|
40
|
+
def ==(hsh2)
|
41
|
+
return false if @order != hsh2.order
|
42
|
+
super hsh2
|
43
|
+
end
|
44
|
+
|
45
|
+
def clear
|
46
|
+
@order = []
|
47
|
+
super
|
48
|
+
end
|
49
|
+
|
50
|
+
def delete(key)
|
51
|
+
@order.delete key
|
52
|
+
super
|
53
|
+
end
|
54
|
+
|
55
|
+
def each_key
|
56
|
+
@order.each { |k| yield k }
|
57
|
+
self
|
58
|
+
end
|
59
|
+
|
60
|
+
def each_value
|
61
|
+
@order.each { |k| yield self[k] }
|
62
|
+
self
|
63
|
+
end
|
64
|
+
|
65
|
+
def each
|
66
|
+
@order.each { |k| yield k, self[k] }
|
67
|
+
self
|
68
|
+
end
|
69
|
+
alias :each_pair :each
|
70
|
+
|
71
|
+
def delete_if
|
72
|
+
@order.clone.each { |k| delete k if yield }
|
73
|
+
self
|
74
|
+
end
|
75
|
+
|
76
|
+
def values
|
77
|
+
ary = []
|
78
|
+
@order.each { |k| ary.push self[k] }
|
79
|
+
ary
|
80
|
+
end
|
81
|
+
|
82
|
+
def keys
|
83
|
+
@order
|
84
|
+
end
|
85
|
+
|
86
|
+
def replace(hsh2)
|
87
|
+
@order = hsh2.keys
|
88
|
+
super hsh2
|
89
|
+
end
|
90
|
+
|
91
|
+
def shift
|
92
|
+
key = @order.first
|
93
|
+
key ? [key, delete(key)] : super
|
94
|
+
end
|
95
|
+
|
96
|
+
def class
|
97
|
+
Hash
|
98
|
+
end
|
99
|
+
|
100
|
+
def __class__
|
101
|
+
OrderedHash
|
102
|
+
end
|
103
|
+
end # OrderedHash
|
104
|
+
end
|
105
|
+
|
106
|
+
end # CoreExt
|
107
|
+
end # Github
|
data/lib/github_api/repos.rb
CHANGED
@@ -26,7 +26,7 @@ module Github
|
|
26
26
|
|
27
27
|
DEFAULT_REPO_OPTIONS = {
|
28
28
|
"homepage" => "https://github.com",
|
29
|
-
"
|
29
|
+
"private" => false,
|
30
30
|
"has_issues" => true,
|
31
31
|
"has_wiki" => true,
|
32
32
|
"has_downloads" => true
|
@@ -36,7 +36,7 @@ module Github
|
|
36
36
|
name
|
37
37
|
description
|
38
38
|
homepage
|
39
|
-
|
39
|
+
private
|
40
40
|
has_issues
|
41
41
|
has_wiki
|
42
42
|
has_downloads
|
@@ -76,7 +76,7 @@ module Github
|
|
76
76
|
# <tt>:name</tt> - Required string
|
77
77
|
# <tt>:description</tt> - Optional string
|
78
78
|
# <tt>:homepage</tt> - Optional string
|
79
|
-
# <tt>:
|
79
|
+
# <tt>:private</tt> - Optional boolean - <tt>false</tt> to create public reps, <tt>false</tt> to create a private one
|
80
80
|
# <tt>:has_issues</tt> - Optional boolean - <tt>true</tt> to enable issues for this repository, <tt>false</tt> to disable them
|
81
81
|
# <tt>:has_wiki</tt> - Optional boolean - <tt>true</tt> to enable the wiki for this repository, <tt>false</tt> to disable it. Default is <tt>true</tt>
|
82
82
|
# <tt>:has_downloads</tt> Optional boolean - <tt>true</tt> to enable downloads for this repository
|
@@ -86,7 +86,7 @@ module Github
|
|
86
86
|
# @github.repos.create_repo "name" => 'repo-name'
|
87
87
|
# "description": "This is your first repo",
|
88
88
|
# "homepage": "https://github.com",
|
89
|
-
# "
|
89
|
+
# "private": false,
|
90
90
|
# "has_issues": true,
|
91
91
|
# "has_wiki": true,
|
92
92
|
# "has_downloads": true
|
@@ -148,7 +148,7 @@ module Github
|
|
148
148
|
# * <tt>:name</tt> Required string
|
149
149
|
# * <tt>:description</tt> Optional string
|
150
150
|
# * <tt>:homepage</tt> Optional string
|
151
|
-
#
|
151
|
+
# <tt>:private</tt> - Optional boolean - <tt>false</tt> to create public reps, <tt>false</tt> to create a private one
|
152
152
|
# * <tt>:has_issues</tt> Optional boolean - <tt>true</tt> to enable issues for this repository, <tt>false</tt> to disable them
|
153
153
|
# * <tt>:has_wiki</tt> Optional boolean - <tt>true</tt> to enable the wiki for this repository, <tt>false</tt> to disable it. Default is <tt>true</tt>
|
154
154
|
# * <tt>:has_downloads</tt> Optional boolean - <tt>true</tt> to enable downloads for this repository
|
@@ -156,7 +156,7 @@ module Github
|
|
156
156
|
# = Examples
|
157
157
|
#
|
158
158
|
# @github = Github.new
|
159
|
-
# @github.repos.edit_repo('user-name', 'repo-name', { :name => 'hello-world', :description => 'This is your first repo', :homepage => "https://github.com", :
|
159
|
+
# @github.repos.edit_repo('user-name', 'repo-name', { :name => 'hello-world', :description => 'This is your first repo', :homepage => "https://github.com", :private => false, :has_issues => true })
|
160
160
|
#
|
161
161
|
def edit_repo(user_name=nil, repo_name=nil, params={})
|
162
162
|
_update_user_repo_params(user_name, repo_name)
|
@@ -1,5 +1,7 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
|
+
require 'github_api/s3_uploader'
|
4
|
+
|
3
5
|
module Github
|
4
6
|
class Repos
|
5
7
|
module Downloads
|
@@ -13,19 +15,6 @@ module Github
|
|
13
15
|
content_type
|
14
16
|
].freeze
|
15
17
|
|
16
|
-
REQUIRED_S3_PARAMS = %w[
|
17
|
-
path
|
18
|
-
acl
|
19
|
-
name
|
20
|
-
accesskeyid
|
21
|
-
policy
|
22
|
-
signature
|
23
|
-
mime_type
|
24
|
-
].freeze
|
25
|
-
|
26
|
-
# Status code for successful upload to Amazon S3 service
|
27
|
-
SUCCESS_STATUS = 201
|
28
|
-
|
29
18
|
# List downloads for a repository
|
30
19
|
#
|
31
20
|
# = Examples
|
@@ -110,45 +99,18 @@ module Github
|
|
110
99
|
# the response object as an argument to upload method.
|
111
100
|
#
|
112
101
|
# = Parameters
|
113
|
-
# * <tt>resource</tt> - Required
|
114
|
-
# * <tt>:
|
102
|
+
# * <tt>resource</tt> - Required resource of the create_download call.
|
103
|
+
# * <tt>:filename</tt> - Required filename, a path to a file location.
|
115
104
|
#
|
116
105
|
def upload(resource, filename)
|
117
106
|
_validate_presence_of resource, filename
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
raise ArgumentError, "Expected following key: #{key}" unless resource.respond_to?(key)
|
122
|
-
end
|
123
|
-
|
124
|
-
# TODO use ordered hash if Ruby < 1.9
|
125
|
-
hash = ruby_18 {
|
126
|
-
require 'active_support'
|
127
|
-
ActiveSupport::OrderedHash.new } || ruby_19 { Hash.new }
|
128
|
-
|
129
|
-
mapped_params = {
|
130
|
-
'key' => resource.path,
|
131
|
-
'acl' => resource.acl,
|
132
|
-
'success_action_status' => SUCCESS_STATUS,
|
133
|
-
'Filename' => resource.name,
|
134
|
-
'AWSAccessKeyId' => resource.accesskeyid,
|
135
|
-
'Policy' => resource.policy,
|
136
|
-
'Signature' => resource.signature,
|
137
|
-
'Content-Type' => resource.mime_type,
|
138
|
-
'file' => prepend_at_for(filename.to_s)
|
139
|
-
}
|
140
|
-
|
141
|
-
post('', mapped_params, { :url => resource.s3_url })
|
107
|
+
|
108
|
+
response = Github::S3Uploader.new(resource, filename).send
|
109
|
+
response.body
|
142
110
|
end
|
143
111
|
alias :upload_to_s3 :upload
|
144
112
|
alias :upload_to_amazon :upload
|
145
113
|
|
146
|
-
private
|
147
|
-
|
148
|
-
def prepend_at_for(file)
|
149
|
-
/^@.*/ =~ file ? '@' + file : file
|
150
|
-
end
|
151
|
-
|
152
114
|
end # Downloads
|
153
115
|
end # Repos
|
154
116
|
end # Github
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'faraday'
|
4
|
+
|
5
|
+
module Github
|
6
|
+
class Response::Xmlize < Response
|
7
|
+
dependency 'nokogiri'
|
8
|
+
|
9
|
+
define_parser do |body|
|
10
|
+
::Nokogiri::XML body
|
11
|
+
end
|
12
|
+
|
13
|
+
def parse(body)
|
14
|
+
case body
|
15
|
+
when ''
|
16
|
+
nil
|
17
|
+
when 'true'
|
18
|
+
true
|
19
|
+
when 'false'
|
20
|
+
false
|
21
|
+
else
|
22
|
+
self.class.parser.call body
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end # Response::Xmlize
|
26
|
+
end # Github
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'github_api/response/xmlize'
|
4
|
+
|
5
|
+
module Github
|
6
|
+
class S3Uploader
|
7
|
+
|
8
|
+
REQUIRED_S3_PARAMS = %w[
|
9
|
+
path
|
10
|
+
acl
|
11
|
+
name
|
12
|
+
accesskeyid
|
13
|
+
policy
|
14
|
+
signature
|
15
|
+
mime_type
|
16
|
+
].freeze
|
17
|
+
|
18
|
+
# Status code for successful upload to Amazon S3 service
|
19
|
+
SUCCESS_STATUS = 201
|
20
|
+
|
21
|
+
attr_accessor :resource, :filename
|
22
|
+
|
23
|
+
def initialize(resource, filename)
|
24
|
+
@resource = resource
|
25
|
+
@filename = filename
|
26
|
+
end
|
27
|
+
|
28
|
+
def send
|
29
|
+
REQUIRED_S3_PARAMS.each do |key|
|
30
|
+
unless resource.respond_to?(key)
|
31
|
+
raise ArgumentError, "Expected following key: #{key}"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
mapped_params = Github::CoreExt::OrderedHash[
|
36
|
+
'key', resource.path,
|
37
|
+
'acl', resource.acl,
|
38
|
+
'success_action_status', SUCCESS_STATUS,
|
39
|
+
'Filename', resource.name,
|
40
|
+
'AWSAccessKeyId', resource.accesskeyid,
|
41
|
+
'Policy', resource.policy,
|
42
|
+
'Signature', resource.signature,
|
43
|
+
'Content-Type', resource.mime_type,
|
44
|
+
'file', Faraday::UploadIO.new(filename, 'application/octet-stream')
|
45
|
+
]
|
46
|
+
|
47
|
+
http = Faraday.new do |builder|
|
48
|
+
builder.request :multipart
|
49
|
+
builder.use Github::Response::Xmlize
|
50
|
+
builder.adapter :net_http
|
51
|
+
end
|
52
|
+
|
53
|
+
http.post resource.s3_url, mapped_params
|
54
|
+
end
|
55
|
+
|
56
|
+
end # S3Uploader
|
57
|
+
end # Github
|
data/lib/github_api/version.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
3
|
require 'spec_helper'
|
4
|
+
require 'github_api/s3_uploader'
|
4
5
|
|
5
6
|
describe Github::Repos::Downloads do
|
6
7
|
let(:github) { Github.new }
|
@@ -11,7 +12,6 @@ describe Github::Repos::Downloads do
|
|
11
12
|
|
12
13
|
it { described_class::VALID_DOWNLOAD_PARAM_NAMES.should_not be_nil }
|
13
14
|
it { described_class::REQUIRED_PARAMS.should_not be_nil }
|
14
|
-
it { described_class::REQUIRED_S3_PARAMS.should_not be_nil }
|
15
15
|
|
16
16
|
describe "downloads" do
|
17
17
|
it { github.repos.should respond_to :downloads }
|
@@ -212,41 +212,25 @@ describe Github::Repos::Downloads do
|
|
212
212
|
end # create_download
|
213
213
|
|
214
214
|
describe 'upload' do
|
215
|
-
let(:
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
:content_type => 'text/plain' }
|
220
|
-
end
|
221
|
-
let(:resource) { Hashie::Mash.new ::JSON.parse(fixture('repos/download_s3.json').read) }
|
222
|
-
let(:file) { 'filename' }
|
215
|
+
let(:resource) { stub(:resource) }
|
216
|
+
let(:filename) { 'filename' }
|
217
|
+
let(:res) { stub(:response, :body => 'success') }
|
218
|
+
let(:uploader) { stub(:uploader, :send => res) }
|
223
219
|
|
224
220
|
context 'resource uploaded' do
|
225
221
|
before do
|
226
|
-
|
227
|
-
to_return(:body => '', :status => 200, :headers => {})
|
222
|
+
Github::S3Uploader.stub(:new) { uploader }
|
228
223
|
end
|
229
224
|
|
230
225
|
it "should fail if resource is of incorrect type" do
|
231
|
-
expect { github.repos.upload
|
226
|
+
expect { github.repos.upload resource, nil }.to raise_error(ArgumentError)
|
232
227
|
end
|
233
228
|
|
234
229
|
it "should upload resource successfully" do
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
end
|
240
|
-
context 'failed to upload resource' do
|
241
|
-
before do
|
242
|
-
stub_post('', 'https://github.s3.amazonaws.com/').with(resource).
|
243
|
-
to_return(:body => '', :status => 404, :headers => {})
|
244
|
-
end
|
245
|
-
|
246
|
-
it "should faile to retrieve resource" do
|
247
|
-
expect {
|
248
|
-
github.repos.upload resource, file
|
249
|
-
}.to raise_error(Github::Error::NotFound)
|
230
|
+
res = stub(:response, :body => 'success')
|
231
|
+
uploader = stub(:uploader, :send => res)
|
232
|
+
Github::S3Uploader.should_receive(:new).with(resource, filename) { uploader }
|
233
|
+
github.repos.upload(resource, filename).should == 'success'
|
250
234
|
end
|
251
235
|
end
|
252
236
|
end # upload
|
data/spec/github/repos_spec.rb
CHANGED
@@ -125,7 +125,7 @@ describe Github::Repos do
|
|
125
125
|
end # contributors
|
126
126
|
|
127
127
|
describe "create_repo" do
|
128
|
-
let(:inputs) { {:name => 'web', :description => "This is your first repo", :homepage => "https://github.com", :
|
128
|
+
let(:inputs) { {:name => 'web', :description => "This is your first repo", :homepage => "https://github.com", :private => false, :has_issues => true, :has_wiki => true}}
|
129
129
|
|
130
130
|
context "resource created successfully for the authenticated user" do
|
131
131
|
before do
|
@@ -202,7 +202,7 @@ describe Github::Repos do
|
|
202
202
|
{ :name => 'web',
|
203
203
|
:description => "This is your first repo",
|
204
204
|
:homepage => "https://github.com",
|
205
|
-
:
|
205
|
+
:private => false,
|
206
206
|
:has_issues => true,
|
207
207
|
:has_wiki => true }
|
208
208
|
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'github_api/s3_uploader'
|
3
|
+
|
4
|
+
describe Github::S3Uploader do
|
5
|
+
|
6
|
+
let(:result) {
|
7
|
+
stub(:resource,
|
8
|
+
'path' => 'downloads/octokit/github/droid',
|
9
|
+
'acl' => 'public-read',
|
10
|
+
'name' => 'droid',
|
11
|
+
"accesskeyid"=>"1DWESVTPGHQVTX38V182",
|
12
|
+
"policy"=> "GlyYXRp==",
|
13
|
+
"signature" => "fMGgiss1w=",
|
14
|
+
"mime_type"=>"image/jpeg",
|
15
|
+
"file"=> '',
|
16
|
+
"s3_url" => 'https://github.s3.amazonaws.com/'
|
17
|
+
)
|
18
|
+
}
|
19
|
+
let(:filename) { '/Users/octokit/droid.jpg' }
|
20
|
+
let(:mapped_hash) { {} }
|
21
|
+
|
22
|
+
let(:uploader) { Github::S3Uploader.new result, filename }
|
23
|
+
|
24
|
+
before do
|
25
|
+
stub_post('https://github.s3.amazonaws.com/', '').
|
26
|
+
to_return(:body => '', :status => 201, :headers => {})
|
27
|
+
Faraday::UploadIO.stub(:new) { '' }
|
28
|
+
Github::CoreExt::OrderedHash.stub(:[]) { mapped_hash }
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'checks for missing parameters' do
|
32
|
+
resource = stub(:resource)
|
33
|
+
uploader = Github::S3Uploader.new resource, filename
|
34
|
+
expect {
|
35
|
+
uploader.send
|
36
|
+
}.to raise_error(ArgumentError)
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'serializes file' do
|
40
|
+
Faraday::UploadIO.should_receive(:new) { '' }
|
41
|
+
uploader.send
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'sends request to amazon aws' do
|
45
|
+
uploader.send
|
46
|
+
a_post('https://github.s3.amazonaws.com/', '').with(mapped_hash).
|
47
|
+
should have_been_made
|
48
|
+
end
|
49
|
+
|
50
|
+
end # Github::S3Uploader
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: github_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.4.
|
5
|
+
version: 0.4.10
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Piotr Murach
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2012-04-
|
13
|
+
date: 2012-04-15 00:00:00 +01:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -232,6 +232,7 @@ files:
|
|
232
232
|
- lib/github_api/constants.rb
|
233
233
|
- lib/github_api/core_ext/array.rb
|
234
234
|
- lib/github_api/core_ext/hash.rb
|
235
|
+
- lib/github_api/core_ext/ordered_hash.rb
|
235
236
|
- lib/github_api/deprecation.rb
|
236
237
|
- lib/github_api/error/bad_request.rb
|
237
238
|
- lib/github_api/error/client_error.rb
|
@@ -288,8 +289,10 @@ files:
|
|
288
289
|
- lib/github_api/response/jsonize.rb
|
289
290
|
- lib/github_api/response/mashify.rb
|
290
291
|
- lib/github_api/response/raise_error.rb
|
292
|
+
- lib/github_api/response/xmlize.rb
|
291
293
|
- lib/github_api/response.rb
|
292
294
|
- lib/github_api/result.rb
|
295
|
+
- lib/github_api/s3_uploader.rb
|
293
296
|
- lib/github_api/users/emails.rb
|
294
297
|
- lib/github_api/users/followers.rb
|
295
298
|
- lib/github_api/users/keys.rb
|
@@ -412,6 +415,7 @@ files:
|
|
412
415
|
- spec/github/request_spec.rb
|
413
416
|
- spec/github/response/helpers_spec.rb
|
414
417
|
- spec/github/result_spec.rb
|
418
|
+
- spec/github/s3_uploader_spec.rb
|
415
419
|
- spec/github/users/emails_spec.rb
|
416
420
|
- spec/github/users/followers_spec.rb
|
417
421
|
- spec/github/users/keys_spec.rb
|