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

.make_objectified_hash(body) ⇒ Object

Parses the response body.



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

def self.make_objectified_hash(body)
  begin
    response = MultiJson.respond_to?(:adapter) ? MultiJson.load(body) : MultiJson.decode(body)
  rescue MultiJson::DecodeError
    raise Error::Parsing.new "Couldn't parse a response body"
  end

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

Instance Method Details

#delete(path) ⇒ Object



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

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

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



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

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

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



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

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

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



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

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

#set_request_defaults(endpoint, private_token) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/gitlab/request.rb', line 44

def set_request_defaults(endpoint, private_token)
  raise Error::MissingCredentials.new("Please set an endpoint") unless endpoint
  raise Error::MissingCredentials.new("Please set a private_token") unless private_token

  self.class.base_uri endpoint
  self.class.default_params :private_token => private_token
end

#validate_response(response) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/gitlab/request.rb', line 52

def validate_response(response)
  case response.code
  when 400
    raise Error::BadRequest.new "Server responsed with code #{response.code}"
  when 401
    raise Error::Unauthorized.new "Server responsed with code #{response.code}"
  when 403
    raise Error::Forbidden.new "Server responsed with code #{response.code}"
  when 404
    raise Error::NotFound.new "Server responsed with code #{response.code}"
  when 500
    raise Error::InternalServerError.new "Server responsed with code #{response.code}"
  when 502
    raise Error::BadGateway.new "Server responsed with code #{response.code}"
  when 503
    raise Error::ServiceUnavailable.new "Server responsed with code #{response.code}"
  end

  response.parsed_response
end