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",


  ### ref: https://github.com/joho/7XX-rfc

  #70X => "Inexcusable",
    701 => "Meh",
    702 => "Emacs",

  #71X => "Novelty Implementations",
    710 => "PHP",
    711 => "Convenience Store",
    719 => "I am not a teapot",

  #72X => "Edge Cases",
    720 => "Unpossible",
    721 => "Known Unknowns",
    722 => "Unknown Unknowns",
    723 => "Tricky",
    724 => "This line should be unreachable",
    725 => "It works on my machine",
    726 => "It's a feature, not a bug",

  #73X => "Fucking",
    731 => "Fucking Rubygems",
    732 => "Fucking Unicode",
    733 => "Fucking Deadlocks",
    734 => "Fucking Deferreds",
    735 => "Fucking IE",
    736 => "Fucking Race Conditions",
    737 => "FuckThreadsing",
    738 => "Fucking Bundler",
    739 => "Fucking Windows",

  #74X => "Meme Driven",
    741 => "Compiling",
    742 => "A kitten dies",
    743 => "I thought I knew regular expressions",
    744 => "Y U NO write integration tests?",
    745 => "I don't always test my code, but when I do I do it in production",
    746 => "Missed Ballmer Peak",
    747 => "Motherfucking Snakes on the Motherfucking Plane",
    748 => "Confounded by Ponies",
    749 => "Reserved for Chuck Norris",

  #75X => "Syntax Errors",
    750 => "Didn't bother to compile it",
    753 => "Syntax Error",

  #76X => "Substance-Affected Developer",
    761 => "Hungover",
    762 => "Stoned",
    763 => "Under-Caffeinated",
    764 => "Over-Caffeinated",
    765 => "Railscamp",
    766 => "Sober",
    767 => "Drunk",

  #77X => "Predictable Problems",
    771 => "Cached for too long",
    772 => "Not cached long enough",
    773 => "Not cached at all",
    774 => "Why was this cached?",
    776 => "Error on the Exception",
    777 => "Coincidence",
    778 => "Off By One Error",
    779 => "Off By Too Many To Count Error",

  #78X => "Somebody Else's Problem",
    781 => "Operations",
    782 => "QA",
    783 => "It was a customer request, honestly",
    784 => "Management, obviously",
    785 => "TPS Cover Sheet not attached",

  #79X => "Internet crashed",
    797 => "This is the last page of the Internet. Go back",
    799 => "End of the world"
  }
)
Symbol2Code =
(code, message)|
    sym = Status.underscore(message.gsub(/\s+/, "_")).to_sym
    hash.update(sym => code)
  end
)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Status

Returns a new instance of Status.



256
257
258
# File 'lib/dao/status.rb', line 256

def initialize(*args)
  update(*args)
end

Instance Attribute Details

#codeObject

instance methods



251
252
253
# File 'lib/dao/status.rb', line 251

def code
  @code
end

#groupObject

Returns the value of attribute group.



253
254
255
# File 'lib/dao/status.rb', line 253

def group
  @group
end

#messageObject

Returns the value of attribute message.



252
253
254
# File 'lib/dao/status.rb', line 252

def message
  @message
end

#sourceObject

Returns the value of attribute source.



254
255
256
# File 'lib/dao/status.rb', line 254

def source
  @source
end

Class Method Details

.defaultObject



171
172
173
# File 'lib/dao/status.rb', line 171

def default
  Status.for(200)
end

.for(*args) ⇒ Object



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
213
214
215
216
217
218
219
# File 'lib/dao/status.rb', line 175

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



154
155
156
# File 'lib/dao/status.rb', line 154

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

.parse(string) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
# File 'lib/dao/status.rb', line 221

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



158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/dao/status.rb', line 158

def underscore(camel_cased_word)
  camel_cased_word.to_s.gsub(/::/, '/').
    gsub(/\s+/, '_').
    gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
    gsub(/([a-z\d])([A-Z])/,'\1_\2').
    gsub(/[^A-Za-z0-9_]/, '_').
    tr("-", "_").
    squeeze('_').
    gsub(/^_+/, '').
    gsub(/_+$/, '').
    downcase
end

Instance Method Details

#==(other) ⇒ Object



309
310
311
312
313
314
315
316
# File 'lib/dao/status.rb', line 309

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

#=~(other) ⇒ Object



300
301
302
303
304
305
306
307
# File 'lib/dao/status.rb', line 300

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

#bad?Boolean Also known as: error?

Returns:

  • (Boolean)


295
296
297
# File 'lib/dao/status.rb', line 295

def bad?
  @group >= 400
end

#cloneObject



318
319
320
# File 'lib/dao/status.rb', line 318

def clone
  clone = Status.for(code)
end

#good?Boolean Also known as: ok?

Returns:

  • (Boolean)


290
291
292
# File 'lib/dao/status.rb', line 290

def good?
  @group < 400
end

#ok!Object



274
275
276
# File 'lib/dao/status.rb', line 274

def ok!
  update(200)
end

#to_json(*args, &block) ⇒ Object



322
323
324
# File 'lib/dao/status.rb', line 322

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

#update(*args) ⇒ Object Also known as: set



260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/dao/status.rb', line 260

def update(*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