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.

Defined Under Namespace

Classes: ResponseHash

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#bodyObject

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

#codeObject

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

#statusObject

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

Returns:

  • (Boolean)

    A boolean that binarizes the response status result.



65
66
67
# File 'lib/mailgun/response.rb', line 65

def success?
  (200..299).include?(status)
end

#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