Class: Dao::Status

Inherits:
String
  • Object
show all
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"
  }
)
Symbol2Code =
(code, message)|
    sym = Status.underscore(message.gsub(/\s+/, "")).to_sym
    hash.update(sym => code)
  end
)
Groups =
({
  100 => 'instruction',
  200 => 'success',
  300 => 'redirection',
  400 => 'client_error',
  500 => 'server_error'
})

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Status

Returns a new instance of Status.



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/dao/status.rb', line 92

def initialize(*args)
  code, message =
    if args.size == 2
      [args.first, args.last]
    else
      status = Status.for(args.first || 200)
      [status.code, status.message]
    end
  @code, @message = Integer(code), String(message)
  @group = (@code / 100) * 100
  replace("#{ @code } #{ @message }".strip)
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



88
89
90
# File 'lib/dao/status.rb', line 88

def code
  @code
end

#groupObject (readonly)

Returns the value of attribute group.



90
91
92
# File 'lib/dao/status.rb', line 90

def group
  @group
end

#messageObject (readonly)

Returns the value of attribute message.



89
90
91
# File 'lib/dao/status.rb', line 89

def message
  @message
end

Class Method Details

.cast(*args) ⇒ Object



226
227
228
229
230
231
232
233
# File 'lib/dao/status.rb', line 226

def cast(*args)
  if args.size == 1
    value = args.first
    value.is_a?(self) ? value : self.for(value)
  else
    self.for(*args)
  end
end

.for(*args) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/dao/status.rb', line 168

def for(*args)
  if args.size >= 2
    code = args.shift
    message = args.join(' ')
    new(code, message)
  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
        message = Code2Message[code]
        new(code, message)
      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
          message = Code2Message[code]
        else
          code = 500
          message = "Unknown Status #{ arg }"
        end
        new(code, message)
      else
        if arg.respond_to?(:code) and arg.respond_to?(:message)
          code, message = arg.code, arg.message
          new(code, message)
        else
          parse(arg)
        end
    end
  end
end

.listObject



127
128
129
# File 'lib/dao/status.rb', line 127

def Status.list
  @list ||= Symbol2Code.sort_by{|sym, code| code}.map{|sym, code| send(sym)}
end

.parse(string) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
# File 'lib/dao/status.rb', line 214

def parse(string)
  first, last = string.to_s.strip.split(%r/\s+/, 2)
  if first =~ %r/^\d+$/
    code = Integer(first)
    message = last
  else
    code = 500
    message = "Unknown Status #{ string.inspect }"
  end
  new(code, message)
end

.underscore(camel_cased_word) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/dao/status.rb', line 64

def Status.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



150
151
152
153
154
155
156
157
# File 'lib/dao/status.rb', line 150

def ==(other)
  begin
    other = Status.for(other)
    self.code == other.code and self.message == other.message
  rescue
    false
  end
end

#=~(other) ⇒ Object



141
142
143
144
145
146
147
148
# File 'lib/dao/status.rb', line 141

def =~(other)
  begin
    other = Status.for(other)
    self.group == other.group
  rescue
    false
  end
end

#bad?Boolean Also known as: error?

Returns:

  • (Boolean)


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

def bad?
  @group >= 400
end

#cloneObject



159
160
161
# File 'lib/dao/status.rb', line 159

def clone
  clone = Status.for(code)
end

#good?Boolean Also known as: ok?

Returns:

  • (Boolean)


131
132
133
# File 'lib/dao/status.rb', line 131

def good?
  @group < 400
end

#to_json(*args, &block) ⇒ Object



163
164
165
# File 'lib/dao/status.rb', line 163

def to_json(*args, &block)
  Map[:code, code, :message, message].to_json(*args, &block)
end