Class: Threatinator::Parsers::JSON::Parser

Inherits:
Threatinator::Parser show all
Defined in:
lib/threatinator/parsers/json/parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Threatinator::Parser

#eql?

Constructor Details

#initialize(opts = {}) ⇒ Parser

Returns a new instance of Parser.



8
9
10
11
# File 'lib/threatinator/parsers/json/parser.rb', line 8

def initialize(opts = {})
  @adapter_class = self.class.adapter_class
  super(opts)
end

Class Method Details

.adapter_classObject

Detects the platform, loads the JSON adapter, and returns the adapter’s class.



15
16
17
18
19
20
21
22
23
24
# File 'lib/threatinator/parsers/json/parser.rb', line 15

def self.adapter_class
  if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby'
    #:nocov:
    raise "JSON parsing not supported for JRuby"
    #:nocov:
  else 
    require 'threatinator/parsers/json/adapters/oj'
    return Threatinator::Parsers::JSON::Adapters::Oj
  end
end

Instance Method Details

#==(other) ⇒ Object



26
27
28
# File 'lib/threatinator/parsers/json/parser.rb', line 26

def ==(other)
  super(other)
end

#run(io) {|record| ... } ⇒ Object

Parameters:

  • io (IO)

Yields:

  • (record)

    Gives one line to the block

Yield Parameters:



33
34
35
36
37
38
39
40
# File 'lib/threatinator/parsers/json/parser.rb', line 33

def run(io)
  adapter = @adapter_class.new
  callback = lambda do |object, opts = {}|
    yield Threatinator::Parsers::JSON::Record.new(object, opts)
  end
  adapter.run(io, &callback)
  nil
end