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.



173
174
175
176
# File 'lib/mailgun.rb', line 173

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

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



170
171
172
# File 'lib/mailgun.rb', line 170

def body
  @body
end

#codeObject

Returns the value of attribute code.



171
172
173
# File 'lib/mailgun.rb', line 171

def code
  @code
end

Instance Method Details

#to_hHash

Return response as Ruby Hash

Returns:

  • (Hash)

    A standard Ruby Hash containing the HTTP result.



182
183
184
185
186
187
188
# File 'lib/mailgun.rb', line 182

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.



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

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



206
207
208
209
210
211
212
# File 'lib/mailgun.rb', line 206

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



218
219
220
221
222
223
224
# File 'lib/mailgun.rb', line 218

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