Class: FaradayCage::Status

Inherits:
Object
  • Object
show all
Defined in:
lib/faraday_cage/status.rb

Overview

Wraps status codes to provide various helper methods for testing a response.

Examples:

Testing whether a status is successful.


status.success?

Getting a status name.


status.name
# => :service_unavailable

Comparing a status.


status == 200
# => true

Converting a status.


status.to_i
# => 422
status.to_sym
# => :unprocessable_entity

Testing a status by name.


status.forbidden?
# => false

Constant Summary collapse

MAPPINGS =
{
  100 => :continue,
  101 => :switching_protocols,
  102 => :processing,
  200 => :ok,
  201 => :created,
  202 => :accepted,
  203 => :non_authoritative_information,
  204 => :no_content,
  205 => :reset_content,
  206 => :partial_content,
  207 => :multi_status,
  226 => :im_used,
  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 => :requested_range_not_satisfiable,
  417 => :expectation_failed,
  422 => :unprocessable_entity,
  423 => :locked,
  424 => :failed_dependency,
  426 => :upgrade_required,
  500 => :internal_server_error,
  501 => :not_implemented,
  502 => :bad_gateway,
  503 => :service_unavailable,
  504 => :gateway_timeout,
  505 => :http_version_not_supported,
  507 => :insufficient_storage,
  510 => :not_extended
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code) ⇒ Status

Returns a new instance of Status.



93
94
95
# File 'lib/faraday_cage/status.rb', line 93

def initialize(code)
  @code = code.to_i
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



91
92
93
# File 'lib/faraday_cage/status.rb', line 91

def code
  @code
end

Instance Method Details

#==(object) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/faraday_cage/status.rb', line 97

def ==(object)
  if object.respond_to?(:code)
    object.code == code
  else
    object == code || object == name
  end
end

#client_error?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/faraday_cage/status.rb', line 148

def client_error?
  type == :client_error
end

#error?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/faraday_cage/status.rb', line 156

def error?
  client_error? || server_error?
end

#informational?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/faraday_cage/status.rb', line 136

def informational?
  type == :informational
end

#inspectObject



121
122
123
# File 'lib/faraday_cage/status.rb', line 121

def inspect
  "#<#{self.class} code: #{code.inspect} name: #{name.inspect}>"
end

#nameObject



105
106
107
# File 'lib/faraday_cage/status.rb', line 105

def name
  @name ||= MAPPINGS[code]
end

#redirect?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/faraday_cage/status.rb', line 144

def redirect?
  type == :redirect
end

#server_error?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/faraday_cage/status.rb', line 152

def server_error?
  type == :server_error
end

#success?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/faraday_cage/status.rb', line 140

def success?
  type == :success
end

#to_iObject



109
110
111
# File 'lib/faraday_cage/status.rb', line 109

def to_i
  code
end

#to_sObject



113
114
115
# File 'lib/faraday_cage/status.rb', line 113

def to_s
  name.to_s
end

#to_symObject



117
118
119
# File 'lib/faraday_cage/status.rb', line 117

def to_sym
  name
end

#typeObject



125
126
127
128
129
130
131
132
133
134
# File 'lib/faraday_cage/status.rb', line 125

def type
  @type ||= case code
  when 100..199 then :informational
  when 200..299 then :success
  when 300..399 then :redirect
  when 400..499 then :client_error
  when 500..599 then :server_error
  else               :unknown
  end
end

#unknown?Boolean

Returns:

  • (Boolean)


160
161
162
# File 'lib/faraday_cage/status.rb', line 160

def unknown?
  type == :unknown
end