class GiantClient

Constants

BODYLESS_METHODS
VERSION

Attributes

adapter[R]
host[RW]
port[RW]
ssl[RW]

Public Class Methods

new(*args) click to toggle source
# File lib/giant_client.rb, line 10
def initialize(*args)

  unless args.length.between?(1, 2)
    raise ArgumentError, "wrong number of arguments (#{args.length} for 1..2)";
  end

  opts = Hash === args.last ? args.last : { :adapter => args.last }
  if String === args.first
    opts[:adapter] = args.first
  end

  @host = opts[:host]
  @ssl = !!opts[:ssl]
  default_port = @ssl ? 443 : 80
  @port = opts[:port] || default_port
  @timeout = opts[:timeout] || 2

  @default_opts = {
    :host => @host,
    :ssl => @ssl,
    :port => @port,
    :path => '/',
    :query => {},
    :headers => {},
    :body => "",
    :timeout => 30
  }

  # default timeouts
  # patron:      5
  # net/http:    60
  # curb:        none
  # excon:       60
  # typhoeus:

  self.adapter = opts[:adapter] || :net_http
  @client = @adapter.new

end

Public Instance Methods

adapter=(new_adapter) click to toggle source
# File lib/giant_client.rb, line 50
def adapter=(new_adapter)
  require "giant_client/#{new_adapter}_adapter"
  normalized = new_adapter.to_s.split('_').map{ |s| s.capitalize }.join
  @adapter = GiantClient.const_get("#{normalized}Adapter")
  @client = @adapter.new
end
last_request() click to toggle source

for the mock adapter only

# File lib/giant_client.rb, line 73
def last_request
  if MockAdapter === @client
    @client.last_request
  else
    raise ArgumentError, "wrong number of arguments (#{args.length} for 1..2)";
  end
end
last_response() click to toggle source
# File lib/giant_client.rb, line 89
def last_response
  if MockAdapter === @client
    @client.last_response
  else
    raise ArgumentError, "wrong number of arguments (#{args.length} for 1..2)";
  end
end
method_missing(method, *args) click to toggle source
# File lib/giant_client.rb, line 57
def method_missing(method, *args)

  unless args.length.between?(1, 2)
    raise ArgumentError, "wrong number of arguments (#{args.length} for 1..2)";
  end

  opts = Hash === args.last ? args.last : { :path => args.last }
  if String === args.first
    opts[:path] = args.first
  end

  opts = @default_opts.merge(opts)
  @client.__send__(method, opts)
end
requests() click to toggle source
# File lib/giant_client.rb, line 81
def requests
  if MockAdapter === @client
    @client.requests
  else
    raise ArgumentError, "wrong number of arguments (#{args.length} for 1..2)";
  end
end
responses() click to toggle source
# File lib/giant_client.rb, line 97
def responses
  if MockAdapter === @client
    @client.responses
  else
    raise ArgumentError, "wrong number of arguments (#{args.length} for 1..2)";
  end
end