Class: Lookbook::DataParser

Inherits:
Service
  • Object
show all
Defined in:
lib/lookbook/services/data/parsers/data_parser.rb

Direct Known Subclasses

JsonParser, YamlParser

Instance Method Summary collapse

Methods inherited from Service

call

Constructor Details

#initialize(input, fail_silently: false, fallback: nil) ⇒ DataParser

Returns a new instance of DataParser.



3
4
5
6
7
# File 'lib/lookbook/services/data/parsers/data_parser.rb', line 3

def initialize(input, fail_silently: false, fallback: nil)
  @input = input
  @fail_silently = fail_silently
  @fallback = fallback
end

Instance Method Details

#callObject



9
10
11
12
13
14
# File 'lib/lookbook/services/data/parsers/data_parser.rb', line 9

def call
  result = @input.present? ? parse(@input) : @fallback
  result.is_a?(Hash) ? result.deep_symbolize_keys : result
rescue => exception
  @fail_silently ? @fallback : raise(exception)
end