Class: Hub::GitHubAPI

Inherits:
Object
  • Object
show all
Includes:
GistAuth, HttpMethods, OAuth, Verbose
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.expand_path(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, Verbose Classes: Configuration, FileStore

Constant Summary

Constants included from Verbose

Verbose::DUMP_HEADERS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Verbose

#dump_body, #dump_headers, #dump_request_info, #dump_response_info, #finalize_request, #perform_request, #verbose_puts

Methods included from GistAuth

#apply_authentication

Methods included from OAuth

#apply_authentication, #local_hostname, #local_user, #obtain_oauth_token

Methods included from HttpMethods

#apply_authentication, #byte_size, #configure_connection, #create_connection, #finalize_request, #get, #get_all, #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
31
# File 'lib/hub/github_api.rb', line 27

def initialize config, options
  @config = config
  @oauth_app_url = options.fetch(:app_url)
  @verbose = options.fetch(:verbose, false)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



18
19
20
# File 'lib/hub/github_api.rb', line 18

def config
  @config
end

#oauth_app_urlObject (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



43
44
45
46
# File 'lib/hub/github_api.rb', line 43

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



114
115
116
117
118
119
120
121
# File 'lib/hub/github_api.rb', line 114

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.



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/hub/github_api.rb', line 136

def create_pullrequest options
  project = options.fetch(:project)
  params = {
    :base => options.fetch(:base),
    :head => options.fetch(:head)
  }

  if options[:issue]
    params[:issue] = options[:issue]
  else
    params[:title] = options[:title] if options[:title]
    params[:body]  = options[:body]  if options[: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.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/hub/github_api.rb', line 80

def create_repo project, options = {}
  is_org = project.owner.downcase != username_via_auth_dance(project.host).downcase
  params = { :name => project.name, :private => !!options[:private] }
  params[:description] = options[:description] if options[:description]
  params[:homepage]    = options[:homepage]    if options[: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.



73
74
75
76
77
# File 'lib/hub/github_api.rb', line 73

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



124
125
126
127
128
129
130
131
132
133
# File 'lib/hub/github_api.rb', line 124

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.



96
97
98
99
100
101
# File 'lib/hub/github_api.rb', line 96

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



104
105
106
107
108
109
110
111
# File 'lib/hub/github_api.rb', line 104

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.

Returns:

  • (Boolean)


68
69
70
# File 'lib/hub/github_api.rb', line 68

def repo_exists? project
  repo_info(project).success?
end

#repo_info(project) ⇒ Object

Public: Fetch data for a specific repo.



62
63
64
65
# File 'lib/hub/github_api.rb', line 62

def repo_info project
  get "https://%s/repos/%s/%s" %
    [api_host(project.host), project.owner, project.name]
end

#statuses(project, sha) ⇒ Object



157
158
159
160
161
162
163
# File 'lib/hub/github_api.rb', line 157

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



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/hub/github_api.rb', line 48

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

#verbose?Boolean

Returns:

  • (Boolean)


33
# File 'lib/hub/github_api.rb', line 33

def verbose?() @verbose end