Class: GiantClient

Inherits:
Object
  • Object
show all
Defined in:
lib/giant_client.rb,
lib/giant_client/error.rb,
lib/giant_client/version.rb,
lib/giant_client/response.rb,
lib/giant_client/curb_adapter.rb,
lib/giant_client/mock_adapter.rb,
lib/giant_client/mock_request.rb,
lib/giant_client/excon_adapter.rb,
lib/giant_client/patron_adapter.rb,
lib/giant_client/abstract_adapter.rb,
lib/giant_client/net_http_adapter.rb,
lib/giant_client/typhoeus_adapter.rb

Defined Under Namespace

Classes: AbstractAdapter, CurbAdapter, Error, ExconAdapter, MockAdapter, MockRequest, NetHttpAdapter, PatronAdapter, Response, TyphoeusAdapter

Constant Summary collapse

BODYLESS_METHODS =
[:get, :delete, :head]
VERSION =
"0.1.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ GiantClient

Returns a new instance of GiantClient.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 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

Instance Attribute Details

#adapterObject

Returns the value of attribute adapter.



8
9
10
# File 'lib/giant_client.rb', line 8

def adapter
  @adapter
end

#hostObject

Returns the value of attribute host.



7
8
9
# File 'lib/giant_client.rb', line 7

def host
  @host
end

#portObject

Returns the value of attribute port.



7
8
9
# File 'lib/giant_client.rb', line 7

def port
  @port
end

#sslObject

Returns the value of attribute ssl.



7
8
9
# File 'lib/giant_client.rb', line 7

def ssl
  @ssl
end

Instance Method Details

#last_requestObject

for the mock adapter only



73
74
75
76
77
78
79
# 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_responseObject



89
90
91
92
93
94
95
# 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

#requestsObject



81
82
83
84
85
86
87
# 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

#responsesObject



97
98
99
100
101
102
103
# 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