class Hub::GitHubAPI

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

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

Attributes

config[R]
oauth_app_url[R]

Public Class Methods

new(config, options) click to toggle source

Public: Create a new API client instance

Options:

  • config: an object that implements:

    • username(host)

    • password(host, user)

    • oauth_token(host, user)

# 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

Public Instance Methods

api_host(host) click to toggle source
# 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) click to toggle source

Public: Fetch the patch from a commit

# 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) click to toggle source

Returns parsed data from the new pull request.

# 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 = {}) click to toggle source

Public: Create a new project.

# 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) click to toggle source

Public: Fork the specified repo.

# 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) click to toggle source

Public: Fetch the first raw blob from a gist

# 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) click to toggle source

Public: Fetch info about a pull request.

# 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) click to toggle source

Public: Fetch a pull request's patch

# 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) click to toggle source

Public: Determine whether a specific repo exists.

# File lib/hub/github_api.rb, line 68
def repo_exists? project
  repo_info(project).success?
end
repo_info(project) click to toggle source

Public: Fetch data for a specific repo.

# 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) click to toggle source
# 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) { || ... } click to toggle source
# 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?() click to toggle source
# File lib/hub/github_api.rb, line 33
def verbose?() @verbose end