Module: ReqresRspec::Formatters

Extended by:
Formatters
Included in:
Formatters
Defined in:
lib/reqres_rspec/formatters.rb,
lib/reqres_rspec/formatters/pdf.rb,
lib/reqres_rspec/formatters/base.rb,
lib/reqres_rspec/formatters/html.rb,
lib/reqres_rspec/formatters/json.rb

Defined Under Namespace

Classes: Base, HTML, JSON, Pdf

Instance Method Summary collapse

Instance Method Details

#process(records) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/reqres_rspec/formatters.rb', line 5

def process(records)
  formatters = ReqresRspec.configuration.formatters
  raise 'No formatters defined' if formatters.empty?

  formatters.each do |fmt|
    case fmt
    when 'html'
      HTML.new(records).process
    when 'pdf'
      HTML.new(records).process unless formatters.include?('html')
      Pdf.new(records).process
    when 'json'
      JSON.new(records).process
    else
      begin
        klass = Object.const_get(fmt)
        unless klass.public_instance_methods.include?(:process)
          raise "Formatter #{fmt} should respond to `process` method"
        end
        klass.new(records).process
      rescue NameError => e
        if e.message =~ /(uninitialized constant|wrong constant name) #{fmt}$/
          raise "Formatter #{fmt} does not exists"
        else
          raise e
        end
      end
    end
  end
end