Class: Lazylead::Memes

Inherits:
Object
  • Object
show all
Defined in:
lib/lazylead/task/accuracy/memes.rb

Overview

TODO:

#339/DEV Prepare the meme list and attach them to the github, section .docs/accuracy/memes As url they will be required for configuration of the each task.

Meme generator based on tickets accuracy. For each range of scores there might be several available memes to be chosen randomly. If no meme found for score, empty url should be provided.

Instance Method Summary collapse

Constructor Details

#initialize(memes, log: Log.new) ⇒ Memes

Returns a new instance of Memes.



37
38
39
40
# File 'lib/lazylead/task/accuracy/memes.rb', line 37

def initialize(memes, log: Log.new)
  @log = log
  @memes = memes
end

Instance Method Details

#enabled?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/lazylead/task/accuracy/memes.rb', line 42

def enabled?
  !@memes.nil? && !range.empty?
end

#find(score) ⇒ Object

Detect meme url based on accuracy score.

Parameters:

  • score

    Accuracy score in percentage



49
50
51
52
53
# File 'lib/lazylead/task/accuracy/memes.rb', line 49

def find(score)
  r = range.find { |m| score >= m[0] && score <= m[1] }
  return "" if r.nil?
  r.last.sample
end

#rangeObject

Parse json-based memes configuration. Expected format is

"memes": {
  "0-9.9": "https://meme.com?id=awful1.gif,https://meme.com?id=awful2.gif",
  "70-89.9": "https://meme.com?id=nice.gif",
  "90-100": "https://meme.com?id=wow.gif"
}


62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/lazylead/task/accuracy/memes.rb', line 62

def range
  @range ||= if @memes.nil?
               []
             else
               rng = JSON.parse(@memes).to_h
               return [] if rng.empty?
               rng.map do |k, v|
                 next unless k.include? "-"
                 row = k.split("-").map(&:to_f)
                 row << v.split(",")
                 row
               end
             end
end