lib/shaco_client/request.rb
# Calls the API via HTTParty and handles errors
# @param url [String] the url to call
# @param verb [Symbol] HTTP verb to use. Defaults to :get
# @param body [Hash] Body for POST request
# @param options [Hash] Options passed to HTTParty
# @return [String] raw response of the call
Calls the API via HTTParty and handles errors
Calls the API via HTTParty and handles errors
def perform url, verb = :get, body = nil, options = {}
      options[:headers] ||= {}
      options[:headers].merge!({
        "Content-Type" => "application/json",
        "Accept"       => "application/json"
      })
      if [:post, :put].include?(verb)
        options[:body] = body.to_json if body
      end
      response = self.class.send(verb, url, options)
      if response.respond_to?(:code) && !(200...300).include?(response.code)
        raise NotFound.new("404 Not Found") if response.not_found?
        raise TooManyRequests.new('429 Rate limit exceeded') if response.code == 429
        raise InvalidAPIResponse.new(url, response)
      end
      if response.respond_to?(:parsed_response)
        response.parsed_response
      else
        response
      end
    end
def perform url , verb = :get , body = nil , options = { } options [ :headers ] ||= { } options [ :headers ] . merge! ( { "Content-Type" => "application/json" , "Accept" => "application/json" } ) if [ :post , :put ] . include? ( verb ) options [ :body ] = body . to_json if body end response = self . class . send ( verb , url , options ) if response . respond_to? ( :code ) && ! ( 200 ... 300 ) . include? ( response . code ) raise NotFound . new ( "404 Not Found" ) if response . not_found? raise TooManyRequests . new ( '429 Rate limit exceeded' ) if response . code == 429 raise InvalidAPIResponse . new ( url , response ) end if response . respond_to? ( :parsed_response ) response . parsed_response else response end end
Calls the API via HTTParty and handles errors
Calls the API via HTTParty and handles errors
raw response of the call
raw response of the call
String
url
the url to call
the url to call
verb
HTTP verb to use. Defaults to :get
HTTP verb to use . Defaults to : get
body
Body for POST request
Body for POST request
options
Options passed to HTTParty
Options passed to HTTParty