github_api 0.3.2 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- data/features/github_api.feature +50 -0
- data/features/step_definitions/github_api_steps.rb +11 -0
- data/features/support/env.rb +1 -1
- data/lib/github_api.rb +13 -0
- data/lib/github_api/connection.rb +2 -0
- data/lib/github_api/response.rb +19 -1
- data/lib/github_api/response/helpers.rb +14 -0
- data/lib/github_api/response/jsonize.rb +7 -3
- data/lib/github_api/response/mashify.rb +6 -8
- data/lib/github_api/result.rb +42 -0
- data/lib/github_api/users.rb +3 -1
- data/lib/github_api/version.rb +1 -1
- data/spec/fixtures/users/user.json +32 -0
- data/spec/github/result_spec.rb +43 -0
- data/spec/github/users_spec.rb +140 -0
- metadata +22 -5
- data/features/github.feature +0 -9
- data/features/step_definitions/github_steps.rb +0 -0
@@ -0,0 +1,50 @@
|
|
1
|
+
Feature: Github API components
|
2
|
+
|
3
|
+
In order to interact with full Github API
|
4
|
+
A user needs to request a Github class instance
|
5
|
+
|
6
|
+
Scenario: Accessing repositories API
|
7
|
+
Given I have github instance
|
8
|
+
When I fetch "repos"
|
9
|
+
Then I will have access to "Github::Repos" API
|
10
|
+
|
11
|
+
Scenario: Accessing organizations API
|
12
|
+
Given I have github instance
|
13
|
+
When I fetch "orgs"
|
14
|
+
Then I will have access to "Github::Orgs" API
|
15
|
+
|
16
|
+
Scenario: Accessing gists API
|
17
|
+
Given I have github instance
|
18
|
+
When I fetch "gists"
|
19
|
+
Then I will have access to "Github::Gists" API
|
20
|
+
|
21
|
+
Scenario: Accessing issues API
|
22
|
+
Given I have github instance
|
23
|
+
When I fetch "issues"
|
24
|
+
Then I will have access to "Github::Issues" API
|
25
|
+
|
26
|
+
Scenario: Accessing pull requests API
|
27
|
+
Given I have github instance
|
28
|
+
When I fetch "pull_requests"
|
29
|
+
Then I will have access to "Github::PullRequests" API
|
30
|
+
|
31
|
+
Scenario: Accessing git data API
|
32
|
+
Given I have github instance
|
33
|
+
When I fetch "git_data"
|
34
|
+
Then I will have access to "Github::GitData" API
|
35
|
+
|
36
|
+
Scenario: Accessing users API
|
37
|
+
Given I have github instance
|
38
|
+
When I fetch "users"
|
39
|
+
Then I will have access to "Github::Users" API
|
40
|
+
|
41
|
+
Scenario: Accessing users API
|
42
|
+
Given I have github instance
|
43
|
+
When I fetch "events"
|
44
|
+
Then I will have access to "Github::Events" API
|
45
|
+
|
46
|
+
Scenario: Accessing authorizations API
|
47
|
+
Given I have github instance
|
48
|
+
When I fetch "oauth"
|
49
|
+
Then I will have access to "Github::Authorizations" API
|
50
|
+
|
@@ -0,0 +1,11 @@
|
|
1
|
+
Given /^I have github instance$/ do
|
2
|
+
@github = Github.new
|
3
|
+
end
|
4
|
+
|
5
|
+
When /^I fetch "([^"]*)"$/ do |method|
|
6
|
+
@response = @github.send(method.to_sym)
|
7
|
+
end
|
8
|
+
|
9
|
+
When /^I will have access to "([^"]*)" API$/ do |api|
|
10
|
+
@response.class.to_s.should match api
|
11
|
+
end
|
data/features/support/env.rb
CHANGED
data/lib/github_api.rb
CHANGED
@@ -25,6 +25,7 @@ module Github
|
|
25
25
|
def respond_to?(method, include_private = false)
|
26
26
|
new.respond_to?(method, include_private) || super(method, include_private)
|
27
27
|
end
|
28
|
+
|
28
29
|
end
|
29
30
|
|
30
31
|
module AutoloadHelper
|
@@ -34,6 +35,17 @@ module Github
|
|
34
35
|
autoload const_name, File.join(prefix, path)
|
35
36
|
end
|
36
37
|
end
|
38
|
+
|
39
|
+
def register_constant(options)
|
40
|
+
options.each do |const_name, value|
|
41
|
+
const_set const_name.upcase.to_s, value
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def lookup_constant(const_name)
|
46
|
+
const_get const_name.upcase.to_s
|
47
|
+
end
|
48
|
+
|
37
49
|
end
|
38
50
|
|
39
51
|
extend AutoloadHelper
|
@@ -44,6 +56,7 @@ module Github
|
|
44
56
|
:Repos => 'repos',
|
45
57
|
:Request => 'request',
|
46
58
|
:Response => 'response',
|
59
|
+
:Result => 'result',
|
47
60
|
:Error => 'error',
|
48
61
|
:Issues => 'issues',
|
49
62
|
:Gists => 'gists',
|
@@ -4,6 +4,7 @@ require 'faraday'
|
|
4
4
|
require 'github_api/response'
|
5
5
|
require 'github_api/response/mashify'
|
6
6
|
require 'github_api/response/jsonize'
|
7
|
+
require 'github_api/response/helpers'
|
7
8
|
require 'github_api/response/raise_error'
|
8
9
|
require 'github_api/request/oauth2'
|
9
10
|
require 'github_api/request/basic_auth'
|
@@ -58,6 +59,7 @@ module Github
|
|
58
59
|
builder.use Github::Request::OAuth2, oauth_token if oauth_token?
|
59
60
|
builder.use Github::Request::BasicAuth, authentication if basic_authed?
|
60
61
|
|
62
|
+
builder.use Github::Response::Helpers
|
61
63
|
unless options[:raw]
|
62
64
|
builder.use Github::Response::Mashify
|
63
65
|
builder.use Github::Response::Jsonize
|
data/lib/github_api/response.rb
CHANGED
@@ -6,5 +6,23 @@ module Github
|
|
6
6
|
# Contains methods and attributes that act on the response returned from the
|
7
7
|
# request
|
8
8
|
class Response < Faraday::Response::Middleware
|
9
|
+
CONTENT_TYPE = 'Content-Type'.freeze
|
10
|
+
|
11
|
+
class << self
|
12
|
+
attr_accessor :parser
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.define_parser(&block)
|
16
|
+
@parser = block
|
17
|
+
end
|
18
|
+
|
19
|
+
def response_type(env)
|
20
|
+
env[:response_headers][CONTENT_TYPE].to_s
|
21
|
+
end
|
22
|
+
|
23
|
+
def parse_response?(env)
|
24
|
+
env[:body].respond_to? :to_str
|
25
|
+
end
|
26
|
+
|
9
27
|
end # Response
|
10
|
-
end
|
28
|
+
end # Github
|
@@ -3,9 +3,13 @@
|
|
3
3
|
require 'faraday'
|
4
4
|
|
5
5
|
module Github
|
6
|
-
class Response::Jsonize <
|
6
|
+
class Response::Jsonize < Response
|
7
7
|
dependency 'multi_json'
|
8
8
|
|
9
|
+
define_parser do |body|
|
10
|
+
::MultiJson.decode body
|
11
|
+
end
|
12
|
+
|
9
13
|
def parse(body)
|
10
14
|
case body
|
11
15
|
when ''
|
@@ -15,8 +19,8 @@ module Github
|
|
15
19
|
when 'false'
|
16
20
|
false
|
17
21
|
else
|
18
|
-
|
22
|
+
self.class.parser.call body
|
19
23
|
end
|
20
24
|
end
|
21
|
-
end
|
25
|
+
end # Response::Jsonize
|
22
26
|
end # Github
|
@@ -3,24 +3,22 @@
|
|
3
3
|
require 'faraday'
|
4
4
|
|
5
5
|
module Github
|
6
|
-
class Response::Mashify <
|
6
|
+
class Response::Mashify < Response
|
7
7
|
dependency 'hashie/mash'
|
8
8
|
|
9
|
-
|
10
|
-
|
9
|
+
define_parser do |body|
|
10
|
+
::Hashie::Mash.new body
|
11
11
|
end
|
12
12
|
|
13
|
-
self.mash_class = ::Hashie::Mash
|
14
|
-
|
15
13
|
def parse(body)
|
16
14
|
case body
|
17
15
|
when Hash
|
18
|
-
self.class.
|
16
|
+
self.class.parser.call body
|
19
17
|
when Array
|
20
|
-
body.map { |item| item.is_a?(Hash) ? self.class.
|
18
|
+
body.map { |item| item.is_a?(Hash) ? self.class.parser.call(item) : item }
|
21
19
|
else
|
22
20
|
body
|
23
21
|
end
|
24
22
|
end
|
25
|
-
end
|
23
|
+
end # Response::Mashify
|
26
24
|
end # Github
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Github
|
4
|
+
module Result
|
5
|
+
|
6
|
+
RATELIMIT = 'X-RateLimit-Remaining'.freeze
|
7
|
+
CONTENT_TYPE = 'Content-Type'.freeze
|
8
|
+
CONTENT_LENGTH = 'content-length'.freeze
|
9
|
+
|
10
|
+
attr_reader :env
|
11
|
+
|
12
|
+
# Requests are limited to API v3 to 5000 per hour.
|
13
|
+
def ratelimit
|
14
|
+
loaded? ? @env[:response_headers][RATELIMIT] : nil
|
15
|
+
end
|
16
|
+
|
17
|
+
def content_type
|
18
|
+
loaded? ? @env[:response_headers][CONTENT_TYPE] : nil
|
19
|
+
end
|
20
|
+
|
21
|
+
def content_length
|
22
|
+
loaded? ? @env[:response_headers][CONTENT_LENGTH] : nil
|
23
|
+
end
|
24
|
+
|
25
|
+
def status
|
26
|
+
loaded? ? @env[:status] : nil
|
27
|
+
end
|
28
|
+
|
29
|
+
def success?
|
30
|
+
(200..299).include? status
|
31
|
+
end
|
32
|
+
|
33
|
+
def body
|
34
|
+
loaded? ? @env[:body] : nil
|
35
|
+
end
|
36
|
+
|
37
|
+
def loaded?
|
38
|
+
!!env
|
39
|
+
end
|
40
|
+
|
41
|
+
end # Result
|
42
|
+
end # Github
|
data/lib/github_api/users.rb
CHANGED
@@ -44,11 +44,12 @@ module Github
|
|
44
44
|
def get_user(user_name=nil, params={})
|
45
45
|
_normalize_params_keys(params)
|
46
46
|
if user_name
|
47
|
-
get("/users/#{
|
47
|
+
get("/users/#{user_name}", params)
|
48
48
|
else
|
49
49
|
get("/user", params)
|
50
50
|
end
|
51
51
|
end
|
52
|
+
alias :get_auth_user :get_user
|
52
53
|
|
53
54
|
# Update the authenticated user
|
54
55
|
#
|
@@ -77,6 +78,7 @@ module Github
|
|
77
78
|
_filter_params_keys(VALID_USER_PARAMS_NAMES, params)
|
78
79
|
patch("/user", params)
|
79
80
|
end
|
81
|
+
alias :update_authenticated_user :update_user
|
80
82
|
|
81
83
|
end # Users
|
82
84
|
end # Github
|
data/lib/github_api/version.rb
CHANGED
@@ -0,0 +1,32 @@
|
|
1
|
+
{
|
2
|
+
"login": "octocat",
|
3
|
+
"id": 1,
|
4
|
+
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
|
5
|
+
"gravatar_id": "somehexcode",
|
6
|
+
"url": "https://api.github.com/users/octocat",
|
7
|
+
"name": "monalisa octocat",
|
8
|
+
"company": "GitHub",
|
9
|
+
"blog": "https://github.com/blog",
|
10
|
+
"location": "San Francisco",
|
11
|
+
"email": "octocat@github.com",
|
12
|
+
"hireable": false,
|
13
|
+
"bio": "There once was...",
|
14
|
+
"public_repos": 2,
|
15
|
+
"public_gists": 1,
|
16
|
+
"followers": 20,
|
17
|
+
"following": 0,
|
18
|
+
"html_url": "https://github.com/octocat",
|
19
|
+
"created_at": "2008-01-14T04:33:35Z",
|
20
|
+
"type": "User",
|
21
|
+
"total_private_repos": 100,
|
22
|
+
"owned_private_repos": 100,
|
23
|
+
"private_gists": 81,
|
24
|
+
"disk_usage": 10000,
|
25
|
+
"collaborators": 8,
|
26
|
+
"plan": {
|
27
|
+
"name": "Medium",
|
28
|
+
"space": 400,
|
29
|
+
"collaborators": 10,
|
30
|
+
"private_repos": 20
|
31
|
+
}
|
32
|
+
}
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Github::Result do
|
4
|
+
|
5
|
+
let(:github) { Github.new }
|
6
|
+
let(:res) { github.events.public }
|
7
|
+
|
8
|
+
before do
|
9
|
+
stub_get("/events").
|
10
|
+
to_return(:body => fixture('events/events.json'),
|
11
|
+
:status => 200,
|
12
|
+
:headers => {
|
13
|
+
:content_type => "application/json; charset=utf-8",
|
14
|
+
'X-RateLimit-Remaining' => '4999',
|
15
|
+
'content-length' => '344'
|
16
|
+
})
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should read response content_type " do
|
20
|
+
res.content_type.should match 'application/json'
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should read response content_length " do
|
24
|
+
res.content_length.should match '344'
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should read response ratelimit" do
|
28
|
+
res.ratelimit.should == '4999'
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should read response statsu" do
|
32
|
+
res.status.should be 200
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should assess successful" do
|
36
|
+
res.success?.should be_true
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should read response body" do
|
40
|
+
res.body.should_not be_empty
|
41
|
+
end
|
42
|
+
|
43
|
+
end # Github::Result
|
@@ -0,0 +1,140 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Github::Users do
|
4
|
+
|
5
|
+
let(:github) { Github.new }
|
6
|
+
let(:user) { 'peter-murach' }
|
7
|
+
|
8
|
+
before do
|
9
|
+
reset_authentication_for github
|
10
|
+
end
|
11
|
+
|
12
|
+
after do
|
13
|
+
reset_authentication_for github
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "get_user" do
|
17
|
+
context "resource found for a user" do
|
18
|
+
before do
|
19
|
+
stub_get("/users/#{user}").
|
20
|
+
to_return(:body => fixture('users/user.json'), :status => 200, :headers => {:content_type => "application/json; charset=utf-8"})
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should get the resources" do
|
24
|
+
github.users.get_user user
|
25
|
+
a_get("/users/#{user}").should have_been_made
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should return resource" do
|
29
|
+
user_resource = github.users.get_user user
|
30
|
+
user_resource.should be_a Hash
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should be a mash type" do
|
34
|
+
user_resource = github.users.get_user user
|
35
|
+
user_resource.should be_a Hashie::Mash
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should get org information" do
|
39
|
+
user_resource = github.users.get_user user
|
40
|
+
user_resource.login.should == 'octocat'
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should yield to a block" do
|
44
|
+
github.users.should_receive(:get_user).with(user).and_yield('web')
|
45
|
+
github.users.get_user(user) { |param| 'web' }
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context "resource found for an authenticated user" do
|
50
|
+
before do
|
51
|
+
reset_authentication_for github
|
52
|
+
github.oauth_token = OAUTH_TOKEN
|
53
|
+
stub_get("/user?access_token=#{OAUTH_TOKEN}").
|
54
|
+
to_return(:body => fixture('users/user.json'), :status => 200, :headers => {:content_type => "application/json; charset=utf-8"})
|
55
|
+
end
|
56
|
+
|
57
|
+
after do
|
58
|
+
reset_authentication_for github
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should get the resources" do
|
62
|
+
github.users.get_user
|
63
|
+
a_get("/user?access_token=#{OAUTH_TOKEN}").should have_been_made
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
context "resource not found for a user" do
|
68
|
+
before do
|
69
|
+
stub_get("/users/#{user}").
|
70
|
+
to_return(:body => "", :status => [404, "Not Found"])
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should return 404 with a message 'Not Found'" do
|
74
|
+
expect {
|
75
|
+
github.users.get_user user
|
76
|
+
}.to raise_error(Github::ResourceNotFound)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end # get_user
|
80
|
+
|
81
|
+
describe "update_user" do
|
82
|
+
let(:user_params) {
|
83
|
+
{
|
84
|
+
"name" => "monalisa octocat",
|
85
|
+
"email" => "octocat@github.com",
|
86
|
+
"blog" => "https://github.com/blog",
|
87
|
+
"company" => "GitHub",
|
88
|
+
"location" => "San Francisco",
|
89
|
+
"hireable" => true,
|
90
|
+
"bio" => "There once..."
|
91
|
+
}
|
92
|
+
}
|
93
|
+
|
94
|
+
context "resouce updated" do
|
95
|
+
before do
|
96
|
+
reset_authentication_for github
|
97
|
+
github.oauth_token = OAUTH_TOKEN
|
98
|
+
stub_patch("/user?access_token=#{OAUTH_TOKEN}").with(user_params).
|
99
|
+
to_return(:body => fixture('users/user.json'), :status => 200, :headers => {:content_type => "application/json; charset=utf-8"})
|
100
|
+
|
101
|
+
end
|
102
|
+
|
103
|
+
after do
|
104
|
+
reset_authentication_for github
|
105
|
+
end
|
106
|
+
|
107
|
+
it "should create resource successfully" do
|
108
|
+
github.users.update_user
|
109
|
+
a_patch("/user?access_token=#{OAUTH_TOKEN}").with(user_params).should have_been_made
|
110
|
+
end
|
111
|
+
|
112
|
+
it "should return the resource" do
|
113
|
+
user_resource = github.users.update_user
|
114
|
+
user_resource.should be_a Hashie::Mash
|
115
|
+
end
|
116
|
+
|
117
|
+
it "should get the resource information" do
|
118
|
+
user_resource = github.users.update_user
|
119
|
+
user_resource.login.should == 'octocat'
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
context "failed to update resource" do
|
124
|
+
before do
|
125
|
+
reset_authentication_for github
|
126
|
+
github.oauth_token = OAUTH_TOKEN
|
127
|
+
stub_patch("/user?access_token=#{OAUTH_TOKEN}").with(user_params).
|
128
|
+
to_return(:body => fixture('users/user.json'), :status => 404, :headers => {:content_type => "application/json; charset=utf-8"})
|
129
|
+
|
130
|
+
end
|
131
|
+
|
132
|
+
it "should fail to retrieve resource" do
|
133
|
+
expect {
|
134
|
+
github.users.update_user
|
135
|
+
}.to raise_error(Github::ResourceNotFound)
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end # update_comment
|
139
|
+
|
140
|
+
end # Github::Users
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 3
|
8
|
-
-
|
9
|
-
version: 0.3.
|
8
|
+
- 3
|
9
|
+
version: 0.3.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Piotr Murach
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-12-
|
17
|
+
date: 2011-12-04 00:00:00 +00:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -180,6 +180,18 @@ dependencies:
|
|
180
180
|
version: "0"
|
181
181
|
type: :development
|
182
182
|
version_requirements: *id012
|
183
|
+
- !ruby/object:Gem::Dependency
|
184
|
+
name: guard-cucumber
|
185
|
+
prerelease: false
|
186
|
+
requirement: &id013 !ruby/object:Gem::Requirement
|
187
|
+
requirements:
|
188
|
+
- - ">="
|
189
|
+
- !ruby/object:Gem::Version
|
190
|
+
segments:
|
191
|
+
- 0
|
192
|
+
version: "0"
|
193
|
+
type: :development
|
194
|
+
version_requirements: *id013
|
183
195
|
description: " Ruby wrapper that supports all of the GitHub API v3 methods(nearly 200). It's build in a modular way, that is, you can either instantiate the whole api wrapper Github.new or use parts of it e.i. Github::Repos.new if working solely with repositories is your main concern. "
|
184
196
|
email: ""
|
185
197
|
executables: []
|
@@ -190,8 +202,8 @@ extra_rdoc_files: []
|
|
190
202
|
|
191
203
|
files:
|
192
204
|
- Rakefile
|
193
|
-
- features/
|
194
|
-
- features/step_definitions/
|
205
|
+
- features/github_api.feature
|
206
|
+
- features/step_definitions/github_api_steps.rb
|
195
207
|
- features/support/env.rb
|
196
208
|
- lib/github_api/api/utils.rb
|
197
209
|
- lib/github_api/api.rb
|
@@ -238,10 +250,12 @@ files:
|
|
238
250
|
- lib/github_api/request/caching.rb
|
239
251
|
- lib/github_api/request/oauth2.rb
|
240
252
|
- lib/github_api/request.rb
|
253
|
+
- lib/github_api/response/helpers.rb
|
241
254
|
- lib/github_api/response/jsonize.rb
|
242
255
|
- lib/github_api/response/mashify.rb
|
243
256
|
- lib/github_api/response/raise_error.rb
|
244
257
|
- lib/github_api/response.rb
|
258
|
+
- lib/github_api/result.rb
|
245
259
|
- lib/github_api/users/emails.rb
|
246
260
|
- lib/github_api/users/followers.rb
|
247
261
|
- lib/github_api/users/keys.rb
|
@@ -281,6 +295,7 @@ files:
|
|
281
295
|
- spec/fixtures/repos/teams.json
|
282
296
|
- spec/fixtures/repos/watched.json
|
283
297
|
- spec/fixtures/repos/watchers.json
|
298
|
+
- spec/fixtures/users/user.json
|
284
299
|
- spec/github/api_spec.rb
|
285
300
|
- spec/github/authorization_spec.rb
|
286
301
|
- spec/github/authorizations_spec.rb
|
@@ -313,6 +328,8 @@ files:
|
|
313
328
|
- spec/github/repos/pub_sub_hubbub_spec.rb
|
314
329
|
- spec/github/repos/watching_spec.rb
|
315
330
|
- spec/github/repos_spec.rb
|
331
|
+
- spec/github/result_spec.rb
|
332
|
+
- spec/github/users_spec.rb
|
316
333
|
- spec/github_spec.rb
|
317
334
|
- spec/spec_helper.rb
|
318
335
|
- README.rdoc
|
data/features/github.feature
DELETED
File without changes
|