Class: Gitlab::Request

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

Direct Known Subclasses

API

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.decode(response) ⇒ Object

Decodes a JSON response into Ruby object.



25
26
27
28
29
30
31
# File 'lib/gitlab/request.rb', line 25

def self.decode(response)
  begin
    JSON.load response
  rescue JSON::ParserError
    raise Error::Parsing.new "The response is not a valid JSON"
  end
end

.parse(body) ⇒ Object

Converts the response body to an ObjectifiedHash.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/gitlab/request.rb', line 12

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

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

Instance Method Details

#delete(path) ⇒ Object



45
46
47
# File 'lib/gitlab/request.rb', line 45

def delete(path)
  validate self.class.delete(path)
end

#get(path, options = {}) ⇒ Object



33
34
35
# File 'lib/gitlab/request.rb', line 33

def get(path, options={})
  validate self.class.get(path, options)
end

#post(path, options = {}) ⇒ Object



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

def post(path, options={})
  validate self.class.post(path, options)
end

#put(path, options = {}) ⇒ Object



41
42
43
# File 'lib/gitlab/request.rb', line 41

def put(path, options={})
  validate self.class.put(path, options)
end

#set_request_defaults(endpoint, private_token, sudo = nil) ⇒ Object

Sets a base_uri and default_params for requests.

Raises:



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

def set_request_defaults(endpoint, private_token, sudo=nil)
  raise Error::MissingCredentials.new("Please set an endpoint to API") unless endpoint
  raise Error::MissingCredentials.new("Please set a private_token for user") unless private_token

  self.class.base_uri endpoint
  self.class.default_params :private_token => private_token, :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.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/gitlab/request.rb', line 51

def validate(response)
  case response.code
    when 400; raise Error::BadRequest.new error_message(response)
    when 401; raise Error::Unauthorized.new error_message(response)
    when 403; raise Error::Forbidden.new error_message(response)
    when 404; raise Error::NotFound.new error_message(response)
    when 405; raise Error::MethodNotAllowed.new error_message(response)
    when 409; raise Error::Conflict.new error_message(response)
    when 500; raise Error::InternalServerError.new error_message(response)
    when 502; raise Error::BadGateway.new error_message(response)
    when 503; raise Error::ServiceUnavailable.new error_message(response)
  end

  response.parsed_response
end