Class: Geet::Github::ApiInterface

Inherits:
Object
  • Object
show all
Defined in:
lib/geet/github/api_interface.rb

Constant Summary collapse

API_AUTH_USER =

We don’t need the login, as the API key uniquely identifies the user

''
API_BASE_URL =
'https://api.github.com'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_token, repo_path: nil, upstream: nil) ⇒ ApiInterface

repo_path: optional for operations that don’t require a repository, eg. gist creation. upstream: boolean; makes sense only when :repo_path is set.



19
20
21
22
23
# File 'lib/geet/github/api_interface.rb', line 19

def initialize(api_token, repo_path: nil, upstream: nil)
  @api_token = api_token
  @repository_path = repo_path
  @upstream = upstream
end

Instance Attribute Details

#repository_pathObject (readonly)

Returns the value of attribute repository_path.



14
15
16
# File 'lib/geet/github/api_interface.rb', line 14

def repository_path
  @repository_path
end

Instance Method Details

#send_request(api_path, params: nil, data: nil, multipage: false, http_method: nil) ⇒ Object

Send a request.

Returns the parsed response, or an Array, in case of multipage. Where no body is present in the response, nil is returned.

params:

:api_path:    api path, will be appended to the API URL.
              for root path, prepend a `/`:
              - use `/gists` for `https://api.github.com/gists`
              when owner/project/repos is included, don't prepend `/`:
              - use `issues` for `https://api.github.com/myowner/myproject/repos/issues`
:params:      (Hash)
:data:        (Hash) if present, will generate a POST request, otherwise, a GET
:multipage:   set true for paged Github responses (eg. issues); it will make the method
              return an array, with the concatenated (parsed) responses
:http_method: symbol format of the method (:get, :patch, :post, :put and :delete)
              :get and :post are automatically inferred by the present of :data; the other
              cases must be specified.


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/geet/github/api_interface.rb', line 48

def send_request(api_path, params: nil, data: nil, multipage: false, http_method: nil)
  address = api_url(api_path)
  # filled only on :multipage
  parsed_responses = []

  loop do
    response = send_http_request(address, params: params, data: data, http_method: http_method)

    parsed_response = JSON.parse(response.body) if response.body

    if error?(response)
      error_message = decode_and_format_error(parsed_response)
      raise Geet::Shared::HttpError.new(error_message, response.code)
    end

    return parsed_response if !multipage

    parsed_responses.concat(parsed_response)

    address = link_next_page(response.to_hash)

    return parsed_responses if address.nil?
  end
end

#upstream?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/geet/github/api_interface.rb', line 25

def upstream?
  @upstream
end