Class: HaveAPI::OutputFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/haveapi/output_formatter.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.formattersObject (readonly)

Returns the value of attribute formatters.



7
8
9
# File 'lib/haveapi/output_formatter.rb', line 7

def formatters
  @formatters
end

Class Method Details

.register(klass) ⇒ Object



9
10
11
12
# File 'lib/haveapi/output_formatter.rb', line 9

def register(klass)
  @formatters ||= []
  @formatters << klass
end

Instance Method Details

#content_typeObject



42
43
44
# File 'lib/haveapi/output_formatter.rb', line 42

def content_type
  @formatter.content_type
end

#error(msg) ⇒ Object



38
39
40
# File 'lib/haveapi/output_formatter.rb', line 38

def error(msg)
  @formatter.format(header(false, nil, msg))
end

#format(status, response, message = nil, errors = nil, version: true) ⇒ Object



34
35
36
# File 'lib/haveapi/output_formatter.rb', line 34

def format(status, response, message = nil, errors = nil, version: true)
  @formatter.format(header(status, response, message, errors, version))
end

#supports?(types) ⇒ Boolean

Returns:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/haveapi/output_formatter.rb', line 15

def supports?(types)
  @formatter = nil

  if types.empty?
    return @formatter = self.class.formatters.first.new
  end

  types.each do |type|
    self.class.formatters.each do |f|
      if f.handle?(type)
        @formatter = f.new
        break
      end
    end
  end

  !@formatter.nil?
end