itunes_receipt_validator 0.0.3 → 0.0.4

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: 8b77b13bb8f2873dbee2a213fd62d354f4fedbde
4
- data.tar.gz: eb68ad8842b921cbbcb4f02d688db66eb7ba78f5
3
+ metadata.gz: 59447ec52b6230c6b2615a9a915acddf433bdb5b
4
+ data.tar.gz: 1dcf1d374181b0f55ac86080eea1d351061800d4
5
5
  SHA512:
6
- metadata.gz: 96134027c767ab5c262377750b0f1a92e330a1917056dfba6a433b08cfabab0767c7dc39695e64cfafc02a80635555dd4f1d9ce2609ad8da9de776313d06c774
7
- data.tar.gz: 3bce5284cf4ef617120923c604acd2d2dcddee596cde29bc93ba58a981117f1ef8381195ce6b80dc18107af5c5774df863129d90438f535999e972d6658a687d
6
+ metadata.gz: 1b3109ff7b381d1e711f32245281273a6158a1604ef1161b7ea389fdd979cca183ebd302f05933a6473a2cd67aa2cb82c838ca70362e012a4a230fca0a89f500
7
+ data.tar.gz: 0ac47696ebda71a9a291429a0117f2a8c930e475c30f29c3cf1c46e8ce283ea81c7c196e447a8b5d6f374eddc9d76b94fa4a70ae9bbc531f418142bab9c258b6
@@ -6,6 +6,35 @@ module ItunesReceiptValidator
6
6
  ##
7
7
  # ItunesReceiptValidator::Transaction
8
8
  class Transaction
9
+ ATTR_MAP = {
10
+ id: :transaction_id,
11
+ original_id: :original_transaction_id,
12
+ product_id: :product_id,
13
+ quantity: :quantity,
14
+ web_order_line_item_id: proc do |hash|
15
+ hash[:web_order_line_item_id].to_s if
16
+ hash.fetch(:web_order_line_item_id, 0).to_i > 0
17
+ end,
18
+ trial_period: ->(hash) { hash[:is_trial_period] == 'true' },
19
+ purchased_at: proc do |hash|
20
+ parse_date(hash[:purchase_date_ms] || hash[:purchase_date])
21
+ end,
22
+ first_purchased_at: proc do |hash|
23
+ parse_date hash[:original_purchase_date_ms] ||
24
+ hash[:original_purchase_date]
25
+ end,
26
+ cancelled_at: proc do |hash|
27
+ parse_date(hash[:cancelled_date_ms] || hash[:cancelled_date])
28
+ end,
29
+ expires_at: proc do |hash|
30
+ if hash[:bid]
31
+ parse_date(hash[:expires_date] || hash[:expires_date_formatted])
32
+ else
33
+ parse_date(hash[:expires_date_ms] || hash[:expires_date])
34
+ end
35
+ end
36
+ }
37
+
9
38
  attr_reader :id, :original_id, :product_id, :quantity, :first_purchased_at,
10
39
  :purchased_at, :expires_at, :cancelled_at,
11
40
  :web_order_line_item_id, :trial_period, :receipt
@@ -39,26 +68,29 @@ module ItunesReceiptValidator
39
68
 
40
69
  private
41
70
 
42
- def convert_ms(ms)
43
- return nil if ms.nil?
44
- Time.at(ms.to_f / 1000).utc
45
- end
71
+ attr_writer :id, :original_id, :product_id, :quantity,
72
+ :web_order_line_item_id, :trial_period, :purchased_at,
73
+ :first_purchased_at, :cancelled_at, :expires_at
46
74
 
47
75
  def normalize(hash)
48
- @id = hash.fetch(:transaction_id)
49
- @original_id = hash.fetch(:original_transaction_id)
50
- @product_id = hash.fetch(:product_id)
51
- @quantity = hash.fetch(:quantity)
52
- @purchased_at = convert_ms hash.fetch(:purchase_date_ms)
53
- @first_purchased_at = convert_ms hash.fetch(:original_purchase_date_ms)
54
- @cancelled_at = convert_ms hash.fetch(:cancelled_date_ms, nil)
55
- @web_order_line_item_id = hash.fetch(:web_order_line_item_id, nil)
56
- @trial_period = hash.fetch(:is_trial_period, nil) == 'true'
57
- if hash[:bid]
58
- @expires_at = convert_ms hash.fetch(:expires_date, nil)
76
+ ATTR_MAP.each do |key, val|
77
+ if val.is_a?(Proc)
78
+ send("#{key}=".to_s, instance_exec(hash, &val))
79
+ else
80
+ send("#{key}=".to_s, hash[val])
81
+ end
82
+ end
83
+ end
84
+
85
+ def parse_date(date)
86
+ return nil if date.nil?
87
+ if date.is_a?(Integer) || (date =~ /^\d+$/) == 0
88
+ Time.at(date.to_f / 1000).utc
59
89
  else
60
- @expires_at = convert_ms hash.fetch(:expires_date_ms, nil)
90
+ Time.strptime(date, '%F %T Etc/%Z').utc
61
91
  end
92
+ rescue StandardError => _e
93
+ nil
62
94
  end
63
95
  end
64
96
  end
@@ -3,5 +3,5 @@
3
3
  module ItunesReceiptValidator
4
4
  ##
5
5
  # Gem version
6
- VERSION = '0.0.3'
6
+ VERSION = '0.0.4'
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: itunes_receipt_validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - mbaasy.com
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-07 00:00:00.000000000 Z
11
+ date: 2015-12-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: itunes_receipt_decoder