Class: FaradayMiddleware::Mashify

Inherits:
Faraday::Response::Middleware
  • Object
show all
Defined in:
lib/faraday_middleware/response/mashify.rb

Overview

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

Direct Known Subclasses

Rashify

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Mashify.



18
19
20
21
# File 'lib/faraday_middleware/response/mashify.rb', line 18

def initialize(app = nil, options = {})
  super(app)
  self.mash_class = options[:mash_class] || self.class.mash_class
end

Class Attribute Details

.mash_classObject

Returns the value of attribute mash_class.



10
11
12
# File 'lib/faraday_middleware/response/mashify.rb', line 10

def mash_class
  @mash_class
end

Instance Attribute Details

#mash_classObject

Returns the value of attribute mash_class.



7
8
9
# File 'lib/faraday_middleware/response/mashify.rb', line 7

def mash_class
  @mash_class
end

Instance Method Details

#parse(body) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/faraday_middleware/response/mashify.rb', line 23

def parse(body)
  case body
  when Hash
    mash_class.new(body)
  when Array
    body.map { |item| parse(item) }
  else
    body
  end
end