Class: Faraday::Mashify::Middleware

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/faraday/mashify/middleware.rb

Overview

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app = nil, options = {}) ⇒ Middleware

Returns a new instance of Middleware.

Parameters:

  • app (Proc) (defaults to: nil)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :mash_class (Class)

    Responses are wrapped in this class (default is ::Hashie::Mash)



20
21
22
23
# 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

Class Attribute Details

.mash_classClass

Returns:

  • (Class)


14
15
16
# File 'lib/faraday/mashify/middleware.rb', line 14

def mash_class
  @mash_class
end

Instance Attribute Details

#mash_classClass

Returns:

  • (Class)


9
10
11
# File 'lib/faraday/mashify/middleware.rb', line 9

def mash_class
  @mash_class
end

Instance Method Details

#on_complete(env) ⇒ Object

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

Parameters:

  • env (Faraday::Env)

    the environment of the response being processed.



31
32
33
# File 'lib/faraday/mashify/middleware.rb', line 31

def on_complete(env)
  env[:body] = parse(env[:body])
end