class Faraday::Mashify::Middleware

Public: Converts parsed response bodies to a Hashie::Mash if they were of Hash or Array type.

Attributes

mash_class[RW]

@!attribute [rw] mash_class

@return [Class]
mash_class[RW]

@!attribute [rw] mash_class

@return [Class]

Public Class Methods

new(app = nil, options = {}) click to toggle source

@param app [Proc] @param options [Hash] @option options [Class] :mash_class Responses are wrapped in this class (default is ‘::Hashie::Mash`)

Calls superclass method
# File lib/faraday/mashify/middleware.rb, line 20
def initialize(app = nil, options = {})
  super(app, options)
  self.mash_class = options[:mash_class] || self.class.mash_class || ::Hashie::Mash
end

Public Instance Methods

on_complete(env) click to toggle source

This method will be called when the response is being processed. You can alter it as you like, accessing things like response_body, response_headers, and more. Refer to Faraday::Env for a list of accessible fields: github.com/lostisland/faraday/blob/main/lib/faraday/options/env.rb

@param env [Faraday::Env] the environment of the response being processed.

# File lib/faraday/mashify/middleware.rb, line 31
def on_complete(env)
  env[:body] = parse(env[:body])
end

Private Instance Methods

parse(body) click to toggle source
# File lib/faraday/mashify/middleware.rb, line 37
def parse(body)
  case body
  when Hash
    mash_class.new(body)
  when Array
    body.map { |item| parse(item) }
  else
    body
  end
end