Class: Mailgun::Response
- Inherits:
-
Object
- Object
- Mailgun::Response
- Defined in:
- lib/mailgun/response.rb
Overview
A Mailgun::Response object is instantiated for each response generated by the Client request. The Response object supports deserialization of the JSON result. Or, if you prefer JSON or YAML formatting, call the method for conversion.
See the Github documentation for full examples.
Defined Under Namespace
Classes: ResponseHash
Instance Attribute Summary collapse
-
#body ⇒ Object
All responses have a payload and a status corresponding to http, though slightly different.
-
#code ⇒ Object
All responses have a payload and a status corresponding to http, though slightly different.
-
#status ⇒ Object
All responses have a payload and a status corresponding to http, though slightly different.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(response) ⇒ Response
constructor
A new instance of Response.
-
#success? ⇒ Boolean
Returns true if response status is 2xx.
-
#to_h ⇒ Hash
Return response as Ruby Hash.
-
#to_h! ⇒ Hash
Replace @body with Ruby Hash.
-
#to_yaml ⇒ String
Return response as Yaml.
-
#to_yaml! ⇒ String
Replace @body with YAML.
Constructor Details
#initialize(response) ⇒ Response
Returns a new instance of Response.
19 20 21 22 23 |
# File 'lib/mailgun/response.rb', line 19 def initialize(response) @body = response.body @status = response.status @code = response.status end |
Instance Attribute Details
#body ⇒ Object
All responses have a payload and a status corresponding to http, though
slightly different
11 12 13 |
# File 'lib/mailgun/response.rb', line 11 def body @body end |
#code ⇒ Object
All responses have a payload and a status corresponding to http, though
slightly different
11 12 13 |
# File 'lib/mailgun/response.rb', line 11 def code @code end |
#status ⇒ Object
All responses have a payload and a status corresponding to http, though
slightly different
11 12 13 |
# File 'lib/mailgun/response.rb', line 11 def status @status end |
Class Method Details
.from_hash(h) ⇒ Object
14 15 16 17 |
# File 'lib/mailgun/response.rb', line 14 def self.from_hash(h) # Create a "fake" response object with the data passed from h self.new ResponseHash.new(h[:body], h[:status]) end |
Instance Method Details
#success? ⇒ Boolean
Returns true if response status is 2xx
65 66 67 |
# File 'lib/mailgun/response.rb', line 65 def success? (200..299).include?(status) end |
#to_h ⇒ Hash
Return response as Ruby Hash
29 30 31 32 33 |
# File 'lib/mailgun/response.rb', line 29 def to_h JSON.parse(@body) rescue => err raise ParseError.new(err), err end |
#to_h! ⇒ Hash
Replace @body with Ruby Hash
38 39 40 41 42 |
# File 'lib/mailgun/response.rb', line 38 def to_h! @body = JSON.parse(@body) rescue => err raise ParseError.new(err), err end |
#to_yaml ⇒ String
Return response as Yaml
47 48 49 50 51 |
# File 'lib/mailgun/response.rb', line 47 def to_yaml YAML.dump(to_h) rescue => err raise ParseError.new(err), err end |
#to_yaml! ⇒ String
Replace @body with YAML
56 57 58 59 60 |
# File 'lib/mailgun/response.rb', line 56 def to_yaml! @body = YAML.dump(to_h) rescue => err raise ParseError.new(err), err end |