summaryrefslogtreecommitdiff
path: root/tests/ruby.rb
blob: 004d516fd032d34a985a09d8a2ccc7bbd8e60015 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class App < Struct.new(:id, :name, :os, :user_agent, :plans, :serial_options, keyword_init: true)
  extend ActiveModel::Translation

  class AppNotFound < StandardError; end
  class PlanNotFound < StandardError; end

  def initialize(*args)
    super

    self.plans.map! { |plan| Plan.new(**plan) }
    self.serial_options = SerialOptions.new(**self.serial_options)
  end

  def icon
    "apps/#{id}.png"
  end

  def find_plan(id)
    plans.detect { |plan| plan.id == id } || (raise PlanNotFound)
  end

  def download_link
    "https://services.panini.house/apps/#{id}/latest_releases"
  end

  def download_link_for(serial_number)
    "#{download_link}?serial_number=#{serial_number}"
  end

  def marketing_list
    Sendy.lists.optional[id]
  end

  def checkout_recovery_list
    Sendy.lists.optional["#{id}_checkout_recovery"]
  end

  def self.all
    template = ERB.new File.read(Rails.root.join("app", "data", "apps.yml.erb"))

    YAML.load(template.result)
      .map(&:with_indifferent_access)
      .map { |app| new(**app) }
  end

  def self.find(id)
    all.find { |app| app.id == id } || (raise AppNotFound)
  end
end