Class: Computron::Client::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/computron/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) {|_self| ... } ⇒ Response

Returns a new instance of Response.

Yields:

  • (_self)

Yield Parameters:



12
13
14
15
# File 'lib/computron/client.rb', line 12

def initialize(opts={})
  @status = {}
  yield self if block_given?
end

Instance Attribute Details

#statusObject (readonly)

Returns the value of attribute status.



10
11
12
# File 'lib/computron/client.rb', line 10

def status
  @status
end

Class Method Details

.http_status_codesObject

Returns a hash containing the response code to the status like => ok.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/computron/client.rb', line 35

def self.http_status_codes
  @http_status_codes ||= %(100 continue
    101 switching_protocols
    200 ok
    201 created
    202 accepted
    203 non_authoritive_information
    204 no_content
    205 reset_content
    206 partial_content
    300 multiple_choices
    301 moved_permanently
    302 found
    303 see_other
    304 not_modified
    305 use_proxy
    307 temporary_redirect
    400 bad_request
    401 unauthorized
    402 payment_required
    403 forbidden
    404 not_found
    405 method_not_allowed
    406 not_acceptable
    407 proxy_authentication_required
    408 request_timeout
    409 conflict
    410 gone
    411 length_required
    412 precondition_failed
    413 request_entity_too_large
    414 request_uri_too_long
    415 unsupported_media_type
    416 request_range_not_satisfiable
    417 expectation_failed
    500 internal_server_error
    501 not_implemented
    502 bad_gateway
    503 service_unavailable
    504 gateway_timeout
    505 http_version_not_supported).inject({}) do |hash, line|
      code, status = line.split(' ')
      hash[code.to_i] = status
      hash
    end
end

Instance Method Details

#call(http) ⇒ Object



17
18
19
20
21
22
# File 'lib/computron/client.rb', line 17

def call(http)
  if callback = status[http.response_header.status]
    callback.call(decode_response(http))
  end
  @finished.call  if @finished
end

#code(code, &block) ⇒ Object

A shortcut for specifying a callback on an HTTP code if you don’t know the status name.



25
26
27
# File 'lib/computron/client.rb', line 25

def code(code, &block)
  status[code] = block
end

#finished(&block) ⇒ Object

Runs after every HTTP request



30
31
32
# File 'lib/computron/client.rb', line 30

def finished(&block)
  @finished = block
end