itunes-receipt-mock 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7adc738d9fac2d63cf39275cc7dfe88e9fc793a6
4
- data.tar.gz: 778c95fc0e66fe9964a69bdf618056d74d5501fc
3
+ metadata.gz: e89959f9b704e3139a602239387361f6aa7a8b98
4
+ data.tar.gz: e237a12123a3c5193b951359c79df514a0e89812
5
5
  SHA512:
6
- metadata.gz: 34cca824abfcfadc5b7f6be8bda9d654381c7f909857c4bcddd5ca2320b1d5f5f05045385c431ae73c8ffe0cfb7a43b3e2d3a5f1f2d288990e820ea722d56f0b
7
- data.tar.gz: 15b52051edcd807605abb864d6af519769346feaaf0dcd0b8e70e30e13e8503e47c6853796c8a5b6c090c8b8097717b60a32638a4c94a3c48b8b01ca9d45a615
6
+ metadata.gz: f21216a625c2226f9cc7f7f011e797f7fc03294f5c698361edc83993e90dcb23840bacb9115640bda5b54a9823a0d9d7430c0e4f64ad2da3f0de36af6e3d4278
7
+ data.tar.gz: 506df8e5fb160fbacbf0172090cabb76d32310461cede025613380ebbae220053f58ce5020582af325fc0f6803b718d3e6732230d510464e2fc96453ea298816
@@ -11,7 +11,7 @@ module ItunesReceiptMock
11
11
  class MissingArgumentError < StandardError; end
12
12
 
13
13
  @transaction_id = 1_000_000_000
14
-
14
+ @web_order_line_item_id = 1_000_000_000
15
15
  ##
16
16
  # Creates a new iTunes receipt
17
17
  #
@@ -26,4 +26,8 @@ module ItunesReceiptMock
26
26
  def self.next_transaction_id
27
27
  @transaction_id += 1
28
28
  end
29
+
30
+ def self.next_web_order_line_item_id
31
+ @web_order_line_item_id += 1
32
+ end
29
33
  end
@@ -6,6 +6,12 @@ module ItunesReceiptMock
6
6
  module Mixins
7
7
  private
8
8
 
9
+ def send_defaults(defaults, options)
10
+ defaults.each do |k, v|
11
+ send "#{k}=", options.fetch(k, v.class == Proc ? instance_eval(&v) : v)
12
+ end
13
+ end
14
+
9
15
  def date_attrs(prefix, date)
10
16
  {
11
17
  "#{prefix}_date" => date.utc.strftime('%F %T') + ' Etc/GMT',
@@ -6,23 +6,23 @@ module ItunesReceiptMock
6
6
  class Purchase
7
7
  include ItunesReceiptMock::Mixins
8
8
 
9
+ PURCHASE_DEFAULTS = {
10
+ product_id: nil,
11
+ quantity: 1,
12
+ transaction_id: proc { ItunesReceiptMock.next_transaction_id },
13
+ original_transaction_id: proc { transaction_id },
14
+ purchase_date: proc { Time.now },
15
+ original_purchase_date: proc { purchase_date }
16
+ }
17
+
9
18
  attr_accessor :quantity, :product_id, :transaction_id,
10
19
  :original_transaction_id, :purchase_date,
11
20
  :original_purchase_date
12
21
 
13
22
  def initialize(options = {})
14
- @product_id = options.fetch :product_id, nil
23
+ send_defaults(PURCHASE_DEFAULTS, options)
15
24
  fail ItunesReceiptMock::MissingArgumentError,
16
25
  'product_id is required' unless @product_id
17
- @quantity = options.fetch :quantity, 1
18
- @transaction_id =
19
- options.fetch :transaction_id,
20
- ItunesReceiptMock.next_transaction_id.to_s
21
- @original_transaction_id =
22
- options.fetch :original_transaction_id, @transaction_id
23
- @purchase_date = options.fetch :purchase_date, Time.now
24
- @original_purchase_date =
25
- options.fetch :original_purchase_date, @purchase_date
26
26
  end
27
27
 
28
28
  def result(_options = {})
@@ -6,6 +6,18 @@ module ItunesReceiptMock
6
6
  class Receipt
7
7
  include ItunesReceiptMock::Mixins
8
8
 
9
+ RECEIPT_DEFAULTS = {
10
+ bundle_id: nil,
11
+ environment: 'Production',
12
+ adam_id: 1,
13
+ app_item_id: 1,
14
+ application_version: 1,
15
+ download_id: 1,
16
+ version_external_identifier: 1,
17
+ original_purchase_date: proc { Time.now },
18
+ original_application_version: 1
19
+ }
20
+
9
21
  attr_reader :in_app
10
22
  attr_accessor :environment, :adam_id, :app_item_id, :bundle_id,
11
23
  :application_version, :download_id,
@@ -14,32 +26,22 @@ module ItunesReceiptMock
14
26
 
15
27
  def initialize(options = {})
16
28
  @in_app = {}
17
- @bundle_id = options.fetch :bundle_id, nil
18
- @environment = options.fetch :environment, 'Production'
19
- @adam_id = options.fetch :adam_id, 0
20
- @app_item_id = options.fetch :app_item_id, 0
21
- @application_version = options.fetch :application_version, '1'
22
- @download_id = options.fetch :download_id, 0
23
- @version_external_identifier =
24
- options.fetch :version_external_identifier, 0
25
- @original_purchase_date = options.fetch :original_purchase_date, Time.now
26
- @original_application_version =
27
- options.fetch :original_application_version, 0
28
-
29
+ send_defaults(RECEIPT_DEFAULTS, options)
29
30
  fail MissingArgumentError, 'bundle_id is required' unless @bundle_id
30
31
  end
31
32
 
32
33
  def result(options = {})
33
- request_date = options[:request_date] = Time.now
34
+ request_date = options.fetch :request_date, Time.now
34
35
  {
35
36
  'receipt_type' => environment,
36
- 'adam_id' => adam_id,
37
- 'app_item_id' => app_item_id,
37
+ 'adam_id' => adam_id.to_i,
38
+ 'app_item_id' => app_item_id.to_i,
38
39
  'bundle_id' => bundle_id,
39
- 'application_version' => application_version,
40
- 'download_id' => download_id,
41
- 'version_external_identifier' => version_external_identifier,
42
- 'original_application_version' => original_application_version,
40
+ 'application_version' => application_version.to_s,
41
+ 'download_id' => download_id.to_i,
42
+ 'version_external_identifier' => version_external_identifier.to_i,
43
+ 'original_application_version' =>
44
+ format('%.1f', original_application_version),
43
45
  'in_app' => in_app_result(options)
44
46
  }
45
47
  .merge(date_attrs('request', request_date))
@@ -4,24 +4,27 @@ module ItunesReceiptMock
4
4
  ##
5
5
  # ItunesReceiptMock::Subscription
6
6
  class Subscription < Purchase
7
+ SUBSCRIPTION_DEFAULTS = {
8
+ expires_date: nil,
9
+ web_order_line_item_id: proc do
10
+ ItunesReceiptMock.next_web_order_line_item_id
11
+ end,
12
+ is_trial_period: false
13
+ }
14
+
7
15
  attr_accessor :expires_date, :web_order_line_item_id, :is_trial_period
8
16
 
9
17
  def initialize(options = {})
10
18
  super
11
- @expires_date = options.fetch :expires_date, nil
12
- @web_order_line_item_id =
13
- options.fetch :web_order_line_item_id,
14
- rand(1_000_000_000..9_999_999_999).to_s
15
- @is_trial_period = options.fetch :is_trial_period, false
16
-
19
+ send_defaults(SUBSCRIPTION_DEFAULTS, options)
17
20
  fail MissingArgumentError, 'expires_date is required' unless @expires_date
18
21
  end
19
22
 
20
23
  def result(_options = {})
21
24
  super.merge(
22
- 'web_order_line_item_id' => web_order_line_item_id,
25
+ 'web_order_line_item_id' => web_order_line_item_id.to_s,
23
26
  'is_trial_period' => is_trial_period
24
- ).merge(date_attrs('expires_date', expires_date))
27
+ ).merge(date_attrs('expires', expires_date))
25
28
  end
26
29
  end
27
30
  end
@@ -1,5 +1,5 @@
1
1
  ##
2
2
  # ItunesReceiptMock::VERSION
3
3
  module ItunesReceiptMock
4
- VERSION = '0.0.4'
4
+ VERSION = '0.0.5'
5
5
  end
@@ -32,15 +32,109 @@ describe ItunesReceiptMock do
32
32
  purchase_date: 1.month.ago,
33
33
  expires_date: Time.now
34
34
  end
35
+ let(:in_app_purchase_json) do
36
+ subject['receipt']['in_app'].detect do |t|
37
+ t['transaction_id'] == purchase.transaction_id
38
+ end
39
+ end
40
+ let(:latest_receipt_info_purchase_json) do
41
+ subject['latest_receipt_info'].detect do |t|
42
+ t['transaction_id'] == purchase.transaction_id
43
+ end
44
+ end
45
+ let(:in_app_subscription_json) do
46
+ subject['receipt']['in_app'].detect do |t|
47
+ t['transaction_id'] == subscription.transaction_id
48
+ end
49
+ end
50
+ let(:latest_receipt_info_subscription_json) do
51
+ subject['latest_receipt_info'].detect do |t|
52
+ t['transaction_id'] == subscription.transaction_id
53
+ end
54
+ end
35
55
 
36
56
  subject { validation.result }
37
57
 
38
- it 'contains the purchases in the receipt.in_app object' do
39
- expect(validation.receipt.in_app.length).to eq(2)
58
+ shared_examples 'a date' do
59
+ it 'displays the date in GMT' do
60
+ expect(json[prefix + '_date'])
61
+ .to eq(date.utc.strftime('%F %T') + ' Etc/GMT')
62
+ end
63
+
64
+ it 'displays the date as a ms timestamp' do
65
+ expect(json[prefix + '_date_ms'])
66
+ .to eq(date.utc.strftime('%s%L').to_i)
67
+ end
68
+
69
+ it 'displays the date in PST' do
70
+ expect(json[prefix + '_date_pst'])
71
+ .to eq(date.getlocal('-08:00').strftime('%F %T') +
72
+ ' America/Los_Angeles')
73
+ end
74
+ end
75
+
76
+ shared_examples 'a purchase json object' do
77
+ it 'contains all the details for a purchase' do
78
+ expect(json['product_id']).to eq(obj.product_id)
79
+ expect(json['quantity']).to eq(obj.quantity)
80
+ expect(json['original_transaction_id'])
81
+ .to eq(obj.original_transaction_id)
82
+ end
83
+
84
+ describe 'purchase date' do
85
+ let(:date) { obj.purchase_date }
86
+ let(:prefix) { 'purchase' }
87
+ it_behaves_like 'a date'
88
+ end
89
+
90
+ describe 'original purchase date' do
91
+ let(:date) { obj.original_purchase_date }
92
+ let(:prefix) { 'original_purchase' }
93
+ it_behaves_like 'a date'
94
+ end
95
+ end
96
+
97
+ shared_examples 'a subscription json object' do
98
+ it_behaves_like 'a purchase json object'
99
+
100
+ it 'contains all the details for a subscription' do
101
+ expect(json['web_order_line_item_id'])
102
+ .to eq(obj.web_order_line_item_id.to_s)
103
+ expect(json['is_trial_period']).to eq(obj.is_trial_period)
104
+ end
105
+
106
+ describe 'expires date' do
107
+ let(:date) { obj.expires_date }
108
+ let(:prefix) { 'expires' }
109
+ it_behaves_like 'a date'
110
+ end
111
+ end
112
+
113
+ it 'contains all the purchases in the receipt.in_app object' do
114
+ expect(subject['receipt']['in_app'].length).to eq(2)
115
+ end
116
+
117
+ it 'contains all the purchases in the latest_receipt_info object' do
118
+ expect(subject['latest_receipt_info'].length).to eq(2)
119
+ end
120
+
121
+ it_behaves_like 'a purchase json object' do
122
+ let(:obj) { purchase }
123
+ let(:json) { in_app_purchase_json }
124
+ end
125
+
126
+ it 'contains the same object for the purchase in latest_receipt_info' do
127
+ expect(in_app_purchase_json).to eq(latest_receipt_info_purchase_json)
128
+ end
129
+
130
+ it_behaves_like 'a subscription json object' do
131
+ let(:obj) { subscription }
132
+ let(:json) { in_app_subscription_json }
40
133
  end
41
134
 
42
- it 'contains the purchases in the latest_receipt_info object' do
43
- expect(validation.latest_receipt_info.length).to eq(2)
135
+ it 'contains the same object for the subscription in latest_receipt_info' do
136
+ expect(in_app_subscription_json)
137
+ .to eq(latest_receipt_info_subscription_json)
44
138
  end
45
139
 
46
140
  context 'renewing the subscription' do
@@ -50,7 +144,7 @@ describe ItunesReceiptMock do
50
144
  end
51
145
 
52
146
  it 'contains two subscription transactions in latest_receipt_info' do
53
- expect(validation.latest_receipt_info.length).to eq(3)
147
+ expect(subject['latest_receipt_info'].length).to eq(3)
54
148
  end
55
149
  end
56
150
  end
@@ -21,11 +21,11 @@ describe ItunesReceiptMock::Receipt do
21
21
 
22
22
  it 'defaults everything else' do
23
23
  expect(subject.environment).to eq('Production')
24
- expect(subject.adam_id).to eq(0)
25
- expect(subject.app_item_id).to eq(0)
26
- expect(subject.application_version).to eq('1')
27
- expect(subject.download_id).to eq(0)
28
- expect(subject.version_external_identifier).to eq(0)
24
+ expect(subject.adam_id).to eq(1)
25
+ expect(subject.app_item_id).to eq(1)
26
+ expect(subject.application_version).to eq(1)
27
+ expect(subject.download_id).to eq(1)
28
+ expect(subject.version_external_identifier).to eq(1)
29
29
  expect(subject.original_purchase_date).to eq(Time.now)
30
30
  end
31
31
  end
@@ -134,21 +134,19 @@ describe ItunesReceiptMock::Receipt do
134
134
  expect(subject['adam_id']).to eq(receipt.adam_id)
135
135
  expect(subject['app_item_id']).to eq(receipt.app_item_id)
136
136
  expect(subject['bundle_id']).to eq(receipt.bundle_id)
137
- expect(subject['application_version']).to eq(receipt.application_version)
137
+ expect(subject['application_version'])
138
+ .to eq(receipt.application_version.to_s)
138
139
  expect(subject['download_id']).to eq(receipt.download_id)
139
- expect(subject['version_external_identifier']).to eq(
140
- receipt.version_external_identifier
141
- )
142
- expect(subject['original_application_version']).to eq(
143
- receipt.original_application_version
144
- )
140
+ expect(subject['version_external_identifier'])
141
+ .to eq(receipt.version_external_identifier)
142
+ expect(subject['original_application_version'])
143
+ .to eq(format('%.1f', receipt.original_application_version))
145
144
  expect(subject['in_app']).to be_a(Array)
146
- expect(subject['request_date']).to eq(
147
- Time.now.utc.strftime('%F %T') + ' Etc/GMT'
148
- )
149
- expect(subject['original_purchase_date']).to eq(
150
- receipt.original_purchase_date.utc.strftime('%F %T') + ' Etc/GMT'
151
- )
145
+ expect(subject['request_date'])
146
+ .to eq(Time.now.utc.strftime('%F %T') + ' Etc/GMT')
147
+ expect(subject['original_purchase_date'])
148
+ .to eq(receipt.original_purchase_date.utc.strftime('%F %T') +
149
+ ' Etc/GMT')
152
150
  end
153
151
 
154
152
  context 'when "request_date" is in options' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: itunes-receipt-mock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - mbaasy.com
@@ -137,7 +137,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
137
137
  requirements:
138
138
  - - ">="
139
139
  - !ruby/object:Gem::Version
140
- version: '0'
140
+ version: 1.9.2
141
141
  required_rubygems_version: !ruby/object:Gem::Requirement
142
142
  requirements:
143
143
  - - ">="