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.



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

#bodyObject

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

#codeObject

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

#statusObject

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

Returns:

  • (Boolean)

    A boolean that binarizes the response status result.



67
68
69
# File 'lib/mailgun/response.rb', line 67

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

#to_hHash

Return response as Ruby Hash

Returns:

  • (Hash)

    A standard Ruby Hash containing the HTTP result.



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

Returns:

  • (Hash)

    A standard Ruby Hash containing the HTTP result.



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_yamlString

Return response as Yaml

Returns:

  • (String)

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

Returns:

  • (String)

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