class GsaAuctions::Client

Public Class Methods

new(params={}) click to toggle source
# File lib/gsa_auctions.rb, line 35
def initialize(params={})
  @api_key = params[:api_key] if params.has_key?(:api_key)
end

Public Instance Methods

get(path, options={}) click to toggle source
# File lib/gsa_auctions.rb, line 46
def get(path, options={})
  query = GsaAuctions::Util.hash_to_query(options)
  url = "#{path}?#{query}"
  response = Curl.get(url)
  
  handle_response(response, options[:format])
end
get_api(options={}) click to toggle source
# File lib/gsa_auctions.rb, line 39
def get_api(options={})
  options[:api_key] = @api_key
  options[:format]  = GsaAuctions::Protocol::DEFAULT_FORMAT unless options[:format].is_a?(String)
  
  get(GsaAuctions::Protocol.api_url, options)
end

Private Instance Methods

handle_response(response, format) click to toggle source
# File lib/gsa_auctions.rb, line 67
def handle_response(response, format)
  raise GsaAuction::Error.new(response.status) if response.response_code != 200
  parsed_response = parse(response.body_str, format)
          
  GsaAuctions::Response.new(parsed_response)
end
parse(json_or_xml_string, format) click to toggle source
# File lib/gsa_auctions.rb, line 56
def parse(json_or_xml_string, format)
  formats = GsaAuctions::Protocol::VALID_FORMATS
  if !formats.include?(format)
    message = "Invalid format given (#{format}). Please choose from: #{formats}"
    raise GsaAuctions::Error.new(message)
  end
  
  return Hash.from_xml(json_or_xml_string) if format == 'xml'
  return JSON.parse(json_or_xml_string)    if format == 'json'
end