# frozen_string_literal: true module StrikeMe module API # Factory helper object connecting the client to the models class Invoices include BaseClient include Enumerable autoload_default :Invoice autoload_default :Quote delegate :first, :last, :size, :any?, to: :list def each(&block) list.each(&block) end def list return @list if @list.present? url = 'invoices' response = client.get url raise ResponseError.new(url, response.body) unless response.success? @list = response.body['items'].map { |item| Invoice.new item } end def paid select(&:paid?) end def find(id) url = "invoices/#{id}" response = client.get url raise ResponseError.new(url, response.body) unless response.success? Invoice.new response.body end # POST invoices — amount is a decimal string per Strike's own API # (not a float/BTC value), matching what #list/#find already hand # back untouched in Invoice#amount. def create(amount:, currency:, description: nil, correlation_id: nil) url = 'invoices' body = { amount: { amount: amount, currency: currency }, description: description, correlationId: correlation_id }.compact response = client.post(url, body) raise ResponseError.new(url, response.body) unless response.success? Invoice.new response.body end # POST invoices/{id}/quote — the second step of actually getting a # payable BOLT 11 (Invoice#create alone only reserves the invoice # shell; nothing is payable until a quote exists). Quotes expire # (Quote#expiration) — callers request a fresh one per payment # attempt rather than caching. def quote(id, description_hash: nil) url = "invoices/#{id}/quote" body = { descriptionHash: description_hash }.compact response = client.post(url, body) raise ResponseError.new(url, response.body) unless response.success? Quote.new response.body end end end end