Class: Hub::GitHubAPI
- Inherits:
-
Object
- Object
- Hub::GitHubAPI
- Includes:
- GistAuth, HttpMethods, OAuth
- Defined in:
- lib/hub/github_api.rb
Overview
Client for the GitHub v3 API.
First time around, user gets prompted for username/password in the shell. Then this information is exchanged for an OAuth token which is saved in a file.
Examples
@api_client ||= begin
config_file = ENV['HUB_CONFIG'] || '~/.config/hub'
file_store = GitHubAPI::FileStore.new File.(config_file)
file_config = GitHubAPI::Configuration.new file_store
GitHubAPI.new file_config, :app_url => 'http://hub.github.com/'
end
Defined Under Namespace
Modules: Exceptions, GistAuth, HttpMethods, OAuth Classes: Configuration, FileStore
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#oauth_app_url ⇒ Object
readonly
Returns the value of attribute oauth_app_url.
Instance Method Summary collapse
- #api_host(host) ⇒ Object
-
#commit_patch(project, sha) ⇒ Object
Public: Fetch the patch from a commit.
-
#create_pullrequest(options) ⇒ Object
Returns parsed data from the new pull request.
-
#create_repo(project, options = {}) ⇒ Object
Public: Create a new project.
-
#fork_repo(project) ⇒ Object
Public: Fork the specified repo.
-
#gist_raw(gist_id) ⇒ Object
Public: Fetch the first raw blob from a gist.
-
#initialize(config, options) ⇒ GitHubAPI
constructor
Public: Create a new API client instance.
-
#pullrequest_info(project, pull_id) ⇒ Object
Public: Fetch info about a pull request.
-
#pullrequest_patch(project, pull_id) ⇒ Object
Public: Fetch a pull request’s patch.
-
#repo_exists?(project) ⇒ Boolean
Public: Determine whether a specific repo exists.
-
#repo_info(project) ⇒ Object
Public: Fetch data for a specific repo.
- #statuses(project, sha) ⇒ Object
- #username_via_auth_dance(host) ⇒ Object
Methods included from GistAuth
Methods included from OAuth
#apply_authentication, #obtain_oauth_token
Methods included from HttpMethods
#apply_authentication, #byte_size, #configure_connection, #create_connection, #finalize_request, #get, #perform_request, #post, #post_form, #request_uri
Constructor Details
#initialize(config, options) ⇒ GitHubAPI
Public: Create a new API client instance
Options:
-
config: an object that implements:
-
username(host)
-
password(host, user)
-
oauth_token(host, user)
-
27 28 29 30 |
# File 'lib/hub/github_api.rb', line 27 def initialize config, @config = config @oauth_app_url = .fetch(:app_url) end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
18 19 20 |
# File 'lib/hub/github_api.rb', line 18 def config @config end |
#oauth_app_url ⇒ Object (readonly)
Returns the value of attribute oauth_app_url.
18 19 20 |
# File 'lib/hub/github_api.rb', line 18 def oauth_app_url @oauth_app_url end |
Instance Method Details
#api_host(host) ⇒ Object
40 41 42 43 |
# File 'lib/hub/github_api.rb', line 40 def api_host host host = host.downcase 'github.com' == host ? 'api.github.com' : host end |
#commit_patch(project, sha) ⇒ Object
Public: Fetch the patch from a commit
111 112 113 114 115 116 117 118 |
# File 'lib/hub/github_api.rb', line 111 def commit_patch project, sha res = get "https://%s/repos/%s/%s/commits/%s" % [api_host(project.host), project.owner, project.name, sha] do |req| req["Accept"] = "application/vnd.github.v3.patch" end res.error! unless res.success? res.body end |
#create_pullrequest(options) ⇒ Object
Returns parsed data from the new pull request.
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/hub/github_api.rb', line 133 def create_pullrequest project = .fetch(:project) params = { :base => .fetch(:base), :head => .fetch(:head) } if [:issue] params[:issue] = [:issue] else params[:title] = [:title] if [:title] params[:body] = [:body] if [:body] end res = post "https://%s/repos/%s/%s/pulls" % [api_host(project.host), project.owner, project.name], params res.error! unless res.success? res.data end |
#create_repo(project, options = {}) ⇒ Object
Public: Create a new project.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/hub/github_api.rb', line 77 def create_repo project, = {} is_org = project.owner.downcase != username_via_auth_dance(project.host).downcase params = { :name => project.name, :private => !![:private] } params[:description] = [:description] if [:description] params[:homepage] = [:homepage] if [:homepage] if is_org res = post "https://%s/orgs/%s/repos" % [api_host(project.host), project.owner], params else res = post "https://%s/user/repos" % api_host(project.host), params end res.error! unless res.success? res.data end |
#fork_repo(project) ⇒ Object
Public: Fork the specified repo.
70 71 72 73 74 |
# File 'lib/hub/github_api.rb', line 70 def fork_repo project res = post "https://%s/repos/%s/%s/forks" % [api_host(project.host), project.owner, project.name] res.error! unless res.success? end |
#gist_raw(gist_id) ⇒ Object
Public: Fetch the first raw blob from a gist
121 122 123 124 125 126 127 128 129 130 |
# File 'lib/hub/github_api.rb', line 121 def gist_raw gist_id res = get("https://%s/gists/%s" % [api_host('github.com'), gist_id]) res.error! unless res.success? raw_url = res.data['files'].values.first['raw_url'] res = get(raw_url) do |req| req['Accept'] = 'text/plain' end res.error! unless res.success? res.body end |
#pullrequest_info(project, pull_id) ⇒ Object
Public: Fetch info about a pull request.
93 94 95 96 97 98 |
# File 'lib/hub/github_api.rb', line 93 def pullrequest_info project, pull_id res = get "https://%s/repos/%s/%s/pulls/%d" % [api_host(project.host), project.owner, project.name, pull_id] res.error! unless res.success? res.data end |
#pullrequest_patch(project, pull_id) ⇒ Object
Public: Fetch a pull request’s patch
101 102 103 104 105 106 107 108 |
# File 'lib/hub/github_api.rb', line 101 def pullrequest_patch project, pull_id res = get "https://%s/repos/%s/%s/pulls/%d" % [api_host(project.host), project.owner, project.name, pull_id] do |req| req["Accept"] = "application/vnd.github.v3.patch" end res.error! unless res.success? res.body end |
#repo_exists?(project) ⇒ Boolean
Public: Determine whether a specific repo exists.
65 66 67 |
# File 'lib/hub/github_api.rb', line 65 def repo_exists? project repo_info(project).success? end |
#repo_info(project) ⇒ Object
Public: Fetch data for a specific repo.
59 60 61 62 |
# File 'lib/hub/github_api.rb', line 59 def repo_info project get "https://%s/repos/%s/%s" % [api_host(project.host), project.owner, project.name] end |
#statuses(project, sha) ⇒ Object
154 155 156 157 158 159 160 |
# File 'lib/hub/github_api.rb', line 154 def statuses project, sha res = get "https://%s/repos/%s/%s/statuses/%s" % [api_host(project.host), project.owner, project.name, sha] res.error! unless res.success? res.data end |
#username_via_auth_dance(host) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/hub/github_api.rb', line 45 def username_via_auth_dance host host = api_host(host) config.username(host) do if block_given? yield else res = get("https://%s/user" % host) res.error! unless res.success? config.value_to_persist(res.data['login']) end end end |