Class: Gitlab::Request

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/gitlab/request.rb

Direct Known Subclasses

API

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#endpointObject

Returns the value of attribute endpoint.



15
16
17
# File 'lib/gitlab/request.rb', line 15

def endpoint
  @endpoint
end

#private_tokenObject

Returns the value of attribute private_token.



15
16
17
# File 'lib/gitlab/request.rb', line 15

def private_token
  @private_token
end

Class Method Details

.decode(response) ⇒ Object

Decodes a JSON response into Ruby object.



35
36
37
38
39
# File 'lib/gitlab/request.rb', line 35

def self.decode(response)
  response ? JSON.load(response) : {}
rescue JSON::ParserError
  raise Error::Parsing, 'The response is not a valid JSON'
end

.parse(body) ⇒ Object

Converts the response body to an ObjectifiedHash.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/gitlab/request.rb', line 18

def self.parse(body)
  body = decode(body)

  if body.is_a? Hash
    ObjectifiedHash.new body
  elsif body.is_a? Array
    PaginatedResponse.new(body.collect! { |e| ObjectifiedHash.new(e) })
  elsif body
    true
  elsif !body
    false
  else
    raise Error::Parsing, "Couldn't parse a response body"
  end
end

Instance Method Details

#request_defaults(sudo = nil) ⇒ Object

Sets a base_uri and default_params for requests.

Raises:



81
82
83
84
85
86
# File 'lib/gitlab/request.rb', line 81

def request_defaults(sudo = nil)
  raise Error::MissingCredentials, 'Please set an endpoint to API' unless endpoint

  self.class.default_params sudo: sudo
  self.class.default_params.delete(:sudo) if sudo.nil?
end

#validate(response) ⇒ Object

Checks the response code for common errors. Returns parsed response for successful requests.

Raises:

  • (error_klass)


69
70
71
72
73
74
75
76
77
# File 'lib/gitlab/request.rb', line 69

def validate(response)
  error_klass = Error::STATUS_MAPPINGS[response.code]
  raise error_klass, response if error_klass

  parsed = response.parsed_response
  parsed.client = self if parsed.respond_to?(:client=)
  parsed.parse_headers!(response.headers) if parsed.respond_to?(:parse_headers!)
  parsed
end