Class: Mailgun::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/mailgun.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

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Returns a new instance of Response.



194
195
196
197
# File 'lib/mailgun.rb', line 194

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

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



191
192
193
# File 'lib/mailgun.rb', line 191

def body
  @body
end

#codeObject

Returns the value of attribute code.



192
193
194
# File 'lib/mailgun.rb', line 192

def code
  @code
end

Instance Method Details

#to_hHash

Return response as Ruby Hash

Returns:

  • (Hash)

    A standard Ruby Hash containing the HTTP result.



203
204
205
206
207
208
209
# File 'lib/mailgun.rb', line 203

def to_h
  begin
    JSON.parse(@body)
  rescue Exception => e
    raise ParseError.new(e), e
  end
end

#to_h!Hash

Replace @body with Ruby Hash

Returns:

  • (Hash)

    A standard Ruby Hash containing the HTTP result.



215
216
217
218
219
220
221
# File 'lib/mailgun.rb', line 215

def to_h!
  begin
    @body = JSON.parse(@body)
  rescue Exception => e
    raise ParseError.new(e), e
  end
end

#to_yamlString

Return response as Yaml

Returns:

  • (String)

    A string containing response as YAML



227
228
229
230
231
232
233
# File 'lib/mailgun.rb', line 227

def to_yaml
  begin
    YAML::dump(JSON.parse(@body))
  rescue Exception => e
    raise ParseError.new(e), e
  end
end

#to_yaml!String

Replace @body with YAML

Returns:

  • (String)

    A string containing response as YAML



239
240
241
242
243
244
245
# File 'lib/mailgun.rb', line 239

def to_yaml!
  begin
    @body = YAML::dump(JSON.parse(@body))
  rescue Exception => e
    raise ParseError.new(e), e
  end
end