Class: Mailgun::Response

Inherits:
Object
  • Object
show all
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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Returns a new instance of Response.


20
21
22
23
# File 'lib/mailgun/response.rb', line 20

def initialize(response)
  @body = response.body
  @code = response.code
end

Instance Attribute Details

#bodyObject

All responses have a payload and a code corresponding to http, though

slightly different

13
14
15
# File 'lib/mailgun/response.rb', line 13

def body
  @body
end

#codeObject

All responses have a payload and a code corresponding to http, though

slightly different

13
14
15
# File 'lib/mailgun/response.rb', line 13

def code
  @code
end

Class Method Details

.from_hash(h) ⇒ Object


15
16
17
18
# File 'lib/mailgun/response.rb', line 15

def self.from_hash(h)
  # Create a "fake" response object with the data passed from h
  self.new OpenStruct.new(h)
end

Instance Method Details

#to_hHash

Return response as Ruby Hash

Returns:

  • (Hash)

    A standard Ruby Hash containing the HTTP result.


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

Returns:

  • (Hash)

    A standard Ruby Hash containing the HTTP result.


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_yamlString

Return response as Yaml

Returns:

  • (String)

    A string containing 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

Returns:

  • (String)

    A string containing response as 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