Class: ApiHammer::FaradayOutputter

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/api_hammer/faraday/outputter.rb

Overview

outputs the response body to the given logger or output device (defaulting to STDOUT)

Direct Known Subclasses

FaradayCurlVOutputter

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ FaradayOutputter

Returns a new instance of FaradayOutputter.



8
9
10
11
12
# File 'lib/api_hammer/faraday/outputter.rb', line 8

def initialize(app, options={})
  @app=app
  @options = options
  @outdev = @options[:outdev] || STDOUT
end

Instance Method Details

#call(request_env) ⇒ Object



14
15
16
17
18
# File 'lib/api_hammer/faraday/outputter.rb', line 14

def call(request_env)
  @app.call(request_env).on_complete do |response_env|
    puts(response_env[:body] || '')
  end
end

#puts(str) ⇒ Object



20
21
22
23
# File 'lib/api_hammer/faraday/outputter.rb', line 20

def puts(str)
  meth = @options[:logger] ? @options[:logger].method(:info) : (@options[:outdev] || STDOUT).method(:puts)
  meth.call(str)
end