Class: Geet::Gitlab::ApiInterface

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

Constant Summary collapse

API_BASE_URL =
'https://gitlab.com/api/v4'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

repo_path: “path/namespace”; required for the current GitLab operations. upstream: boolean; required for the current GitLab operations.



18
19
20
21
22
# File 'lib/geet/gitlab/api_interface.rb', line 18

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

Instance Attribute Details

#repository_pathObject (readonly)

Returns the value of attribute repository_path.



13
14
15
# File 'lib/geet/gitlab/api_interface.rb', line 13

def repository_path
  @repository_path
end

Instance Method Details

#path_with_namespace(encoded: false) ⇒ Object



28
29
30
# File 'lib/geet/gitlab/api_interface.rb', line 28

def path_with_namespace(encoded: false)
  encoded ? CGI.escape(@path_with_namespace) : @path_with_namespace
end

#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.


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/geet/gitlab/api_interface.rb', line 51

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)
      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?

    # Gitlab's next link address already includes all the params, so we remove
    # the passed ones (if there's any).
    params = nil
  end
end

#upstream?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/geet/gitlab/api_interface.rb', line 24

def upstream?
  @upstream
end