Class: Scraped::ResponseDecorator

Inherits:
Object
  • Object
show all
Defined in:
lib/scraped/response_decorator.rb

Instance Method Summary collapse

Constructor Details

#initialize(response:, decorators:) ⇒ ResponseDecorator

Returns a new instance of ResponseDecorator.



3
4
5
6
# File 'lib/scraped/response_decorator.rb', line 3

def initialize(response:, decorators:)
  @original_response = response
  @decorators = decorators.to_a
end

Instance Method Details

#responseObject



8
9
10
11
12
13
14
15
16
17
# File 'lib/scraped/response_decorator.rb', line 8

def response
  decorators.reduce(original_response) do |r, decorator_config|
    unless decorator_config.respond_to?(:[])
      decorator_config = { decorator: decorator_config }
    end
    decorator_class = decorator_config[:decorator]
    decorator_class.new(response: r, config: decorator_config)
                   .decorated_response
  end
end