Class: Helu

Inherits:
Object
  • Object
show all
Defined in:
lib/project/product_info_fetcher.rb,
lib/project/helu.rb

Overview

# Sample use: ## Asynchronous, raw: inapps = %w[first second third] end

## Asynchronous, wrapped in method call: Helu::ProductInfoFetcher.fetch(inapps) do |pi|

p pi

end

## Synchronous: pi = Helu::ProductInfoFetcher.fetch(inapps) p pi

# All three calls return hash of the following form: # { # “inapp_id”: { # id: “inapp_id”, # title: “inapp_localized_title”, # description: “inapp_localized_description”, # price: “0.89”, # float # currency: “EUR”, # price_str: “u20AC0.89”, # }, # # … # }

Defined Under Namespace

Classes: LocalStorage, ProductInfoFetcher

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(product_id) ⇒ Helu

Returns a new instance of Helu.



13
14
15
16
17
# File 'lib/project/helu.rb', line 13

def initialize(product_id)
  @product_id = product_id
  SKPaymentQueue.defaultQueue.addTransactionObserver(self)
  @storage = LocalStorage.new
end

Instance Attribute Details

#failObject

Returns the value of attribute fail.



4
5
6
# File 'lib/project/helu.rb', line 4

def fail
  @fail
end

#product_idObject (readonly)

Returns the value of attribute product_id.



3
4
5
# File 'lib/project/helu.rb', line 3

def product_id
  @product_id
end

#restoreObject

Returns the value of attribute restore.



4
5
6
# File 'lib/project/helu.rb', line 4

def restore
  @restore
end

#storageObject

Returns the value of attribute storage.



4
5
6
# File 'lib/project/helu.rb', line 4

def storage
  @storage
end

#winningObject

Returns the value of attribute winning.



4
5
6
# File 'lib/project/helu.rb', line 4

def winning
  @winning
end

Class Method Details

.fetch_product_info(*products, &b) ⇒ Object

see product_info_fetcher.rb for info



8
9
10
# File 'lib/project/helu.rb', line 8

def fetch_product_info(*products, &b)
  ProductInfoFetcher.fetch(*products, &b)
end

Instance Method Details

#bought?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/project/helu.rb', line 32

def bought?
  @storage.all.include?(product_id)
end

#buyObject



19
20
21
22
# File 'lib/project/helu.rb', line 19

def buy
  payment = SKPayment.paymentWithProductIdentifier(product_id)
  SKPaymentQueue.defaultQueue.addPayment(payment)
end

#closeObject



28
29
30
# File 'lib/project/helu.rb', line 28

def close
  SKPaymentQueue.defaultQueue.removeTransactionObserver(self)
end

#completeTransaction(transaction) ⇒ Object



46
47
48
# File 'lib/project/helu.rb', line 46

def completeTransaction(transaction)
  finishTransaction(transaction,wasSuccessful:true)
end

#failedTransaction(transaction) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/project/helu.rb', line 54

def failedTransaction(transaction)
  if transaction.error && (transaction.error.code != SKErrorPaymentCancelled)
    finishTransaction(transaction, wasSuccessful:false)
  elsif transaction.error && (transaction.error.code == SKErrorPaymentCancelled)
    @fail.call(transaction)
  else
    SKPaymentQueue.defaultQueue.finishTransaction(transaction)
  end
end

#finishTransaction(transaction, wasSuccessful: wasSuccessful) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/project/helu.rb', line 36

def finishTransaction(transaction, wasSuccessful:wasSuccessful)
  SKPaymentQueue.defaultQueue.finishTransaction(transaction)
  if wasSuccessful 
    @winning.call(transaction)
    storage.add(product_id)
  else
    @fail.call(transaction)
  end
end

#paymentQueue(queue, updatedTransactions: transactions) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/project/helu.rb', line 64

def paymentQueue(queue,updatedTransactions:transactions)
  transactions.each do |transaction|
    if transaction.payment.productIdentifier == product_id
      case transaction.transactionState
        when SKPaymentTransactionStatePurchased
          completeTransaction(transaction)
        when SKPaymentTransactionStateFailed
          failedTransaction(transaction)
        when SKPaymentTransactionStateRestored
          restoreTransaction(transaction)
        else 
      end
    end
  end
end

#paymentQueueRestoreCompletedTransactionsFinished(queue) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/project/helu.rb', line 80

def paymentQueueRestoreCompletedTransactionsFinished(queue)
  ids = []
  queue.transactions.each do |transaction|
    product_id = transaction.payment.productIdentifier
    ids << product_id
  end
  @restore.call if ids.uniq.include? product_id
end

#restoreTransaction(transaction) ⇒ Object



50
51
52
# File 'lib/project/helu.rb', line 50

def restoreTransaction(transaction)
  finishTransaction(transaction,wasSuccessful:true)
end