Class: Drntest::JSONLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/drntest/json-loader.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeJSONLoader

Returns a new instance of JSONLoader.



34
35
36
37
38
39
40
# File 'lib/drntest/json-loader.rb', line 34

def initialize
  @parser = Yajl::Parser.new
  @objects = []
  @parser.on_parse_complete = lambda do |object|
    @objects << object
  end
end

Instance Attribute Details

#objectsObject (readonly)

Returns the value of attribute objects.



32
33
34
# File 'lib/drntest/json-loader.rb', line 32

def objects
  @objects
end

Class Method Details

.report_error(path, data, error) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/drntest/json-loader.rb', line 21

def report_error(path, data, error)
  marker = "-" * 60
  puts("Failed to load JSONs file: #{path}")
  puts(marker)
  puts(data)
  puts(marker)
  puts(error)
  puts(marker)
end

Instance Method Details

#<<(data) ⇒ Object



42
43
44
# File 'lib/drntest/json-loader.rb', line 42

def <<(data)
  @parser << data
end

#load(path) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/drntest/json-loader.rb', line 46

def load(path)
  path.open do |file|
    data = ""
    file.each_line do |line|
      data << line
      begin
        self << line
      rescue Yajl::ParseError => error
        self.class.report_error(path, data, error)
        break
      end
    end
  end
  @objects
end