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 Method Summary collapse

Constructor Details

#initialize(api_token, repository_path, upstream) ⇒ ApiInterface

Returns a new instance of ApiInterface.



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

def initialize(api_token, repository_path, upstream)
  @api_token = api_token
  @repository_path = repository_path
  @upstream = upstream
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.

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: :get, :patch, :post and :put are accepted, but only :patch/:put are meaningful,
              since the others are automatically inferred by :data.


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/geet/github/api_interface.rb', line 41

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 error?(response)
      formatted_error = decode_and_format_error(parsed_response)
      raise(formatted_error)
    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)


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

def upstream?
  @upstream
end