Sha256: f22cd5e3df91b11668dcb03f4203ecc212c6013d17827795d135c13b0d540314

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

module GalaxyConverter
  class Note
    PREFIXES = ["how much is", "how many credits is"]
    QUESTION = "?"
    NO_IDEA = "I have no idea what you are talking about"

    def self.from(notes)
      Array(notes).map { |body| new(body) }
    end

    attr_reader :body, :units, :good

    def initialize(body)
      @body = body.to_s.strip.downcase
      @units, @good = detect
    end

    def question?
      @body.end_with?(QUESTION)
    end

    def commercial?
      !!@body.index("credits")
    end

    def answer(total = 0)
      return NO_IDEA if total.zero?
      [].tap do |s|
        s << units
        s << good.to_s.capitalize
        s << "is"
        s << "%g" % total
        s << "Credits" if commercial?
      end.reject(&:empty?).join(" ")
    end

    private def detect
      tokens = stripped.split
      return [tokens.join(" "), nil] unless commercial?
      good = tokens.pop
      [tokens.join(" "), good]
    end

    private def stripped
      @body.sub(/#{PREFIXES.join("|")}/, "").sub(QUESTION, "").strip
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
galaxy_converter-3.0.1 lib/galaxy_converter/note.rb