Class: DateHolidays::Reader::JsBridge
- Inherits:
-
Object
- Object
- DateHolidays::Reader::JsBridge
- 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.('../../../bin', __dir__).freeze
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#debug ⇒ Object
readonly
Returns the value of attribute debug.
Instance Method Summary collapse
- #extract(sub_cmd, *args) ⇒ Object
- #get_output(args) ⇒ Object
-
#initialize(config = Config.default, debug: false) ⇒ JsBridge
constructor
TODO: initialize with a configuration object for how to run the command.
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
#config ⇒ Object (readonly)
Returns the value of attribute config.
20 21 22 |
# File 'lib/date_holidays/reader/js_bridge.rb', line 20 def config @config end |
#debug ⇒ Object (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 |