Class: Easy::Api::Error
- Inherits:
-
Object
- Object
- Easy::Api::Error
- Defined in:
- lib/easy/api/error.rb
Overview
Encapsulates the types of errors that API calls can respond with
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#message ⇒ Object
readonly
Returns the value of attribute message.
Class Method Summary collapse
Instance Method Summary collapse
-
#as_json(options = {}) ⇒ Hash
Used by Rails to parse the error as json.
-
#initialize(type, msg = nil) ⇒ Error
constructor
Initializes a new error based on the type, with an optional custom message.
-
#to_hash ⇒ Hash
Returns the error as a hash.
-
#to_xml(options = {}) ⇒ Hash
Used by Rails to parse the error as xml.
Constructor Details
#initialize(type, msg = nil) ⇒ Error
Initializes a new error based on the type, with an optional custom message
28 29 30 31 |
# File 'lib/easy/api/error.rb', line 28 def initialize(type, msg=nil) @code = self.class.codes[type] @message = msg || self.class.[type] end |
Instance Attribute Details
#code ⇒ Object (readonly)
Returns the value of attribute code.
4 5 6 |
# File 'lib/easy/api/error.rb', line 4 def code @code end |
#message ⇒ Object (readonly)
Returns the value of attribute message.
4 5 6 |
# File 'lib/easy/api/error.rb', line 4 def @message end |
Class Method Details
.codes ⇒ Object
6 7 8 9 10 11 12 13 |
# File 'lib/easy/api/error.rb', line 6 def self.codes @_codes ||= { :invalid => 400, :unauthorized => 401, :not_found => 404, :unexpected => 500 } end |
.messages ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/easy/api/error.rb', line 15 def self. @_messages ||= { :invalid => "Invalid request", :unauthorized => "Unauthorized request", :not_found => "Resource not found", :unexpected => 'Sorry! An exception has occurred', } end |
Instance Method Details
#as_json(options = {}) ⇒ Hash
Used by Rails to parse the error as json
43 44 45 |
# File 'lib/easy/api/error.rb', line 43 def as_json(={}) to_hash end |
#to_hash ⇒ Hash
Returns the error as a hash
36 37 38 |
# File 'lib/easy/api/error.rb', line 36 def to_hash {:code => @code, :message => @message} end |
#to_xml(options = {}) ⇒ Hash
Used by Rails to parse the error as xml
50 51 52 |
# File 'lib/easy/api/error.rb', line 50 def to_xml(={}) to_hash.to_xml() end |