Class: Threatinator::Parsers::JSON::Adapters::Oj

Inherits:
Oj::ScHandler
  • Object
show all
Defined in:
lib/threatinator/parsers/json/adapters/oj.rb

Instance Method Summary collapse

Constructor Details

#initializeOj

Returns a new instance of Oj.



7
8
9
10
# File 'lib/threatinator/parsers/json/adapters/oj.rb', line 7

def initialize
  @root = nil
  @depth = 0
end

Instance Method Details

#array_append(a, v) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/threatinator/parsers/json/adapters/oj.rb', line 52

def array_append(a,v)
  if @depth == 1
    do_callback(v)
  else 
    a << v
  end
end

#array_endObject



60
61
62
# File 'lib/threatinator/parsers/json/adapters/oj.rb', line 60

def array_end
  @depth -= 1
end

#array_startObject



45
46
47
48
49
50
# File 'lib/threatinator/parsers/json/adapters/oj.rb', line 45

def array_start
  ret = []
  @depth += 1
  @root = ret if @root.nil?
  ret
end

#do_callback(data, key = nil) ⇒ Object



21
22
23
# File 'lib/threatinator/parsers/json/adapters/oj.rb', line 21

def do_callback(data, key = nil)
  @callback.call(data, key: key)
end

#hash_endObject



41
42
43
# File 'lib/threatinator/parsers/json/adapters/oj.rb', line 41

def hash_end
  @depth -= 1
end

#hash_set(h, k, v) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/threatinator/parsers/json/adapters/oj.rb', line 32

def hash_set(h,k,v)
  if @depth == 1
    do_callback(v, k)
  else 
    h[k] = v
  end
  v
end

#hash_startObject



25
26
27
28
29
30
# File 'lib/threatinator/parsers/json/adapters/oj.rb', line 25

def hash_start
  ret = {}
  @depth += 1
  @root = ret if @root.nil?
  ret
end

#run(io, &callback) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/threatinator/parsers/json/adapters/oj.rb', line 12

def run(io, &callback)
  @callback = callback
  begin
    ::Oj.sc_parse(self, io)
  rescue ::Oj::ParseError => e
    raise Threatinator::Exceptions::ParseError.new(e)
  end
end