Class: Dao::Status
- Inherits:
-
String
- Object
- String
- Dao::Status
- Defined in:
- lib/dao/status.rb
Constant Summary collapse
- Code2Message =
( { 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", 420 => "Enhance Your Calm", 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" } )
- Groups =
({ 100 => 'instruction', 200 => 'success', 300 => 'redirection', 400 => 'client_error', 500 => 'server_error' })
- Symbol2Code =
(code, message)| sym = Status.underscore(message.gsub(/\s+/, "")).to_sym hash.update(sym => code) end )
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
instance methods.
-
#group ⇒ Object
readonly
Returns the value of attribute group.
-
#message ⇒ Object
readonly
Returns the value of attribute message.
Class Method Summary collapse
- .default ⇒ Object
- .for(*args) ⇒ Object
- .list ⇒ Object
- .parse(string) ⇒ Object
- .underscore(camel_cased_word) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
- #=~(other) ⇒ Object
- #bad? ⇒ Boolean (also: #error?)
- #clone ⇒ Object
- #good? ⇒ Boolean (also: #ok?)
-
#initialize(*args) ⇒ Status
constructor
A new instance of Status.
- #to_json(*args, &block) ⇒ Object
- #update(*args) ⇒ Object (also: #set)
Constructor Details
#initialize(*args) ⇒ Status
Returns a new instance of Status.
172 173 174 |
# File 'lib/dao/status.rb', line 172 def initialize(*args) update(*args) end |
Instance Attribute Details
#code ⇒ Object (readonly)
instance methods
168 169 170 |
# File 'lib/dao/status.rb', line 168 def code @code end |
#group ⇒ Object (readonly)
Returns the value of attribute group.
170 171 172 |
# File 'lib/dao/status.rb', line 170 def group @group end |
#message ⇒ Object (readonly)
Returns the value of attribute message.
169 170 171 |
# File 'lib/dao/status.rb', line 169 def @message end |
Class Method Details
.for(*args) ⇒ Object
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 |
# File 'lib/dao/status.rb', line 92 def for(*args) if args.size >= 2 code = args.shift = args.join(' ') new(code, ) else arg = args.shift case arg when Result result = arg if arg.errors.nil? or arg.errors.empty? or arg.valid? new(200) else new(500) end when Status arg when Fixnum code = arg = Code2Message[code] new(code, ) when Symbol, String if arg.to_s =~ %r/^\d+$/ code = arg.to_i else sym = Status.underscore(arg).to_sym code = Symbol2Code[sym] end if code = Code2Message[code] else code = 500 = "Unknown Status #{ arg }" end new(code, ) else if arg.respond_to?(:code) and arg.respond_to?(:message) code, = arg.code, arg. new(code, ) else parse(arg) end end end end |
.list ⇒ Object
75 76 77 |
# File 'lib/dao/status.rb', line 75 def list @list ||= Symbol2Code.sort_by{|sym, code| code}.map{|sym, code| send(sym)} end |
.parse(string) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/dao/status.rb', line 138 def parse(string) first, last = string.to_s.strip.split(%r/\s+/, 2) if first =~ %r/^\d+$/ code = Integer(first) = last else code = 500 = "Unknown Status #{ string.inspect }" end new(code, ) end |
.underscore(camel_cased_word) ⇒ Object
79 80 81 82 83 84 85 86 |
# File 'lib/dao/status.rb', line 79 def underscore(camel_cased_word) camel_cased_word.to_s.gsub(/::/, '/'). gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). #gsub(/\s+/, ''). tr("-", "_"). downcase end |
Instance Method Details
#==(other) ⇒ Object
221 222 223 224 225 226 227 228 |
# File 'lib/dao/status.rb', line 221 def ==(other) begin other = Status.for(other) self.code == other.code and self. == other. rescue false end end |
#=~(other) ⇒ Object
212 213 214 215 216 217 218 219 |
# File 'lib/dao/status.rb', line 212 def =~(other) begin other = Status.for(other) self.group == other.group rescue false end end |
#bad? ⇒ Boolean Also known as: error?
207 208 209 |
# File 'lib/dao/status.rb', line 207 def bad? @group >= 400 end |
#clone ⇒ Object
230 231 232 |
# File 'lib/dao/status.rb', line 230 def clone clone = Status.for(code) end |
#good? ⇒ Boolean Also known as: ok?
202 203 204 |
# File 'lib/dao/status.rb', line 202 def good? @group < 400 end |
#to_json(*args, &block) ⇒ Object
234 235 236 |
# File 'lib/dao/status.rb', line 234 def to_json(*args, &block) Map[:code, code, :message, ].to_json(*args, &block) end |
#update(*args) ⇒ Object Also known as: set
176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/dao/status.rb', line 176 def update(*args) code, = if args.size == 2 [args.first, args.last] else status = Status.for(args.first || 200) [status.code, status.] end @code, @message = Integer(code), String() @group = (@code / 100) * 100 replace("#{ @code } #{ @message }".strip) end |