Vibescript Showcase

Shipping quote

Quote delivery windows with enum-based shipping speeds, money totals, and Duration math.

Showcase
Source showcase/commerce/shipping_quote.vibe
# title: Shipping quote
# category: Vibescript Showcase
# difficulty: Showcase
# summary: Quote delivery windows with enum-based shipping speeds, money totals, and Duration math.
# description: This example combines three of the language's strongest semantic features in one realistic flow: enums for intent, money for totals, and durations for delivery windows.
# tags: commerce, money, durations, enums
# featured: true
# feature_rank: 3
# vibe: 0.2

enum ShippingSpeed
  Economy
  Priority
  SameDay
end

def transit_time(speed: ShippingSpeed) -> duration
  if speed == ShippingSpeed::Economy
    5.days
  elsif speed == ShippingSpeed::Priority
    2.days
  else
    6.hours
  end
end

def shipping_fee(speed: ShippingSpeed, subtotal: money) -> money
  if speed == ShippingSpeed::Economy && subtotal >= money("100.00 USD")
    money("0.00 USD")
  elsif speed == ShippingSpeed::Economy
    money("6.00 USD")
  elsif speed == ShippingSpeed::Priority
    money("18.00 USD")
  else
    money("35.00 USD")
  end
end

def shipping_quote(speed: ShippingSpeed, subtotal: money, purchased_at: time) -> hash
  fee = shipping_fee(speed, subtotal)
  transit = transit_time(speed)
  eta = transit.after(purchased_at)

  {
    speed: speed.name,
    subtotal: subtotal,
    fee: fee,
    total: subtotal + fee,
    transit: transit.iso8601,
    eta: eta.format("2006-01-02T15:04:05Z")
  }
end

def run
  purchased_at = Time.at(1700000000)
  {
    economy: shipping_quote(ShippingSpeed::Economy, money("125.00 USD"), purchased_at),
    same_day: shipping_quote(ShippingSpeed::SameDay, money("48.00 USD"), purchased_at)
  }
end
Output
Press run to execute run from this example.
showcase idiomatic-vibescript commerce money durations enums browser-runner