Class: PurchaseKit::Product

Inherits:
Object
  • Object
show all
Defined in:
lib/purchasekit/product.rb,
lib/purchasekit/product/demo.rb,
lib/purchasekit/product/remote.rb

Overview

Represents a product configured in the PurchaseKit dashboard.

Products contain the store-specific product IDs for Apple and Google. Display text (name, description, price) should be fetched from the stores at runtime or defined in your views for i18n support.

Example:

product = PurchaseKit::Product.find("prod_XXXXX")
product.apple_product_id  # => "com.example.pro.annual"
product.google_product_id # => "pro_annual"

Defined Under Namespace

Classes: Demo, Remote

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, apple_product_id: nil, google_product_id: nil, google_base_plan_id: nil) ⇒ Product

Returns a new instance of Product.



16
17
18
19
20
21
# File 'lib/purchasekit/product.rb', line 16

def initialize(id:, apple_product_id: nil, google_product_id: nil, google_base_plan_id: nil)
  @id = id
  @apple_product_id = apple_product_id
  @google_product_id = google_product_id
  @google_base_plan_id = google_base_plan_id
end

Instance Attribute Details

#apple_product_idObject (readonly)

Returns the value of attribute apple_product_id.



14
15
16
# File 'lib/purchasekit/product.rb', line 14

def apple_product_id
  @apple_product_id
end

#google_base_plan_idObject (readonly)

Returns the value of attribute google_base_plan_id.



14
15
16
# File 'lib/purchasekit/product.rb', line 14

def google_base_plan_id
  @google_base_plan_id
end

#google_product_idObject (readonly)

Returns the value of attribute google_product_id.



14
15
16
# File 'lib/purchasekit/product.rb', line 14

def google_product_id
  @google_product_id
end

#idObject (readonly)

Returns the value of attribute id.



14
15
16
# File 'lib/purchasekit/product.rb', line 14

def id
  @id
end

Class Method Details

.find(id) ⇒ Product

Find a product by ID.

In demo mode, reads from configured demo_products. In production, fetches from the PurchaseKit API.

Parameters:

  • id (String)

    The product ID (e.g., “prod_XXXXX” or a demo key)

Returns:

Raises:



45
46
47
48
49
50
51
# File 'lib/purchasekit/product.rb', line 45

def self.find(id)
  if PurchaseKit.config.demo_mode?
    Demo.find(id)
  else
    Remote.find(id)
  end
end

Instance Method Details

#store_product_id(platform:) ⇒ String

Get the store-specific product ID for a platform.

Parameters:

  • platform (Symbol)

    :apple or :google

Returns:

  • (String)

    The store product ID



28
29
30
31
32
33
34
# File 'lib/purchasekit/product.rb', line 28

def store_product_id(platform:)
  case platform
  when :apple then apple_product_id
  when :google then google_product_id
  else raise ArgumentError, "Unknown platform: #{platform}"
  end
end