Sha256: ffa55fd84a7f0d14d1f0c0a185dd1a63fc2fcc7529e2ce6763906c24e703e9eb

Contents?: true

Size: 1.53 KB

Versions: 20

Compression:

Stored size: 1.53 KB

Contents

require 'ostruct'

module Mailgun
  # 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.
  class Response
    # All responses have a payload and a code corresponding to http, though
    #   slightly different
    attr_accessor :body, :code

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

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

    # Return response as Ruby Hash
    #
    # @return [Hash] A standard Ruby Hash containing the HTTP result.

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

    # Replace @body with Ruby Hash
    #
    # @return [Hash] A standard Ruby Hash containing the HTTP result.
    def to_h!
      @body = JSON.parse(@body)
    rescue => err
      raise ParseError.new(err), err
    end

    # Return response as Yaml
    #
    # @return [String] A string containing response as YAML
    def to_yaml
      YAML.dump(to_h)
    rescue => err
      raise ParseError.new(err), err
    end

    # Replace @body with YAML
    #
    # @return [String] A string containing response as YAML
    def to_yaml!
      @body = YAML.dump(to_h)
    rescue => err
      raise ParseError.new(err), err
    end
  end
end

Version data entries

20 entries across 20 versions & 2 rubygems

Version Path
mailgun-ruby-1.2.11 lib/mailgun/response.rb
mailgun-ruby-1.2.10 lib/mailgun/response.rb
mailgun-ruby-1.2.9 lib/mailgun/response.rb
mailgun-ruby-1.2.8 lib/mailgun/response.rb
mailgun-ruby-1.2.7 lib/mailgun/response.rb
mailgun-ruby-1.2.6 lib/mailgun/response.rb
mailgun-ruby-1.2.5 lib/mailgun/response.rb
mailgun-ruby-1.2.4 lib/mailgun/response.rb
mailgun-ruby-1.2.3 lib/mailgun/response.rb
mailgun-ruby-1.2.0 lib/mailgun/response.rb
mailgun-ruby-1.1.11 lib/mailgun/response.rb
mailgun-ruby-1.1.10 lib/mailgun/response.rb
mailgun-ruby-1.1.9 lib/mailgun/response.rb
wj-mailgun-ruby-1.1.7 lib/mailgun/response.rb
mailgun-ruby-1.1.8 lib/mailgun/response.rb
mailgun-ruby-1.1.6 lib/mailgun/response.rb
mailgun-ruby-1.1.5 lib/mailgun/response.rb
mailgun-ruby-1.1.4 lib/mailgun/response.rb
mailgun-ruby-1.1.3 lib/mailgun/response.rb
mailgun-ruby-1.1.2 lib/mailgun/response.rb