lib/gitlab/request.rb in gitlab-2.0.0 vs lib/gitlab/request.rb in gitlab-2.1.0

- old
+ new

@@ -2,72 +2,79 @@ module Gitlab # @private class Request include HTTParty - format :json + format :json headers 'Accept' => 'application/json' - parser Proc.new {|body| make_objectified_hash(body)} + parser Proc.new {|body| parse(body)} - # Parses the response body. - 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 + # Converts the response body to an ObjectifiedHash. + def self.parse(body) + body = decode(body) - if response.is_a? Hash - ObjectifiedHash.new response - elsif response.is_a? Array - response.collect! {|e| ObjectifiedHash.new(e)} + 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 + # Decodes a JSON response into Ruby object. + def self.decode(response) + begin + if MultiJson.respond_to?(:adapter) + MultiJson.load response + else + MultiJson.decode response + end + rescue MultiJson::DecodeError + raise Error::Parsing.new "The response is not a valid JSON" + end + end + def get(path, options={}) - validate_response self.class.get(path, options) + validate self.class.get(path, options) end def post(path, options={}) - validate_response self.class.post(path, options) + validate self.class.post(path, options) end def put(path, options={}) - validate_response self.class.put(path, options) + validate self.class.put(path, options) end def delete(path) - validate_response self.class.delete(path) + validate self.class.delete(path) end - 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 - - def validate_response(response) + # Checks the response code for common errors. + # Returns parsed response for successful requests. + def validate(response) + message = "Server responsed with code #{response.code}" 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}" + when 400; raise Error::BadRequest.new message + when 401; raise Error::Unauthorized.new message + when 403; raise Error::Forbidden.new message + when 404; raise Error::NotFound.new message + when 500; raise Error::InternalServerError.new message + when 502; raise Error::BadGateway.new message + when 503; raise Error::ServiceUnavailable.new message end response.parsed_response + end + + # Sets a base_uri and private_token parameter for requests. + # @raise [Error::MissingCredentials] if endpoint or private_token not set. + def set_request_defaults(endpoint, private_token) + 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 end end end