github_api 0.3.5 → 0.3.6
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/LICENSE.txt +20 -0
- data/README.rdoc +228 -0
- data/Rakefile +52 -0
- data/features/github_api.feature +50 -0
- data/features/options.feature +4 -0
- data/features/pagination.feature +4 -0
- data/features/step_definitions/github_api_steps.rb +11 -0
- data/features/support/env.rb +13 -0
- data/lib/github_api.rb +73 -0
- data/lib/github_api/api.rb +175 -0
- data/lib/github_api/api/utils.rb +9 -0
- data/lib/github_api/authorization.rb +73 -0
- data/lib/github_api/authorizations.rb +119 -0
- data/lib/github_api/cache_control.rb +19 -0
- data/lib/github_api/client.rb +55 -0
- data/lib/github_api/compatibility.rb +23 -0
- data/lib/github_api/configuration.rb +96 -0
- data/lib/github_api/connection.rb +75 -0
- data/lib/github_api/core_ext/array.rb +14 -0
- data/lib/github_api/core_ext/hash.rb +42 -0
- data/lib/github_api/error.rb +35 -0
- data/lib/github_api/events.rb +202 -0
- data/lib/github_api/gists.rb +200 -0
- data/lib/github_api/gists/comments.rb +86 -0
- data/lib/github_api/git_data.rb +26 -0
- data/lib/github_api/git_data/blobs.rb +51 -0
- data/lib/github_api/git_data/commits.rb +78 -0
- data/lib/github_api/git_data/references.rb +113 -0
- data/lib/github_api/git_data/tags.rb +78 -0
- data/lib/github_api/git_data/trees.rb +89 -0
- data/lib/github_api/issues.rb +215 -0
- data/lib/github_api/issues/comments.rb +123 -0
- data/lib/github_api/issues/events.rb +54 -0
- data/lib/github_api/issues/labels.rb +191 -0
- data/lib/github_api/issues/milestones.rb +140 -0
- data/lib/github_api/mime_type.rb +55 -0
- data/lib/github_api/orgs.rb +95 -0
- data/lib/github_api/orgs/members.rb +120 -0
- data/lib/github_api/orgs/teams.rb +245 -0
- data/lib/github_api/pull_requests.rb +224 -0
- data/lib/github_api/pull_requests/comments.rb +144 -0
- data/lib/github_api/repos.rb +286 -0
- data/lib/github_api/repos/collaborators.rb +81 -0
- data/lib/github_api/repos/commits.rb +180 -0
- data/lib/github_api/repos/downloads.rb +155 -0
- data/lib/github_api/repos/forks.rb +48 -0
- data/lib/github_api/repos/hooks.rb +174 -0
- data/lib/github_api/repos/keys.rb +104 -0
- data/lib/github_api/repos/pub_sub_hubbub.rb +102 -0
- data/lib/github_api/repos/watching.rb +94 -0
- data/lib/github_api/request.rb +84 -0
- data/lib/github_api/request/basic_auth.rb +31 -0
- data/lib/github_api/request/caching.rb +33 -0
- data/lib/github_api/request/oauth2.rb +33 -0
- data/lib/github_api/response.rb +28 -0
- data/lib/github_api/response/helpers.rb +14 -0
- data/lib/github_api/response/jsonize.rb +26 -0
- data/lib/github_api/response/mashify.rb +24 -0
- data/lib/github_api/response/raise_error.rb +33 -0
- data/lib/github_api/result.rb +42 -0
- data/lib/github_api/users.rb +84 -0
- data/lib/github_api/users/emails.rb +49 -0
- data/lib/github_api/users/followers.rb +98 -0
- data/lib/github_api/users/keys.rb +84 -0
- data/lib/github_api/version.rb +12 -0
- data/spec/README.rdoc +22 -0
- data/spec/coverage_adapter.rb +15 -0
- data/spec/fixtures/auths/authorization.json +14 -0
- data/spec/fixtures/auths/authorizations.json +16 -0
- data/spec/fixtures/events/events.json +29 -0
- data/spec/fixtures/issues/comment.json +13 -0
- data/spec/fixtures/issues/comments.json +15 -0
- data/spec/fixtures/issues/event.json +13 -0
- data/spec/fixtures/issues/events.json +15 -0
- data/spec/fixtures/issues/issue.json +56 -0
- data/spec/fixtures/issues/issues.json +58 -0
- data/spec/fixtures/issues/milestone.json +18 -0
- data/spec/fixtures/issues/milestones.json +20 -0
- data/spec/fixtures/orgs/members.json +9 -0
- data/spec/fixtures/orgs/org.json +18 -0
- data/spec/fixtures/orgs/orgs.json +8 -0
- data/spec/fixtures/orgs/team.json +8 -0
- data/spec/fixtures/orgs/team_repos.json +29 -0
- data/spec/fixtures/orgs/teams.json +7 -0
- data/spec/fixtures/repos/branches.json +9 -0
- data/spec/fixtures/repos/collaborators.json +8 -0
- data/spec/fixtures/repos/commit.json +53 -0
- data/spec/fixtures/repos/commit_comment.json +16 -0
- data/spec/fixtures/repos/commit_comments.json +18 -0
- data/spec/fixtures/repos/commits.json +27 -0
- data/spec/fixtures/repos/contributors.json +8 -0
- data/spec/fixtures/repos/download.json +10 -0
- data/spec/fixtures/repos/download_s3.json +21 -0
- data/spec/fixtures/repos/downloads.json +12 -0
- data/spec/fixtures/repos/fork.json +27 -0
- data/spec/fixtures/repos/forks.json +29 -0
- data/spec/fixtures/repos/hook.json +15 -0
- data/spec/fixtures/repos/hooks.json +10 -0
- data/spec/fixtures/repos/key.json +6 -0
- data/spec/fixtures/repos/keys.json +8 -0
- data/spec/fixtures/repos/languages.json +4 -0
- data/spec/fixtures/repos/repo.json +90 -0
- data/spec/fixtures/repos/repo_comments.json +18 -0
- data/spec/fixtures/repos/repos.json +29 -0
- data/spec/fixtures/repos/tags.json +11 -0
- data/spec/fixtures/repos/teams.json +7 -0
- data/spec/fixtures/repos/watched.json +29 -0
- data/spec/fixtures/repos/watchers.json +8 -0
- data/spec/fixtures/users/user.json +32 -0
- data/spec/github/api_spec.rb +25 -0
- data/spec/github/authorization_spec.rb +176 -0
- data/spec/github/authorizations_spec.rb +242 -0
- data/spec/github/client_spec.rb +50 -0
- data/spec/github/core_ext/hash_spec.rb +44 -0
- data/spec/github/events_spec.rb +491 -0
- data/spec/github/gists/comments_spec.rb +5 -0
- data/spec/github/gists_spec.rb +5 -0
- data/spec/github/git_data/blobs_spec.rb +5 -0
- data/spec/github/git_data/commits_spec.rb +5 -0
- data/spec/github/git_data/references_spec.rb +5 -0
- data/spec/github/git_data/tags_spec.rb +5 -0
- data/spec/github/git_data/trees_spec.rb +5 -0
- data/spec/github/git_data_spec.rb +5 -0
- data/spec/github/issues/comments_spec.rb +254 -0
- data/spec/github/issues/events_spec.rb +153 -0
- data/spec/github/issues/labels_spec.rb +5 -0
- data/spec/github/issues/milestones_spec.rb +260 -0
- data/spec/github/issues_spec.rb +287 -0
- data/spec/github/mime_type_spec.rb +70 -0
- data/spec/github/orgs/members_spec.rb +275 -0
- data/spec/github/orgs/teams_spec.rb +563 -0
- data/spec/github/orgs_spec.rb +160 -0
- data/spec/github/repos/collaborators_spec.rb +169 -0
- data/spec/github/repos/commits_spec.rb +424 -0
- data/spec/github/repos/downloads_spec.rb +247 -0
- data/spec/github/repos/forks_spec.rb +108 -0
- data/spec/github/repos/hooks_spec.rb +333 -0
- data/spec/github/repos/keys_spec.rb +217 -0
- data/spec/github/repos/pub_sub_hubbub_spec.rb +83 -0
- data/spec/github/repos/watching_spec.rb +222 -0
- data/spec/github/repos_spec.rb +571 -0
- data/spec/github/result_spec.rb +43 -0
- data/spec/github/users_spec.rb +140 -0
- data/spec/github_spec.rb +109 -0
- data/spec/spec_helper.rb +86 -0
- data/spec/support/base.rb +13 -0
- metadata +149 -4
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
module Github
|
|
4
|
+
class Repos
|
|
5
|
+
module Keys
|
|
6
|
+
|
|
7
|
+
VALID_KEY_PARAM_NAMES = %w[ title key ].freeze
|
|
8
|
+
|
|
9
|
+
# List deploy keys
|
|
10
|
+
#
|
|
11
|
+
# = Examples
|
|
12
|
+
# @github = Github.new
|
|
13
|
+
# @github.repos.keys 'user-name', 'repo-name'
|
|
14
|
+
# @github.repos.keys 'user-name', 'repo-name' { |key| ... }
|
|
15
|
+
#
|
|
16
|
+
def keys(user_name=nil, repo_name=nil, params={})
|
|
17
|
+
_update_user_repo_params(user_name, repo_name)
|
|
18
|
+
_validate_user_repo_params(user, repo) unless user? && repo?
|
|
19
|
+
_normalize_params_keys(params)
|
|
20
|
+
|
|
21
|
+
response = get("/repos/#{user}/#{repo}/keys", params)
|
|
22
|
+
return response unless block_given?
|
|
23
|
+
response.each { |el| yield el }
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Get a key
|
|
27
|
+
#
|
|
28
|
+
# = Examples
|
|
29
|
+
# @github = Github.new
|
|
30
|
+
# @github.repos.get_key 'user-name', 'repo-name', 'key-id'
|
|
31
|
+
#
|
|
32
|
+
def get_key(user_name, repo_name, key_id, params={})
|
|
33
|
+
_update_user_repo_params(user_name, repo_name)
|
|
34
|
+
_validate_user_repo_params(user, repo) unless user? && repo?
|
|
35
|
+
_validate_presence_of key_id
|
|
36
|
+
_normalize_params_keys(params)
|
|
37
|
+
|
|
38
|
+
get("/repos/#{user}/#{repo}/keys/#{key_id}", params)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Create a key
|
|
42
|
+
#
|
|
43
|
+
# = Inputs
|
|
44
|
+
# * <tt>:title</tt> - Required string.
|
|
45
|
+
# * <tt>:key</tt> - Required string.
|
|
46
|
+
#
|
|
47
|
+
# = Examples
|
|
48
|
+
# @github = Github.new
|
|
49
|
+
# @github.repos.create_key 'user-name', 'repo-name',
|
|
50
|
+
# "title" => "octocat@octomac",
|
|
51
|
+
# "key" => "ssh-rsa AAA..."
|
|
52
|
+
#
|
|
53
|
+
def create_key(user_name=nil, repo_name=nil, params={})
|
|
54
|
+
_update_user_repo_params(user_name, repo_name)
|
|
55
|
+
_validate_user_repo_params(user, repo) unless user? && repo?
|
|
56
|
+
_normalize_params_keys(params)
|
|
57
|
+
_filter_params_keys(VALID_KEY_PARAM_NAMES, params)
|
|
58
|
+
|
|
59
|
+
raise ArgumentError, "Required params are: #{VALID_KEY_PARAM_NAMES.join(', ')}" unless _validate_inputs(VALID_KEY_PARAM_NAMES, params)
|
|
60
|
+
|
|
61
|
+
post("/repos/#{user}/#{repo}/keys", params)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Edit a key
|
|
65
|
+
#
|
|
66
|
+
# = Inputs
|
|
67
|
+
# * <tt>:title</tt> - Required string.
|
|
68
|
+
# * <tt>:key</tt> - Required string.
|
|
69
|
+
#
|
|
70
|
+
# = Examples
|
|
71
|
+
# @github = Github.new
|
|
72
|
+
# @github.repos.create_key 'user-name', 'repo-name',
|
|
73
|
+
# "title" => "octocat@octomac",
|
|
74
|
+
# "key" => "ssh-rsa AAA..."
|
|
75
|
+
#
|
|
76
|
+
def edit_key(user_name, repo_name, key_id, params={})
|
|
77
|
+
_update_user_repo_params(user_name, repo_name)
|
|
78
|
+
_validate_user_repo_params(user, repo) unless user? && repo?
|
|
79
|
+
_validate_presence_of key_id
|
|
80
|
+
|
|
81
|
+
_normalize_params_keys(params)
|
|
82
|
+
_filter_params_keys(VALID_KEY_PARAM_NAMES, params)
|
|
83
|
+
|
|
84
|
+
patch("/repos/#{user}/#{repo}/keys/#{key_id}", params)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# Delete key
|
|
88
|
+
#
|
|
89
|
+
# = Examples
|
|
90
|
+
# @github = Github.new
|
|
91
|
+
# @github.repos.delete_key 'user-name', 'repo-name', 'key-id'
|
|
92
|
+
#
|
|
93
|
+
def delete_key(user_name, repo_name, key_id, params={})
|
|
94
|
+
_update_user_repo_params(user_name, repo_name)
|
|
95
|
+
_validate_user_repo_params(user, repo) unless user? && repo?
|
|
96
|
+
_validate_presence_of key_id
|
|
97
|
+
_normalize_params_keys(params)
|
|
98
|
+
|
|
99
|
+
delete("/repos/#{user}/#{repo}/keys/#{key_id}", params)
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
end # Keys
|
|
103
|
+
end # Repos
|
|
104
|
+
end # Github
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
module Github
|
|
4
|
+
class Repos
|
|
5
|
+
module PubSubHubbub
|
|
6
|
+
|
|
7
|
+
# Subscribe to existing topic through pubsubhubbub
|
|
8
|
+
#
|
|
9
|
+
# = Parameters
|
|
10
|
+
# * topic - Required string - The URI of the GitHub repository to subscribe to. The path must be in the format of /:user/:repo/events/:event.
|
|
11
|
+
# * callback - Required string - The URI to receive the updates to the topic.
|
|
12
|
+
#
|
|
13
|
+
# = Examples
|
|
14
|
+
# @github = Github.new :oauth_token => '...'
|
|
15
|
+
# @github.subscribe 'https://github.com/:user/:repo/events/push',
|
|
16
|
+
# 'github://Email?address=peter-murach@gmail.com',
|
|
17
|
+
# :verify => 'sync',
|
|
18
|
+
# :secret => '...'
|
|
19
|
+
#
|
|
20
|
+
def subscribe(topic, callback, params={})
|
|
21
|
+
_validate_presence_of topic, callback
|
|
22
|
+
_normalize_params_keys(params)
|
|
23
|
+
_merge_action!("subscribe", topic, callback, params)
|
|
24
|
+
|
|
25
|
+
post("/hub", params)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Unsubscribe from existing topic though pubsubhubbub
|
|
29
|
+
#
|
|
30
|
+
#
|
|
31
|
+
def unsubscribe(topic, callback, params={})
|
|
32
|
+
_validate_presence_of topic, callback
|
|
33
|
+
_normalize_params_keys(params)
|
|
34
|
+
_merge_action!("unsubscribe", topic, callback, params)
|
|
35
|
+
|
|
36
|
+
post("/hub", params)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Subscribe repository to service hook through pubsubhubbub
|
|
40
|
+
#
|
|
41
|
+
# = Parameters
|
|
42
|
+
# * repo-name - Required string,
|
|
43
|
+
# * service-name - Required string
|
|
44
|
+
# * <tt>:event</tt> - Required hash key for the type of event. The default event is <tt>push</tt>
|
|
45
|
+
#
|
|
46
|
+
# = Examples
|
|
47
|
+
# @github = Github.new :oauth_token => '...'
|
|
48
|
+
# @github.repos.subscribe_service 'user-name', 'repo-name', 'campfire',
|
|
49
|
+
# :subdomain => 'github',
|
|
50
|
+
# :room => 'Commits',
|
|
51
|
+
# :token => 'abc123',
|
|
52
|
+
# :event => 'watch'
|
|
53
|
+
#
|
|
54
|
+
def subscribe_service(user_name, repo_name, service_name, params={})
|
|
55
|
+
_validate_presence_of user_name, repo_name, service_name
|
|
56
|
+
_normalize_params_keys(params)
|
|
57
|
+
event = params.delete('event') || 'push'
|
|
58
|
+
topic = "https://github.com/#{user_name}/#{repo_name}/events/#{event}"
|
|
59
|
+
callback = "github://#{service_name}?#{params.serialize}"
|
|
60
|
+
|
|
61
|
+
subscribe(topic, callback)
|
|
62
|
+
end
|
|
63
|
+
alias :subscribe_repository :subscribe_service
|
|
64
|
+
|
|
65
|
+
# Subscribe repository to service hook through pubsubhubbub
|
|
66
|
+
#
|
|
67
|
+
# = Parameters
|
|
68
|
+
# * repo-name - Required string,
|
|
69
|
+
# * service-name - Required string
|
|
70
|
+
# * <tt>:event</tt> - Optional hash key for the type of event. The default event is <tt>push</tt>
|
|
71
|
+
#
|
|
72
|
+
# = Examples
|
|
73
|
+
# @github = Github.new :oauth_token => '...'
|
|
74
|
+
# @github.repos.unsubscribe_service 'user-name', 'repo-name', 'campfire'
|
|
75
|
+
#
|
|
76
|
+
def unsubscribe_service(user_name, repo_name, service_name, params={})
|
|
77
|
+
_validate_presence_of user_name, repo_name, service_name
|
|
78
|
+
_normalize_params_keys(params)
|
|
79
|
+
event = params.delete('event') || 'push'
|
|
80
|
+
topic = "https://github.com/#{user_name}/#{repo_name}/events/#{event}"
|
|
81
|
+
callback = "github://#{service_name}"
|
|
82
|
+
|
|
83
|
+
unsubscribe(topic, callback)
|
|
84
|
+
end
|
|
85
|
+
alias :unsubscribe_repository :unsubscribe_service
|
|
86
|
+
|
|
87
|
+
private
|
|
88
|
+
|
|
89
|
+
def _merge_action!(action, topic, callback, params) # :nodoc:
|
|
90
|
+
options = {
|
|
91
|
+
"hub.mode" => action.to_s,
|
|
92
|
+
"hub.topic" => topic.to_s,
|
|
93
|
+
"hub.callback" => callback,
|
|
94
|
+
"hub.verify" => params.delete('verify') || 'sync',
|
|
95
|
+
"hub.secret" => params.delete('secret') || ''
|
|
96
|
+
}
|
|
97
|
+
params.merge! options
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
end # PubSubHubbub
|
|
101
|
+
end # Repos
|
|
102
|
+
end # Github
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
module Github
|
|
4
|
+
class Repos
|
|
5
|
+
module Watching
|
|
6
|
+
|
|
7
|
+
# List repo watchers
|
|
8
|
+
#
|
|
9
|
+
# = Examples
|
|
10
|
+
# @github = Github.new :user => 'user-name', :repo => 'repo-name'
|
|
11
|
+
# @github.repos.watchers
|
|
12
|
+
# @github.repos.watchers { |watcher| ... }
|
|
13
|
+
#
|
|
14
|
+
def watchers(user_name=nil, repo_name=nil, params={})
|
|
15
|
+
_update_user_repo_params(user_name, repo_name)
|
|
16
|
+
_validate_user_repo_params(user, repo) unless user? && repo?
|
|
17
|
+
_normalize_params_keys(params)
|
|
18
|
+
|
|
19
|
+
response = get("/repos/#{user}/#{repo}/watchers", params)
|
|
20
|
+
return response unless block_given?
|
|
21
|
+
response.each { |el| yield el }
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# List repos being watched by a user
|
|
25
|
+
#
|
|
26
|
+
# = Examples
|
|
27
|
+
# @github = Github.new :user => 'user-name'
|
|
28
|
+
# @github.repos.watched
|
|
29
|
+
#
|
|
30
|
+
# List repos being watched by the authenticated user
|
|
31
|
+
#
|
|
32
|
+
# = Examples
|
|
33
|
+
# @github = Github.new :oauth_token => '...'
|
|
34
|
+
# @github.repos.watched
|
|
35
|
+
#
|
|
36
|
+
def watched(user_name=nil, params={})
|
|
37
|
+
_update_user_repo_params(user_name)
|
|
38
|
+
_normalize_params_keys(params)
|
|
39
|
+
|
|
40
|
+
response = if user
|
|
41
|
+
get("/users/#{user}/watched", params)
|
|
42
|
+
else
|
|
43
|
+
get("/user/watched", params)
|
|
44
|
+
end
|
|
45
|
+
return response unless block_given?
|
|
46
|
+
response.each { |el| yield el }
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Check if you are watching a repository
|
|
50
|
+
#
|
|
51
|
+
# Returns <tt>true</tt> if this repo is watched by you, <tt>false</tt> otherwise
|
|
52
|
+
# = Examples
|
|
53
|
+
# @github = Github.new
|
|
54
|
+
# @github.repos.watching? 'user-name', 'repo-name'
|
|
55
|
+
#
|
|
56
|
+
def watching?(user_name, repo_name, params={})
|
|
57
|
+
_validate_presence_of user_name, repo_name
|
|
58
|
+
_normalize_params_keys(params)
|
|
59
|
+
get("/user/watched/#{user_name}/#{repo_name}", params)
|
|
60
|
+
true
|
|
61
|
+
rescue Github::ResourceNotFound
|
|
62
|
+
false
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Watch a repository
|
|
66
|
+
#
|
|
67
|
+
# You need to be authenticated to watch a repository
|
|
68
|
+
#
|
|
69
|
+
# = Examples
|
|
70
|
+
# @github = Github.new
|
|
71
|
+
# @github.repos.start_watching 'user-name', 'repo-name'
|
|
72
|
+
#
|
|
73
|
+
def start_watching(user_name, repo_name, params={})
|
|
74
|
+
_validate_presence_of user_name, repo_name
|
|
75
|
+
_normalize_params_keys(params)
|
|
76
|
+
put("/user/watched/#{user_name}/#{repo_name}", params)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# Stop watching a repository
|
|
80
|
+
#
|
|
81
|
+
# You need to be authenticated to stop watching a repository.
|
|
82
|
+
# = Examples
|
|
83
|
+
# @github = Github.new
|
|
84
|
+
# @github.repos.start_watching 'user-name', 'repo-name'
|
|
85
|
+
#
|
|
86
|
+
def stop_watching(user_name, repo_name, params={})
|
|
87
|
+
_validate_presence_of user_name, repo_name
|
|
88
|
+
_normalize_params_keys(params)
|
|
89
|
+
delete("/user/watched/#{user_name}/#{repo_name}", params)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
end # Watching
|
|
93
|
+
end # Repos
|
|
94
|
+
end # Github
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'base64'
|
|
4
|
+
require 'addressable/uri'
|
|
5
|
+
require 'multi_json'
|
|
6
|
+
|
|
7
|
+
module Github
|
|
8
|
+
# Defines HTTP verbs
|
|
9
|
+
module Request
|
|
10
|
+
|
|
11
|
+
METHODS = [:get, :post, :put, :delete, :patch]
|
|
12
|
+
METHODS_WITH_BODIES = [ :post, :put, :patch ]
|
|
13
|
+
|
|
14
|
+
TOKEN_REQUIRED_REGEXP = [
|
|
15
|
+
/repos\/.*\/.*\/comments/,
|
|
16
|
+
]
|
|
17
|
+
|
|
18
|
+
def get(path, params={}, options={})
|
|
19
|
+
request(:get, path, params, options)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def patch(path, params={}, options={})
|
|
23
|
+
request(:patch, path, params, options)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def post(path, params={}, options={})
|
|
27
|
+
request(:post, path, params, options)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def put(path, params={}, options={})
|
|
31
|
+
request(:put, path, params, options)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def delete(path, params={}, options={})
|
|
35
|
+
request(:delete, path, params, options)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def request(method, path, params, options)
|
|
39
|
+
if !METHODS.include?(method)
|
|
40
|
+
raise ArgumentError, "unkown http method: #{method}"
|
|
41
|
+
end
|
|
42
|
+
_extract_mime_type(params, options)
|
|
43
|
+
|
|
44
|
+
puts "EXECUTED: #{method} - #{path} with #{params} and #{options}" if ENV['DEBUG']
|
|
45
|
+
|
|
46
|
+
response = connection(options).send(method) do |request|
|
|
47
|
+
case method.to_sym
|
|
48
|
+
when *(METHODS - METHODS_WITH_BODIES)
|
|
49
|
+
request.url(path, params)
|
|
50
|
+
when *METHODS_WITH_BODIES
|
|
51
|
+
request.path = path
|
|
52
|
+
request.body = MultiJson.encode(_process_params(params)) unless params.empty?
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
response.body
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
private
|
|
59
|
+
|
|
60
|
+
def _process_params(params) # :nodoc:
|
|
61
|
+
return params['data'] if params.has_key?('data')
|
|
62
|
+
return params
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def _extract_mime_type(params, options) # :nodoc:
|
|
66
|
+
options['resource'] = params.delete('resource') || ''
|
|
67
|
+
options['mime_type'] = params.delete('mime_type')
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# no need for this smizzle
|
|
71
|
+
def formatted_path(path, options={})
|
|
72
|
+
[ path, options.fetch(:format, format) ].compact.join('.')
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def basic_auth(login, password) # :nodoc:
|
|
76
|
+
auth = Base64.encode("#{login}:#{password}")
|
|
77
|
+
auth.gsub!("\n", "")
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def token_auth
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
end # Request
|
|
84
|
+
end # Github
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'faraday'
|
|
4
|
+
require 'base64'
|
|
5
|
+
|
|
6
|
+
module Github
|
|
7
|
+
module Request
|
|
8
|
+
class BasicAuth < Faraday::Middleware
|
|
9
|
+
dependency 'base64'
|
|
10
|
+
|
|
11
|
+
def call(env)
|
|
12
|
+
env[:request_headers].merge!('Authorization' => "Basic #{@auth}\"")
|
|
13
|
+
|
|
14
|
+
@app.call env
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def initialize(app, *args)
|
|
18
|
+
@app = app
|
|
19
|
+
credentials = ""
|
|
20
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
|
21
|
+
if options.has_key? :login
|
|
22
|
+
credentials = "#{options[:login]}:#{options[:password]}"
|
|
23
|
+
elsif options.has_key? :basic_auth
|
|
24
|
+
credentials = "#{options[:basic_auth]}"
|
|
25
|
+
end
|
|
26
|
+
@auth = Base64.encode64(credentials)
|
|
27
|
+
@auth.gsub!("\n", "")
|
|
28
|
+
end
|
|
29
|
+
end # BasicAuth
|
|
30
|
+
end # Request
|
|
31
|
+
end # Github
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'faraday'
|
|
4
|
+
|
|
5
|
+
module Github
|
|
6
|
+
module Request
|
|
7
|
+
class Caching < Faraday::Middleware
|
|
8
|
+
attr_reader :cache
|
|
9
|
+
|
|
10
|
+
def initialize(app, cache = nil, options={})
|
|
11
|
+
super(app)
|
|
12
|
+
@cache = cache || Proc.new{}.call
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def call(env)
|
|
16
|
+
if env[:method] == :get
|
|
17
|
+
# TODO choose cache method
|
|
18
|
+
# file_store
|
|
19
|
+
# mem_cache_store
|
|
20
|
+
# memory_store
|
|
21
|
+
else
|
|
22
|
+
@app.call(env)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def cache_key
|
|
27
|
+
url = env[:url].dup
|
|
28
|
+
if url.query
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end # Request
|
|
33
|
+
end # Github
|