Class: ProconBypassMan::ExternalInput::ExternalData
- Inherits:
-
Object
- Object
- ProconBypassMan::ExternalInput::ExternalData
- Defined in:
- lib/procon_bypass_man/external_input/external_data.rb
Constant Summary collapse
- UNPRESS_BUTTONS =
Set.new(ProconBypassMan::Procon::ButtonCollection.available.map { |x| "un#{x}".to_sym })
Instance Attribute Summary collapse
-
#hex ⇒ String, NilClass
readonly
16進数表現のデータ.
-
#raw_data ⇒ String, NilClass
readonly
ログに表示する用.
Class Method Summary collapse
- .is_json(raw_data) ⇒ Boolean
-
.parse!(raw_data) ⇒ ExternalData
JSON か カンマ区切りのbuttons.
Instance Method Summary collapse
-
#buttons ⇒ Array<Symbol>
NOTE: ログに表示する用.
-
#initialize(hex:, buttons:, raw_data: nil) ⇒ ExternalData
constructor
A new instance of ExternalData.
- #press_buttons ⇒ Array<Symbol>
- #to_binary ⇒ String, NilClass
- #unpress_buttons ⇒ Array<Symbol>
Constructor Details
#initialize(hex:, buttons:, raw_data: nil) ⇒ ExternalData
Returns a new instance of ExternalData.
38 39 40 41 42 |
# File 'lib/procon_bypass_man/external_input/external_data.rb', line 38 def initialize(hex: , buttons: , raw_data: nil) @hex = hex @buttons = || [] @raw_data = raw_data end |
Instance Attribute Details
#hex ⇒ String, NilClass (readonly)
Returns 16進数表現のデータ.
7 8 9 |
# File 'lib/procon_bypass_man/external_input/external_data.rb', line 7 def hex @hex end |
#raw_data ⇒ String, NilClass (readonly)
Returns ログに表示する用.
10 11 12 |
# File 'lib/procon_bypass_man/external_input/external_data.rb', line 10 def raw_data @raw_data end |
Class Method Details
.is_json(raw_data) ⇒ Boolean
31 32 33 |
# File 'lib/procon_bypass_man/external_input/external_data.rb', line 31 def self.is_json(raw_data) raw_data.start_with?('{') end |
.parse!(raw_data) ⇒ ExternalData
Returns JSON か カンマ区切りのbuttons.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/procon_bypass_man/external_input/external_data.rb', line 14 def self.parse!(raw_data) raise ParseError unless raw_data.ascii_only? if is_json(raw_data) begin json = JSON.parse(raw_data) return new(hex: json['hex'], buttons: json['buttons']) rescue JSON::ParserError raise ParseError end end return new(hex: nil, buttons: raw_data.scan(/:\w+:/).map { |x| x.gsub(':', '') }, raw_data: raw_data) end |
Instance Method Details
#buttons ⇒ Array<Symbol>
NOTE: ログに表示する用
66 67 68 |
# File 'lib/procon_bypass_man/external_input/external_data.rb', line 66 def @buttons.map(&:to_sym) end |
#press_buttons ⇒ Array<Symbol>
51 52 53 54 55 |
# File 'lib/procon_bypass_man/external_input/external_data.rb', line 51 def .select do || ProconBypassMan::Procon::ButtonCollection::BUTTONS_MAP[] end end |
#to_binary ⇒ String, NilClass
45 46 47 48 |
# File 'lib/procon_bypass_man/external_input/external_data.rb', line 45 def to_binary return nil if @hex.nil? [@hex].pack('H*') end |
#unpress_buttons ⇒ Array<Symbol>
58 59 60 61 62 |
# File 'lib/procon_bypass_man/external_input/external_data.rb', line 58 def .select { || UNPRESS_BUTTONS.include?() }.map { |b| (b.to_s).to_sym } end |