Class: Hub::GitHubAPI

Inherits:
Object
  • Object
show all
Includes:
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.expand_path(config_file)
  file_config = GitHubAPI::Configuration.new file_store
  GitHubAPI.new file_config, :app_url => 'http://defunkt.io/hub/'
end

Defined Under Namespace

Modules: Exceptions, HttpMethods, OAuth Classes: Configuration, FileStore

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from OAuth

#apply_authentication, #obtain_oauth_token

Methods included from HttpMethods

#apply_authentication, #byte_size, #configure_connection, #create_connection, #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)

    • api_token(host, user)

    • password(host, user)

    • oauth_token(host, user)



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

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

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



21
22
23
# File 'lib/hub/github_api.rb', line 21

def config
  @config
end

#oauth_app_urlObject (readonly)

Returns the value of attribute oauth_app_url.



21
22
23
# File 'lib/hub/github_api.rb', line 21

def oauth_app_url
  @oauth_app_url
end

Instance Method Details

#api_host(host) ⇒ Object



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

def api_host host
  host = host.downcase
  'github.com' == host ? 'api.github.com' : host
end

#create_pullrequest(options) ⇒ Object

Returns parsed data from the new pull request.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/hub/github_api.rb', line 92

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.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/hub/github_api.rb', line 68

def create_repo project, options = {}
  is_org = project.owner.downcase != config.username(api_host(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.



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

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

#pullrequest_info(project, pull_id) ⇒ Object

Public: Fetch info about a pull request.



84
85
86
87
88
89
# File 'lib/hub/github_api.rb', line 84

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

#repo_exists?(project) ⇒ Boolean

Public: Determine whether a specific repo exists.

Returns:

  • (Boolean)


56
57
58
# File 'lib/hub/github_api.rb', line 56

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

#repo_info(project) ⇒ Object

Public: Fetch data for a specific repo.



50
51
52
53
# File 'lib/hub/github_api.rb', line 50

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