Class: Otori::Signals

Inherits:
Object
  • Object
show all
Defined in:
lib/otori/signals.rb

Constant Summary collapse

KEYS =
{
  mouse: "m",
  touch: "t",
  scroll: "s",
  keyboard: "k",
  focus: "f"
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(flags = {}) ⇒ Signals

Returns a new instance of Signals.



28
29
30
# File 'lib/otori/signals.rb', line 28

def initialize(flags = {})
  @flags = KEYS.keys.to_h { [_1, flags.fetch(_1, false) == true] }
end

Class Method Details

.from_json(json) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/otori/signals.rb', line 15

def self.from_json(json)
  raw = JSON.parse(json.to_s)
  raise JSON::ParserError, "expected an object" unless raw.is_a?(Hash)

  new(KEYS.transform_values { raw[_1] == true })
rescue JSON::ParserError
  new
end

.human_rating(json) ⇒ Object



24
25
26
# File 'lib/otori/signals.rb', line 24

def self.human_rating(json)
  from_json(json).human_rating
end

Instance Method Details

#human_ratingObject



36
37
38
# File 'lib/otori/signals.rb', line 36

def human_rating
  @flags.values.count(true) / KEYS.size.to_f
end

#to_hObject



40
# File 'lib/otori/signals.rb', line 40

def to_h = @flags.dup