helu 0.4.1 → 0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +14 -3
  3. data/lib/project/helu.rb +63 -14
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 479a6c67dade5a2f04d2f1f37c3ec9716317cd53
4
- data.tar.gz: c4c3b119ad109f3f00afb10b2ea013e0464d9d1d
3
+ metadata.gz: 63367011c2bac9119e14a1eec6a5b2216406181b
4
+ data.tar.gz: 55cdd3775667b53eb18ab3549bec8d10a03f64c1
5
5
  SHA512:
6
- metadata.gz: 071d7ecd6e3bab16b8ed8d144da28c7d6dd3f28a92060bf36a2c7445967f7c39e198b63d0b08cc047b780a99f2a402d5dcc6b33ee7c9e9896a50a455e0a07942
7
- data.tar.gz: 4412caaa34e3440cbd1b5fc6b5c99e1833f4e345b8033a44d1b2b7cfd60d37ad7c0f6767f973d82f89d0e7d7a2b2fbcb7a7768e4f870e37317f616f9185e52cf
6
+ metadata.gz: 309593bf1d36c34ca54c1754e7d1eedba3039871f5f068b2aff12ddac6201cb57f6d4b9c17b2c9d2db4d001f411d95436d4ebb6657f2f1c966410ec7a6edf992
7
+ data.tar.gz: 58b695a813139495f55fd1de3222d17bf4dfca018e8f9a8915891251a099d8dac78c95b61506d7041f6145463512da33ee08e2ccf81555988bfa5b77e8a990b3
data/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # helu
2
2
 
3
+ [![Build Status](https://travis-ci.org/ivanacostarubio/helu.png)](https://travis-ci.org/ivanacostarubio/helu)
4
+
5
+
3
6
  ## Installation
4
7
 
5
8
  Add this line to your application's Gemfile:
@@ -26,19 +29,27 @@ Start a helu with the In App Purchase ID:
26
29
  @helu.fail = lambda { |transaction| puts transaction ; # node here for failed in app purchase }
27
30
  @helu.winning = lambda { |transaction| puts transaction ; # code here for successful in app purchase }
28
31
 
32
+ #### also for restoring in app purchases
33
+
34
+ @helu.restore = lambda { restoring_method }
35
+
29
36
 
30
37
  The transaction object on the lambda is the one we get from Apple; Therefore, it is a SKPaymentTransaction. [More information about it here](http://developer.apple.com/library/ios/#documentation/StoreKit/Reference/SKPaymentTransaction_Class/Reference/Reference.html)
31
38
 
32
39
 
33
40
 
34
- #### buy the product:
41
+ #### buy the product:
35
42
 
36
43
  @helu.buy
37
44
 
45
+ #### restore the product:
46
+
47
+ @helu.restore
48
+
38
49
 
39
- Make sure that if your code ever throws out the Helu object, it better also close the store before doing so.
50
+ ### Make sure that if your code ever throws out the Helu object, it better also close the store before doing so.
40
51
 
41
- @helu.close_the_store
52
+ @helu.close
42
53
 
43
54
 
44
55
  ## Example App:
@@ -1,28 +1,29 @@
1
1
  class Helu
2
2
 
3
3
  attr_reader :product_id
4
+ attr_accessor :storage, :winning, :restore, :fail
4
5
 
5
6
  def initialize(product_id)
6
7
  @product_id = product_id
7
8
  SKPaymentQueue.defaultQueue.addTransactionObserver(self)
9
+ @storage = LocalStorage.new
8
10
  end
9
11
 
10
- def close_the_store
11
- SKPaymentQueue.defaultQueue.removeTransactionObserver(self)
12
+ def buy
13
+ payment = SKPayment.paymentWithProductIdentifier(product_id)
14
+ SKPaymentQueue.defaultQueue.addPayment(payment)
12
15
  end
13
16
 
14
- def fail=(fail_block)
15
- @fail = fail_block
17
+ def restore
18
+ SKPaymentQueue.defaultQueue.restoreCompletedTransactions
16
19
  end
17
20
 
18
- def winning=(winning_block)
19
- @winning = winning_block
21
+ def close
22
+ SKPaymentQueue.defaultQueue.removeTransactionObserver(self)
20
23
  end
21
24
 
22
-
23
- def buy
24
- payment = SKPayment.paymentWithProductIdentifier(product_id)
25
- SKPaymentQueue.defaultQueue.addPayment(payment)
25
+ def bought?
26
+ @storage.all.include?(product_id)
26
27
  end
27
28
 
28
29
  # private
@@ -30,7 +31,12 @@ class Helu
30
31
  def finishTransaction(transaction, wasSuccessful:wasSuccessful)
31
32
  SKPaymentQueue.defaultQueue.finishTransaction(transaction)
32
33
  produt_id = transaction.payment.productIdentifier
33
- wasSuccessful ? @winning.call(transaction) : @fail.call(transaction)
34
+ if wasSuccessful
35
+ @winning.call(transaction)
36
+ storage.add(product_id)
37
+ else
38
+ @fail.call(transaction)
39
+ end
34
40
  end
35
41
 
36
42
  def completeTransaction(transaction)
@@ -38,7 +44,6 @@ class Helu
38
44
  end
39
45
 
40
46
  def restoreTransaction(transaction)
41
- recordTransaction(transaction.originalTransaction)
42
47
  finishTransaction(transaction,wasSuccessful:true)
43
48
  end
44
49
 
@@ -56,7 +61,7 @@ class Helu
56
61
 
57
62
  def paymentQueue(queue,updatedTransactions:transactions)
58
63
  transactions.each do |transaction|
59
- if transaction.payment.productIdentifier == @product_id
64
+ if transaction.payment.productIdentifier == product_id
60
65
  case transaction.transactionState
61
66
  when SKPaymentTransactionStatePurchased
62
67
  completeTransaction(transaction)
@@ -70,4 +75,48 @@ class Helu
70
75
  end
71
76
  end
72
77
 
73
- end
78
+ def paymentQueueRestoreCompletedTransactionsFinished(queue)
79
+ ids = []
80
+ queue.transactions.each do |transaction|
81
+ product_id = transaction.payment.productIdentifier
82
+ ids << product_id
83
+ end
84
+ @restore.call if ids.uniq.include? product_id
85
+ end
86
+
87
+
88
+ class LocalStorage
89
+
90
+ def clean
91
+ defaults.setObject(nil, forKey: key_for_defaults)
92
+ defaults.synchronize
93
+ end
94
+
95
+ def add(product_id)
96
+ if all
97
+ defaults.setObject([all, product_id].flatten, forKey: key_for_defaults)
98
+ else
99
+ defaults.setObject([product_id], forKey: key_for_defaults)
100
+ end
101
+
102
+ defaults.synchronize
103
+ end
104
+
105
+ def all
106
+ return [] if defaults.valueForKey(key_for_defaults) == nil
107
+ defaults.valueForKey(key_for_defaults)
108
+ end
109
+
110
+ private
111
+
112
+ def key_for_defaults
113
+ "helu_products"
114
+ end
115
+
116
+ def defaults
117
+ NSUserDefaults.standardUserDefaults
118
+ end
119
+
120
+ end
121
+
122
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: helu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: '0.5'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ivan Acosta-Rubio
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-10 00:00:00.000000000 Z
11
+ date: 2013-07-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake