Class: GithubPivotalFlow::GitHubAPI

Inherits:
Object
  • Object
show all
Includes:
HttpMethods, OAuth
Defined in:
lib/github_pivotal_flow/github_api.rb

Overview

Client for the GitHub v3 API.

Defined Under Namespace

Modules: Exceptions, HttpMethods, OAuth

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, #finalize_request, #get, #perform_request, #post, #post_form, #request_uri

Constructor Details

#initialize(config, options) ⇒ GitHubAPI

Returns a new instance of GitHubAPI.



6
7
8
9
# File 'lib/github_pivotal_flow/github_api.rb', line 6

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.



4
5
6
# File 'lib/github_pivotal_flow/github_api.rb', line 4

def config
  @config
end

#oauth_app_urlObject (readonly)

Returns the value of attribute oauth_app_url.



4
5
6
# File 'lib/github_pivotal_flow/github_api.rb', line 4

def oauth_app_url
  @oauth_app_url
end

Instance Method Details

#api_host(host) ⇒ Object



19
20
21
22
# File 'lib/github_pivotal_flow/github_api.rb', line 19

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.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/github_pivotal_flow/github_api.rb', line 47

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

#repo_exists?(project) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/github_pivotal_flow/github_api.rb', line 42

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

#repo_info(project) ⇒ Object



37
38
39
40
# File 'lib/github_pivotal_flow/github_api.rb', line 37

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

#statuses(project, sha) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/github_pivotal_flow/github_api.rb', line 68

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



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/github_pivotal_flow/github_api.rb', line 24

def username_via_auth_dance host
  host = api_host(host)
  config.github_username(host) do
    if block_given?
      yield
    else
      res = get("https://%s/user" % host)
      res.error! unless res.success?
      config.github_username = res.data['login']
    end
  end
end