Class: CurbFu::Response::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/curb-fu/response.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status, headers, body) ⇒ Base

Returns a new instance of Base.



6
7
8
9
10
11
# File 'lib/curb-fu/response.rb', line 6

def initialize(status, headers, body)
  @status = status
  set_response_type(status)
  @body = body
  @headers = headers.is_a?(String) ? parse_headers(headers) : headers
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



4
5
6
# File 'lib/curb-fu/response.rb', line 4

def body
  @body
end

#headersObject

Returns the value of attribute headers.



4
5
6
# File 'lib/curb-fu/response.rb', line 4

def headers
  @headers
end

#statusObject

Returns the value of attribute status.



4
5
6
# File 'lib/curb-fu/response.rb', line 4

def status
  @status
end

Class Method Details

.from_curb_response(curb) ⇒ Object



147
148
149
150
# File 'lib/curb-fu/response.rb', line 147

def from_curb_response(curb)
  response = self.new(curb.response_code, curb.header_str, curb.body_str)
  response
end

.from_hash(hash) ⇒ Object



152
153
154
155
# File 'lib/curb-fu/response.rb', line 152

def from_hash(hash)
  return nil if hash.nil?
  self.new(hash[:status], hash[:headers], hash[:body])
end

.from_rack_response(rack) ⇒ Object

Raises:

  • (ArgumentError)


142
143
144
145
# File 'lib/curb-fu/response.rb', line 142

def from_rack_response(rack)
  raise ArgumentError.new("Rack response may not be nil") if rack.nil?
  response = self.new(rack.status, rack.headers, rack.body)
end

Instance Method Details

#[](key) ⇒ Object



70
71
72
# File 'lib/curb-fu/response.rb', line 70

def [](key)
  get_fields(key).last
end

#client_fail?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/curb-fu/response.rb', line 29

def client_fail?
  self.is_a?(CurbFu::Response::ClientError)
end

#content_lengthObject



50
51
52
53
54
# File 'lib/curb-fu/response.rb', line 50

def content_length
  if ( header_value = self['Content-Length'] )
    header_value.to_i
  end
end

#content_typeObject



56
57
58
59
60
# File 'lib/curb-fu/response.rb', line 56

def content_type
  if ( header_value = self['Content-Type'] )
    header_value.split(';').first
  end
end

#failure?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/curb-fu/response.rb', line 21

def failure?
  !(success? || redirect?)
end

#get_fields(key) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/curb-fu/response.rb', line 62

def get_fields(key)
  if ( match = @headers.find{|k,v| k.downcase == key.downcase} )
    [match.last].flatten
  else
    []
  end
end

#parse_headers(header_string) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/curb-fu/response.rb', line 33

def parse_headers(header_string)
  header_lines = header_string.split($/)
  header_lines.shift
  header_lines.inject({}) do |hsh, line|
    whole_enchillada, key, value = /^(.*?):\s*(.*)$/.match(line.chomp).to_a
    unless whole_enchillada.nil?
      # note: headers with multiple instances should have multiple values in the headers hash
      hsh[key] = hsh[key] ? [hsh[key]].flatten << value : value
    end
    hsh
  end
end

#redirect?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/curb-fu/response.rb', line 17

def redirect?
  self.is_a?(CurbFu::Response::Redirection)
end

#server_fail?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/curb-fu/response.rb', line 25

def server_fail?
  self.is_a?(CurbFu::Response::ServerError)
end

#set_response_type(status) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/curb-fu/response.rb', line 74

def set_response_type(status)
  case status
  when 100..199 then
    self.extend CurbFu::Response::Information
    case self.status
    when 101 then self.extend CurbFu::Response::Continue
    when 102 then self.extend CurbFu::Response::SwitchProtocl
    end
  when 200..299 then
    self.extend CurbFu::Response::Success
    case self.status
    when 200 then self.extend CurbFu::Response::OK
    when 201 then self.extend CurbFu::Response::Created
    when 202 then self.extend CurbFu::Response::Accepted
    when 203 then self.extend CurbFu::Response::NonAuthoritativeInformation
    when 204 then self.extend CurbFu::Response::NoContent
    when 205 then self.extend CurbFu::Response::ResetContent
    when 206 then self.extend CurbFu::Response::PartialContent
    end
  when 300..399 then
    self.extend CurbFu::Response::Redirection
    case self.status
    when 300 then self.extend CurbFu::Response::MultipleChoice
    when 301 then self.extend CurbFu::Response::MovedPermanently
    when 302 then self.extend CurbFu::Response::Found
    when 303 then self.extend CurbFu::Response::SeeOther
    when 304 then self.extend CurbFu::Response::NotModified
    when 305 then self.extend CurbFu::Response::UseProxy
    when 307 then self.extend CurbFu::Response::TemporaryRedirect
    end
  when 400..499 then
    self.extend CurbFu::Response::ClientError
    case self.status
    when 400 then self.extend CurbFu::Response::BadRequest
    when 401 then self.extend CurbFu::Response::Unauthorized
    when 402 then self.extend CurbFu::Response::PaymentRequired
    when 403 then self.extend CurbFu::Response::Forbidden
    when 404 then self.extend CurbFu::Response::NotFound
    when 405 then self.extend CurbFu::Response::MethodNotAllowed
    when 406 then self.extend CurbFu::Response::NotAcceptable
    when 407 then self.extend CurbFu::Response::ProxyAuthenticationRequired
    when 408 then self.extend CurbFu::Response::RequestTimeOut
    when 409 then self.extend CurbFu::Response::Conflict
    when 410 then self.extend CurbFu::Response::Gone
    when 411 then self.extend CurbFu::Response::LengthRequired
    when 412 then self.extend CurbFu::Response::PreconditionFailed
    when 413 then self.extend CurbFu::Response::RequestEntityTooLarge
    when 414 then self.extend CurbFu::Response::RequestURITooLong
    when 415 then self.extend CurbFu::Response::UnsupportedMediaType
    when 416 then self.extend CurbFu::Response::UnsupportedMediaType
    when 417 then self.extend CurbFu::Response::ExpectationFailed
    end
  when 500..599 then
    self.extend CurbFu::Response::ServerError
    case self.status
    when 500 then self.extend CurbFu::Response::InternalServerError
    when 501 then self.extend CurbFu::Response::NotImplemented
    when 502 then self.extend CurbFu::Response::BadGateway
    when 503 then self.extend CurbFu::Response::ServiceUnavailable
    when 504 then self.extend CurbFu::Response::GatewayTimeOut
    when 505 then self.extend CurbFu::Response::VersionNotSupported
    end
  else
    self.extend CurbFu::Response::UnknownResponse
  end
end

#success?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/curb-fu/response.rb', line 13

def success?
  self.is_a?(CurbFu::Response::Success)
end

#to_hashObject



46
47
48
# File 'lib/curb-fu/response.rb', line 46

def to_hash
  { :status => status, :body => body, :headers => headers }
end