Class: Portage::Cli::Buy

Inherits:
Object
  • Object
show all
Defined in:
lib/portage/cli/buy.rb

Overview

portage buy <url> — the single entrypoint for "get this thing bought", regardless of whether the target store speaks native UCP, only offers a catalog, or doesn't speak UCP at all but happens to run a platform we have an adapter (and this process's own credentials) for.

Never tries to buy as an anonymous shopper via scraping/session-hijacking (ToS violation, explicitly ruled out), and never falls back to an adapter unless this process already has that platform's own env vars set — i.e. it's your own store, or one you're integrated with, never a stranger's. See docs/design-log.md for the reasoning behind it.

Defined Under Namespace

Classes: PermissiveAuthenticator

Constant Summary collapse

CART_CAP =
"dev.ucp.shopping.cart".freeze
CHECKOUT_CAP =
"dev.ucp.shopping.checkout".freeze
REDIRECT_LIMIT =
5

Instance Method Summary collapse

Constructor Details

#initialize(url:, query:, qty: 1, payment_token: nil, yes: false, dry_run: false, product_id: nil) ⇒ Buy

Returns a new instance of Buy.

Parameters:

  • product_id (String, nil) (defaults to: nil)

    buy exactly this product instead of whatever the catalog search happens to rank first — how portage find hands a picked offer over without the ranking being guessed twice.



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/portage/cli/buy.rb', line 28

def initialize(url:, query:, qty: 1, payment_token: nil, yes: false, dry_run: false, product_id: nil)
  raw = url.to_s.strip
  raw = "https://#{raw}" unless raw =~ %r{\Ahttps?://}i
  @uri = URI.parse(raw)
  @query = query
  @qty = qty
  @payment_token = payment_token
  @yes = yes
  @dry_run = dry_run
  @product_id = product_id
end

Instance Method Details

#callObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/portage/cli/buy.rb', line 40

def call
  session = discover(@uri)
  return native_flow(session) if session

  body, headers = fetch_homepage(@uri)

  linked = manifest_link(body)
  if linked
    session = discover(linked)
    return native_flow(session) if session
  end

  platform = Portage::Ucp::Resolver.detect_platform(body, headers)
  adapter_flow(platform) || dead_end
end