Class: Gitlab::Request
- Inherits:
-
Object
- Object
- Gitlab::Request
- Includes:
- HTTParty
- Defined in:
- lib/gitlab/request.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#private_token ⇒ Object
Returns the value of attribute private_token.
Class Method Summary collapse
-
.decode(response) ⇒ Object
Decodes a JSON response into Ruby object.
-
.parse(body) ⇒ Object
Converts the response body to an ObjectifiedHash.
Instance Method Summary collapse
- #delete(path, options = {}) ⇒ Object
- #get(path, options = {}) ⇒ Object
- #post(path, options = {}) ⇒ Object
- #put(path, options = {}) ⇒ Object
-
#set_request_defaults(endpoint, private_token, sudo = nil) ⇒ Object
Sets a base_uri and default_params for requests.
-
#validate(response) ⇒ Object
Checks the response code for common errors.
Instance Attribute Details
#private_token ⇒ Object
Returns the value of attribute private_token.
12 13 14 |
# File 'lib/gitlab/request.rb', line 12 def private_token @private_token end |
Class Method Details
.decode(response) ⇒ Object
Decodes a JSON response into Ruby object.
28 29 30 31 32 33 34 |
# File 'lib/gitlab/request.rb', line 28 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.
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/gitlab/request.rb', line 15 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, options = {}) ⇒ Object
51 52 53 54 |
# File 'lib/gitlab/request.rb', line 51 def delete(path, ={}) set_private_token_header() validate self.class.delete(path, ) end |
#get(path, options = {}) ⇒ Object
36 37 38 39 |
# File 'lib/gitlab/request.rb', line 36 def get(path, ={}) set_private_token_header() validate self.class.get(path, ) end |
#post(path, options = {}) ⇒ Object
41 42 43 44 |
# File 'lib/gitlab/request.rb', line 41 def post(path, ={}) set_private_token_header(, path) validate self.class.post(path, ) end |
#put(path, options = {}) ⇒ Object
46 47 48 49 |
# File 'lib/gitlab/request.rb', line 46 def put(path, ={}) set_private_token_header() validate self.class.put(path, ) end |
#set_request_defaults(endpoint, private_token, sudo = nil) ⇒ Object
Sets a base_uri and default_params for requests.
76 77 78 79 80 81 82 83 |
# File 'lib/gitlab/request.rb', line 76 def set_request_defaults(endpoint, private_token, sudo=nil) raise Error::MissingCredentials.new("Please set an endpoint to API") unless endpoint @private_token = private_token self.class.base_uri 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.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/gitlab/request.rb', line 58 def validate(response) case response.code when 400; raise Error::BadRequest.new (response) when 401; raise Error::Unauthorized.new (response) when 403; raise Error::Forbidden.new (response) when 404; raise Error::NotFound.new (response) when 405; raise Error::MethodNotAllowed.new (response) when 409; raise Error::Conflict.new (response) when 500; raise Error::InternalServerError.new (response) when 502; raise Error::BadGateway.new (response) when 503; raise Error::ServiceUnavailable.new (response) end response.parsed_response end |