Class: Httpstatus::Status

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

Constant Summary collapse

DEFAULT =
:internal_server_error

Instance Method Summary collapse

Constructor Details

#initialize(key = nil) ⇒ Status

Returns a new instance of Status.



5
6
7
# File 'lib/httpstatus/status.rb', line 5

def initialize key = nil
  self.key = key
end

Instance Method Details

#codeObject



18
19
20
# File 'lib/httpstatus/status.rb', line 18

def code
  i18n :code
end

#default!Object



34
35
36
# File 'lib/httpstatus/status.rb', line 34

def default!
  @key = nil
end

#default?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/httpstatus/status.rb', line 38

def default?
  status.key == DEFAULT
end

#error?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/httpstatus/status.rb', line 30

def error?
  (400..599) === code
end

#keyObject



9
10
11
# File 'lib/httpstatus/status.rb', line 9

def key
  @key || DEFAULT
end

#key=(v) ⇒ Object



13
14
15
16
# File 'lib/httpstatus/status.rb', line 13

def key= v
  @key = v.is_a?(Symbol) ? v : (v.is_a?(String) ? v.to_sym : nil)
  default! unless valid?
end

#messageObject



26
27
28
# File 'lib/httpstatus/status.rb', line 26

def message
  i18n :message
end

#titleObject



22
23
24
# File 'lib/httpstatus/status.rb', line 22

def title
  i18n :title
end

#to_hash(*options) ⇒ Object Also known as: to_h, as_json



42
43
44
# File 'lib/httpstatus/status.rb', line 42

def to_hash *options
  { status: code, title: title, message: message }
end

#to_json(*options) ⇒ Object



52
53
54
# File 'lib/httpstatus/status.rb', line 52

def to_json(*options)
  to_h.to_json(*options).html_safe
end

#to_sObject



48
49
50
# File 'lib/httpstatus/status.rb', line 48

def to_s
  "#{code} #{title}\n#{message}"
end

#to_xml(*options) ⇒ Object



60
61
62
# File 'lib/httpstatus/status.rb', line 60

def to_xml(*options)
  to_h.to_xml(*options).html_safe
end

#to_yaml(*options) ⇒ Object



56
57
58
# File 'lib/httpstatus/status.rb', line 56

def to_yaml(*options)
  to_h.to_yaml(*options).html_safe
end