github_api 0.12.0 → 0.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +333 -327
- data/lib/github_api.rb +39 -4
- data/lib/github_api/api/actions.rb +35 -25
- data/lib/github_api/api/config/property_set.rb +1 -0
- data/lib/github_api/client/repos/deployments.rb +1 -0
- data/lib/github_api/configuration.rb +14 -6
- data/lib/github_api/connection.rb +5 -41
- data/lib/github_api/middleware.rb +30 -0
- data/lib/github_api/request/basic_auth.rb +3 -1
- data/lib/github_api/request/jsonize.rb +3 -1
- data/lib/github_api/version.rb +1 -1
- data/spec/github/api/actions_spec.rb +19 -0
- data/spec/github/api/set_spec.rb +2 -2
- data/spec/github/api/with_spec.rb +2 -3
- data/spec/github/api_spec.rb +1 -5
- data/spec/github/client/repos/deployments/create_spec.rb +2 -2
- data/spec/github/client/repos/deployments/create_status_spec.rb +2 -2
- data/spec/github/configuration_spec.rb +1 -1
- data/spec/github/configure_spec.rb +15 -0
- data/spec/github/request/jsonize_spec.rb +2 -2
- data/spec/github/stack_spec.rb +26 -0
- metadata +22 -24
data/lib/github_api.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
|
+
require 'faraday'
|
3
4
|
require 'github_api/version'
|
4
5
|
require 'github_api/configuration'
|
5
6
|
require 'github_api/constants'
|
@@ -8,6 +9,7 @@ require 'github_api/connection'
|
|
8
9
|
require 'github_api/deprecation'
|
9
10
|
require 'github_api/core_ext/ordered_hash'
|
10
11
|
require 'github_api/ext/faraday'
|
12
|
+
require 'github_api/middleware'
|
11
13
|
|
12
14
|
module Github
|
13
15
|
LIBNAME = 'github_api'
|
@@ -28,7 +30,17 @@ module Github
|
|
28
30
|
#
|
29
31
|
# @api public
|
30
32
|
def new(options = {}, &block)
|
31
|
-
|
33
|
+
Client.new(options, &block)
|
34
|
+
end
|
35
|
+
|
36
|
+
# Default middleware stack that uses default adapter as specified
|
37
|
+
# by configuration setup
|
38
|
+
#
|
39
|
+
# @return [Proc]
|
40
|
+
#
|
41
|
+
# @api private
|
42
|
+
def default_middleware(options = {})
|
43
|
+
Middleware.default(options)
|
32
44
|
end
|
33
45
|
|
34
46
|
# Delegate to Github::Client
|
@@ -52,7 +64,8 @@ module Github
|
|
52
64
|
end
|
53
65
|
|
54
66
|
module ClassMethods
|
55
|
-
|
67
|
+
|
68
|
+
# Requires internal libraries
|
56
69
|
#
|
57
70
|
# @param [String] prefix
|
58
71
|
# the relative path prefix
|
@@ -66,9 +79,31 @@ module Github
|
|
66
79
|
end
|
67
80
|
end
|
68
81
|
|
69
|
-
#
|
82
|
+
# The client configuration
|
83
|
+
#
|
84
|
+
# @return [Configuration]
|
85
|
+
#
|
86
|
+
# @api public
|
70
87
|
def configuration
|
71
|
-
@configuration ||=
|
88
|
+
@configuration ||= Configuration.new
|
89
|
+
end
|
90
|
+
|
91
|
+
# Configure options
|
92
|
+
#
|
93
|
+
# @example
|
94
|
+
# Github.configure do |c|
|
95
|
+
# c.some_option = true
|
96
|
+
# end
|
97
|
+
#
|
98
|
+
# @yield the configuration block
|
99
|
+
# @yieldparam configuration [Github::Configuration]
|
100
|
+
# the configuration instance
|
101
|
+
#
|
102
|
+
# @return [nil]
|
103
|
+
#
|
104
|
+
# @api public
|
105
|
+
def configure
|
106
|
+
yield configuration
|
72
107
|
end
|
73
108
|
end
|
74
109
|
|
@@ -1,48 +1,58 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
3
|
module Github
|
4
|
-
#
|
4
|
+
# Responsible for providing inspection of api methods
|
5
5
|
class API
|
6
6
|
# Returns all API public methods for a given class.
|
7
|
+
#
|
8
|
+
# @return [nil]
|
9
|
+
#
|
10
|
+
# @api public
|
7
11
|
def self.extend_with_actions(child_class)
|
8
12
|
child_class.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
|
9
13
|
def self.actions
|
10
|
-
self.new.api_methods_in(#{child_class})
|
14
|
+
self.new.api_methods_in(#{child_class}) +
|
15
|
+
self.new.module_methods_in(#{child_class})
|
11
16
|
end
|
17
|
+
|
12
18
|
def actions
|
13
|
-
api_methods_in(#{child_class})
|
19
|
+
api_methods_in(#{child_class}) + module_methods_in(#{child_class})
|
14
20
|
end
|
15
21
|
RUBY_EVAL
|
16
22
|
end
|
17
23
|
|
24
|
+
# Finds api methods in a class
|
25
|
+
#
|
26
|
+
# @param [Class] klass
|
27
|
+
# The klass to inspect for methods.
|
28
|
+
#
|
29
|
+
# @api private
|
18
30
|
def api_methods_in(klass)
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
if mod.to_s =~ /#{klass}/
|
25
|
-
puts "| \\ #{mod.to_s}"
|
26
|
-
mod.instance_methods(false).each do |met|
|
27
|
-
puts "| |--> #{met}"
|
28
|
-
end
|
29
|
-
puts "| /"
|
31
|
+
methods = klass.send(:instance_methods, false) - [:actions]
|
32
|
+
methods.sort.each_with_object([]) do |method_name, accumulator|
|
33
|
+
unless method_name.to_s.include?('with') ||
|
34
|
+
method_name.to_s.include?('without')
|
35
|
+
accumulator << method_name
|
30
36
|
end
|
37
|
+
accumulator
|
31
38
|
end
|
32
|
-
puts "---"
|
33
|
-
nil
|
34
39
|
end
|
35
40
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
41
|
+
# Finds methods included through class modules
|
42
|
+
#
|
43
|
+
# @param [Class] klass
|
44
|
+
# The klass to inspect for methods.
|
45
|
+
#
|
46
|
+
# @api private
|
47
|
+
def module_methods_in(klass)
|
48
|
+
klass.included_modules.each_with_object([]) do |mod, accumulator|
|
49
|
+
if mod.to_s =~ /#{klass}/
|
50
|
+
mod.instance_methods(false).each do |method|
|
51
|
+
accumulator << method
|
52
|
+
end
|
53
|
+
end
|
54
|
+
accumulator
|
44
55
|
end
|
45
|
-
args
|
46
56
|
end
|
47
57
|
end # API
|
48
58
|
end # Github
|
@@ -3,11 +3,18 @@
|
|
3
3
|
require 'github_api/api/config'
|
4
4
|
|
5
5
|
module Github
|
6
|
+
# Stores the configuration
|
6
7
|
class Configuration < API::Config
|
7
8
|
|
8
9
|
# Other adapters are :typhoeus, :patron, :em_synchrony, :excon, :test
|
9
10
|
property :adapter, default: :net_http
|
10
11
|
|
12
|
+
# By default, don't traverse the page links
|
13
|
+
property :auto_pagination, default: false
|
14
|
+
|
15
|
+
# Basic authentication
|
16
|
+
property :basic_auth
|
17
|
+
|
11
18
|
# By default, don't set an application key
|
12
19
|
property :client_id
|
13
20
|
|
@@ -28,15 +35,16 @@ module Github
|
|
28
35
|
:ca_file => File.expand_path('../ssl_certs/cacerts.pem', __FILE__)
|
29
36
|
}
|
30
37
|
|
31
|
-
# By default the
|
38
|
+
# By default the Accept header will make a request for JSON
|
32
39
|
property :mime_type
|
33
40
|
|
34
41
|
# The value sent in the http header for 'User-Agent' if none is set
|
35
|
-
property :user_agent, default: "Github Ruby Gem #{Github::VERSION::STRING}".freeze
|
42
|
+
property :user_agent, default: "Github API Ruby Gem #{Github::VERSION::STRING}".freeze
|
36
43
|
|
37
44
|
# By default uses the Faraday connection options if none is set
|
38
45
|
property :connection_options, default: {}
|
39
46
|
|
47
|
+
# Global repository name
|
40
48
|
property :repo
|
41
49
|
|
42
50
|
property :user
|
@@ -47,10 +55,10 @@ module Github
|
|
47
55
|
|
48
56
|
property :password
|
49
57
|
|
50
|
-
|
51
|
-
|
52
|
-
# By default, don't traverse the page links
|
53
|
-
property :auto_pagination, default: false
|
58
|
+
# By default display 30 resources
|
59
|
+
property :per_page, default: 30
|
54
60
|
|
61
|
+
# Add Faraday::RackBuilder to overwrite middleware
|
62
|
+
property :stack
|
55
63
|
end # Configuration
|
56
64
|
end # Github
|
@@ -1,12 +1,5 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
|
-
require 'faraday'
|
4
|
-
require 'github_api/response'
|
5
|
-
require 'github_api/response/mashify'
|
6
|
-
require 'github_api/response/jsonize'
|
7
|
-
require 'github_api/response/raise_error'
|
8
|
-
require 'github_api/response/header'
|
9
|
-
|
10
3
|
module Github
|
11
4
|
# Specifies Http connection options
|
12
5
|
module Connection
|
@@ -36,32 +29,6 @@ module Github
|
|
36
29
|
}
|
37
30
|
end
|
38
31
|
|
39
|
-
# Default middleware stack that uses default adapter as specified at
|
40
|
-
# configuration stage.
|
41
|
-
#
|
42
|
-
def default_middleware(options = {})
|
43
|
-
api = options[:api]
|
44
|
-
proc do |builder|
|
45
|
-
builder.use Github::Request::Jsonize
|
46
|
-
builder.use Faraday::Request::Multipart
|
47
|
-
builder.use Faraday::Request::UrlEncoded
|
48
|
-
builder.use Github::Request::OAuth2, api.oauth_token if api.oauth_token?
|
49
|
-
builder.use Github::Request::BasicAuth, api.authentication if api.basic_authed?
|
50
|
-
|
51
|
-
builder.use Faraday::Response::Logger if ENV['DEBUG']
|
52
|
-
unless options[:raw]
|
53
|
-
builder.use Github::Response::Mashify
|
54
|
-
builder.use Github::Response::Jsonize
|
55
|
-
end
|
56
|
-
builder.use Github::Response::RaiseError
|
57
|
-
builder.adapter options[:adapter]
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
@connection = nil
|
62
|
-
|
63
|
-
@stack = nil
|
64
|
-
|
65
32
|
def clear_cache
|
66
33
|
@connection = nil
|
67
34
|
end
|
@@ -73,15 +40,11 @@ module Github
|
|
73
40
|
# Exposes middleware builder to facilitate custom stacks and easy
|
74
41
|
# addition of new extensions such as cache adapter.
|
75
42
|
#
|
76
|
-
|
43
|
+
# @api public
|
44
|
+
def stack(options = {})
|
77
45
|
@stack ||= begin
|
78
46
|
builder_class = defined?(Faraday::RackBuilder) ? Faraday::RackBuilder : Faraday::Builder
|
79
|
-
|
80
|
-
if block_given?
|
81
|
-
builder_class.new(&block)
|
82
|
-
else
|
83
|
-
builder_class.new(&default_middleware(options))
|
84
|
-
end
|
47
|
+
builder_class.new(&Github.default_middleware(options))
|
85
48
|
end
|
86
49
|
end
|
87
50
|
|
@@ -91,7 +54,8 @@ module Github
|
|
91
54
|
def connection(api, options = {})
|
92
55
|
connection_options = default_options(options)
|
93
56
|
clear_cache unless options.empty?
|
94
|
-
|
57
|
+
builder = api.stack ? api.stack : stack(options.merge!(api: api))
|
58
|
+
connection_options.merge!(builder: builder)
|
95
59
|
if ENV['DEBUG']
|
96
60
|
p "Connection options : \n"
|
97
61
|
pp connection_options
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'github_api/response'
|
4
|
+
require 'github_api/response/mashify'
|
5
|
+
require 'github_api/response/jsonize'
|
6
|
+
require 'github_api/response/raise_error'
|
7
|
+
require 'github_api/response/header'
|
8
|
+
|
9
|
+
module Github
|
10
|
+
class Middleware
|
11
|
+
def self.default(options = {})
|
12
|
+
api = options[:api]
|
13
|
+
proc do |builder|
|
14
|
+
builder.use Github::Request::Jsonize
|
15
|
+
builder.use Faraday::Request::Multipart
|
16
|
+
builder.use Faraday::Request::UrlEncoded
|
17
|
+
builder.use Github::Request::OAuth2, api.oauth_token if api.oauth_token?
|
18
|
+
builder.use Github::Request::BasicAuth, api.authentication if api.basic_authed?
|
19
|
+
|
20
|
+
builder.use Faraday::Response::Logger if ENV['DEBUG']
|
21
|
+
unless options[:raw]
|
22
|
+
builder.use Github::Response::Mashify
|
23
|
+
builder.use Github::Response::Jsonize
|
24
|
+
end
|
25
|
+
builder.use Github::Response::RaiseError
|
26
|
+
builder.adapter options[:adapter]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end # Middleware
|
30
|
+
end # Github
|
@@ -9,7 +9,9 @@ module Github
|
|
9
9
|
dependency 'base64'
|
10
10
|
|
11
11
|
def call(env)
|
12
|
-
|
12
|
+
unless @auth.to_s.empty?
|
13
|
+
env[:request_headers].merge!('Authorization' => "Basic #{@auth}\"")
|
14
|
+
end
|
13
15
|
|
14
16
|
@app.call env
|
15
17
|
end
|
@@ -16,7 +16,9 @@ module Github
|
|
16
16
|
env[:body] = encode_body env[:body] unless env[:body].respond_to?(:to_str)
|
17
17
|
else
|
18
18
|
# Ensure valid body for put and post requests
|
19
|
-
env[:
|
19
|
+
if [:put, :patch, :post].include? env[:method]
|
20
|
+
env[:body] = encode_body({})
|
21
|
+
end
|
20
22
|
end
|
21
23
|
@app.call env
|
22
24
|
end
|
data/lib/github_api/version.rb
CHANGED
@@ -0,0 +1,19 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Github::API, '#actions' do
|
6
|
+
let(:api) { Github::Client::Repos::Contents }
|
7
|
+
|
8
|
+
context 'when class' do
|
9
|
+
it "lists all available actions for an api" do
|
10
|
+
expect(api.actions).to eq([:archive, :create, :delete, :find, :get, :readme, :update])
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context 'when instance' do
|
15
|
+
it "lists all available actions for an api" do
|
16
|
+
expect(api.new.actions).to eq([:archive, :create, :delete, :find, :get, :readme, :update])
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/spec/github/api/set_spec.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
@@ -11,7 +11,7 @@ describe Github::API, '#set' do
|
|
11
11
|
end
|
12
12
|
|
13
13
|
context 'accpets more than one option' do
|
14
|
-
before { subject.set :
|
14
|
+
before { subject.set user: 'user-name', repo: 'repo-name' }
|
15
15
|
|
16
16
|
it 'sets user' do
|
17
17
|
expect(subject.user).to eql('user-name')
|
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
@@ -9,7 +9,7 @@ describe Github::API, '#with' do
|
|
9
9
|
|
10
10
|
context 'with hash' do
|
11
11
|
it 'supports list of options' do
|
12
|
-
subject.with(:
|
12
|
+
subject.with(user: user, repo: 'github')
|
13
13
|
subject.user.should == user
|
14
14
|
end
|
15
15
|
end
|
@@ -24,5 +24,4 @@ describe Github::API, '#with' do
|
|
24
24
|
expect { subject.with('peter-murach') }.to raise_error(ArgumentError)
|
25
25
|
end
|
26
26
|
end
|
27
|
-
|
28
27
|
end
|
data/spec/github/api_spec.rb
CHANGED
@@ -23,11 +23,7 @@ describe Github::API do
|
|
23
23
|
it 'ensures output contains api methods' do
|
24
24
|
methods = [ 'method_a', 'method_b']
|
25
25
|
repos.stub(:instance_methods).and_return methods
|
26
|
-
|
27
|
-
subject.api_methods_in(repos)
|
28
|
-
}
|
29
|
-
output.should =~ /.*method_a.*/
|
30
|
-
output.should =~ /.*method_b.*/
|
26
|
+
expect(subject.api_methods_in(repos)).to eq(['method_a', 'method_b'])
|
31
27
|
end
|
32
28
|
end
|
33
29
|
|
@@ -14,7 +14,7 @@ describe Github::Client::Repos::Deployments, '#create' do
|
|
14
14
|
}
|
15
15
|
|
16
16
|
before {
|
17
|
-
stub_post(request_path).with(params).
|
17
|
+
stub_post(request_path).with(:body => params).
|
18
18
|
to_return(:body => body, :status => status,
|
19
19
|
:headers => {:content_type => "application/json; charset=utf-8"})
|
20
20
|
}
|
@@ -33,7 +33,7 @@ describe Github::Client::Repos::Deployments, '#create' do
|
|
33
33
|
|
34
34
|
it "should create resource successfully" do
|
35
35
|
subject.create user, repo, params
|
36
|
-
a_post(request_path).with(params).should have_been_made
|
36
|
+
a_post(request_path).with(:body => params).should have_been_made
|
37
37
|
end
|
38
38
|
|
39
39
|
it "should return the resource" do
|