module RestClient

Interface for making REST requests

Constants

REDIRECT_CODES

Public Instance Methods

get(uri, options = { as: :json }) click to toggle source
# File lib/plugins/clients/rest_client.rb, line 8
def get(uri, options = { as: :json })
  parse_raw_response(raw_get(uri), options[:as])
end

Private Instance Methods

parse_raw_response(response, parse_option) click to toggle source

rubocop:disable all

# File lib/plugins/clients/rest_client.rb, line 26
def parse_raw_response(response, parse_option)
  case parse_option
  when :json
    return  JSON.parse response
  when :xml
    return Oga.parse_xml response
  when :html
    return Oga.parse_html response
  when :csv
    return CSV.parse response
  when :text
    return response
  else
    return response
  end
end
raw_get(uri) click to toggle source
# File lib/plugins/clients/rest_client.rb, line 16
def raw_get(uri)
  response  = Net::HTTP.get_response(URI.parse(uri))
  REDIRECT_CODES.include?(response.code) ? raw_get(response.header["location"]) : response.body
rescue URI::InvalidURIError, TypeError => e
  raise(e.class, "'#{uri}' is not a valid URI", caller)
rescue SocketError => e
  raise(e.class, "Can't connect to '#{uri}', check your connected to a network.", caller)
end