Class: Lurker::Json::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/lurker/json/reader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Reader

Returns a new instance of Reader.



6
7
8
9
# File 'lib/lurker/json/reader.rb', line 6

def initialize(path)
  @path = path
  @attempts_left = 1
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



4
5
6
# File 'lib/lurker/json/reader.rb', line 4

def path
  @path
end

Instance Method Details

#readObject Also known as: payload



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/lurker/json/reader.rb', line 11

def read
  return YAML.load_file(@path) unless @path.match(/\.erb$/)

  context = Lurker::ErbSchemaContext.new
  erb = ERB.new(IO.read @path).result(context.get_binding)
  YAML.load(erb)
rescue Errno::ENOENT
  raise if @attempts_left.zero?

  @path = @path.sub(/\#\/?$/, '').sub(/\.json/, '.json.yml')
  @attempts_left -= 1

  retry
end