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.
21 22 23 24 25 |
# File 'lib/mailgun/response.rb', line 21 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
13 14 15 |
# File 'lib/mailgun/response.rb', line 13 def body @body end |
#code ⇒ Object
All responses have a payload and a status corresponding to http, though
slightly different
13 14 15 |
# File 'lib/mailgun/response.rb', line 13 def code @code end |
#status ⇒ Object
All responses have a payload and a status corresponding to http, though
slightly different
13 14 15 |
# File 'lib/mailgun/response.rb', line 13 def status @status end |
Class Method Details
.from_hash(h) ⇒ Object
16 17 18 19 |
# File 'lib/mailgun/response.rb', line 16 def self.from_hash(h) # Create a "fake" response object with the data passed from h new ResponseHash.new(h[:body], h[:status]) end |
Instance Method Details
#success? ⇒ Boolean
Returns true if response status is 2xx
67 68 69 |
# File 'lib/mailgun/response.rb', line 67 def success? (200..299).include?(status) end |
#to_h ⇒ Hash
Return response as Ruby Hash
31 32 33 34 35 |
# File 'lib/mailgun/response.rb', line 31 def to_h JSON.parse(@body) rescue StandardError => e raise ParseError.new(e), e end |
#to_h! ⇒ Hash
Replace @body with Ruby Hash
40 41 42 43 44 |
# File 'lib/mailgun/response.rb', line 40 def to_h! @body = JSON.parse(@body) rescue StandardError => e raise ParseError.new(e), e end |
#to_yaml ⇒ String
Return response as Yaml
49 50 51 52 53 |
# File 'lib/mailgun/response.rb', line 49 def to_yaml YAML.dump(to_h) rescue StandardError => e raise ParseError.new(e), e end |
#to_yaml! ⇒ String
Replace @body with YAML
58 59 60 61 62 |
# File 'lib/mailgun/response.rb', line 58 def to_yaml! @body = YAML.dump(to_h) rescue StandardError => e raise ParseError.new(e), e end |