Class: Deploio::PriceFetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/deploio/price_fetcher.rb

Constant Summary collapse

CACHE_DIR =
File.expand_path("~/.deploio")
CACHE_FILE =
File.join(CACHE_DIR, "prices.json")
CACHE_TTL =

24 hours in seconds

24 * 60 * 60
API_URL =
"https://calculator-api-production.2deb129.deploio.app/product"

Instance Method Summary collapse

Constructor Details

#initializePriceFetcher

Returns a new instance of PriceFetcher.



15
16
17
# File 'lib/deploio/price_fetcher.rb', line 15

def initialize
  @prices = nil
end

Instance Method Details

#fetchObject



19
20
21
# File 'lib/deploio/price_fetcher.rb', line 19

def fetch
  @prices ||= load_cached_prices || fetch_and_cache_prices
end

#format_price(price) ⇒ Object



62
63
64
65
66
# File 'lib/deploio/price_fetcher.rb', line 62

def format_price(price)
  return "-" if price.nil?

  "CHF #{price}/mo"
end

#price_for_app(app_data) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/deploio/price_fetcher.rb', line 41

def price_for_app(app_data)
  fetch
  return nil unless @prices

  spec = app_data["spec"] || app_data
  status = app_data["status"] || {}
  for_provider = spec["forProvider"] || {}
  at_provider = status["atProvider"] || {}
  config = for_provider["config"] || {}

  size = (config["size"] || "micro").downcase
  # Use status replicas (actual running) if available, otherwise spec replicas, default to 1
  replicas = at_provider["replicas"] || for_provider["replicas"] || 1
  replicas = replicas.to_i

  size_price = @prices.dig("app", size)
  return nil if size_price.nil?

  size_price * replicas
end

#price_for_service(type, spec) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/deploio/price_fetcher.rb', line 23

def price_for_service(type, spec)
  fetch
  return nil unless @prices

  case type
  when "postgres", "mysql"
    price_for_database(type, spec)
  when "postgresdatabases", "mysqldatabases"
    price_for_single_database
  when "keyvaluestore"
    price_for_keyvaluestore(spec)
  when "opensearch"
    price_for_opensearch
  when "bucket"
    price_for_bucket
  end
end