Class: DateHolidays::Reader::JsBridge

Inherits:
Object
  • Object
show all
Defined in:
lib/date_holidays/reader/js_bridge.rb

Overview

A communication bridge to the JavaScript process which houses the date-holidays node module.

Constant Summary collapse

BIN_PATH =
File.expand_path('../../../bin', __dir__).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = Config.default, debug: false) ⇒ JsBridge

TODO: initialize with a configuration object for how to run the command



23
24
25
26
27
28
# File 'lib/date_holidays/reader/js_bridge.rb', line 23

def initialize(config = Config.default, debug: false)
  @config = config
  @debug = debug

  freeze
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



20
21
22
# File 'lib/date_holidays/reader/js_bridge.rb', line 20

def config
  @config
end

#debugObject (readonly)

Returns the value of attribute debug.



20
21
22
# File 'lib/date_holidays/reader/js_bridge.rb', line 20

def debug
  @debug
end

Instance Method Details

#extract(sub_cmd, *args) ⇒ Object



30
31
32
# File 'lib/date_holidays/reader/js_bridge.rb', line 30

def extract(sub_cmd, *args)
  JSON.parse(get_output(holidays_to_json_command + [sub_cmd, *args]))
end

#get_output(args) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/date_holidays/reader/js_bridge.rb', line 34

def get_output(args)
  args = Array(args)
  cmd_tokens_as_strings = args.map(&:to_s)
  output = nil

  print_command(cmd_tokens_as_strings) if debug

  IO.popen(cmd_tokens_as_strings, err: %i[child out]) { |cmd_io| output = cmd_io.read }
  output
end