Class: Fal::Price

Inherits:
Object
  • Object
show all
Defined in:
lib/fal/price.rb

Overview

Represents pricing information for a model endpoint. Fetches data from the Platform API at /models and /models/pricing.

Defined Under Namespace

Modules: Unit

Constant Summary collapse

MODELS_PATH =
"/models"
PRICING_PATH =
"/models/pricing"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes, client: Fal.client) ⇒ Price

Returns a new instance of Price.

Parameters:

  • attributes (Hash)

    Raw attributes from pricing API

  • client (Fal::Client) (defaults to: Fal.client)


34
35
36
37
# File 'lib/fal/price.rb', line 34

def initialize(attributes, client: Fal.client)
  @client = client
  reset_attributes(attributes)
end

Instance Attribute Details

#currencyString (readonly)

Returns:

  • (String)


30
31
32
# File 'lib/fal/price.rb', line 30

def currency
  @currency
end

#endpoint_idString (readonly)

Returns:

  • (String)


24
25
26
# File 'lib/fal/price.rb', line 24

def endpoint_id
  @endpoint_id
end

#unitString (readonly)

Returns:

  • (String)


28
29
30
# File 'lib/fal/price.rb', line 28

def unit
  @unit
end

#unit_priceFloat (readonly)

Returns:

  • (Float)


26
27
28
# File 'lib/fal/price.rb', line 26

def unit_price
  @unit_price
end

Class Method Details

.all(client: Fal.client) ⇒ Array<Fal::Price>

Return an array of all prices.

Parameters:

Returns:



76
77
78
79
80
# File 'lib/fal/price.rb', line 76

def all(client: Fal.client)
  results = []
  each(client: client) { |price| results << price }
  results
end

.each(client: Fal.client) {|Fal::Price| ... } ⇒ void

This method returns an undefined value.

Iterate over all prices by paging through models and fetching pricing in batches.

Parameters:

Yields:



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/fal/price.rb', line 54

def each(client: Fal.client, &block)
  cursor = nil
  loop do
    models_response = client.get_api(MODELS_PATH, query: { limit: 50, cursor: cursor }.compact)
    models = Array(models_response && models_response["models"])
    endpoint_ids = models.map { |m| m["endpoint_id"] }.compact

    if endpoint_ids.any?
      pricing_response = client.get_api(PRICING_PATH, query: { endpoint_id: endpoint_ids })
      Array(pricing_response && pricing_response["prices"]).each do |attributes|
        block.call(new(attributes, client: client))
      end
    end

    cursor = models_response && models_response["next_cursor"]
    break if cursor.nil?
  end
end

.find_by(endpoint_id:, client: Fal.client) ⇒ Fal::Price?

Find pricing for a specific model endpoint.

Parameters:

  • endpoint_id (String)
  • client (Fal::Client) (defaults to: Fal.client)

Returns:



44
45
46
47
48
# File 'lib/fal/price.rb', line 44

def find_by(endpoint_id:, client: Fal.client)
  response = client.get_api(PRICING_PATH, query: { endpoint_id: endpoint_id })
  entry = Array(response && response["prices"]).find { |p| p["endpoint_id"] == endpoint_id }
  entry ? new(entry, client: client) : nil
end