iap_verifier 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d89828b0eed3faefdafd855ad7de9e956a098557
4
- data.tar.gz: f1fec54b3ebbcc1aa7dad8538062e73e3cdcd3fe
3
+ metadata.gz: 12b8929ff939efa3791342dea19cb317a0a7fc85
4
+ data.tar.gz: 106416c9890832238e63870e29561eed5a451a3c
5
5
  SHA512:
6
- metadata.gz: d37bf0595895be84ae763af57d5d7fd4f7829b2b0f76695ab35e839f0dc1803de41ed6c443476b85408111e8c20cd4d38516660239f5ebcfe88b4d42493a468c
7
- data.tar.gz: 8e376237522bd58407ebfeca8a549529e7beba250977f019a47f3fb8008c7b081c54f6934416bf304b36761571146702f85c222f6cdd748bd706fa4a0852b581
6
+ metadata.gz: 3e5ed21ace584601597c4446e9a559af34ea8b24cd907024ed0fb2f475d792e39b55424f7d5147c779cca2b261fd373de8496f70146125288001513477a3aba1
7
+ data.tar.gz: 6556e1ab3f7397dc8fbf8e796ae58f7c273b120f5755d50abc993d8c836b1c53f7f9266b104ad07326023e7c623a81407e03d560a338cc9e72049cdb8c5f343a
@@ -2,13 +2,27 @@
2
2
 
3
3
  ## master
4
4
 
5
+ ## v0.3.0
6
+
7
+ Improvement
8
+
9
+ * Handle malformed response from Apple's server.
10
+ * Remove external dependencies (Oj and HTTPClient).
11
+
5
12
  ## v0.2.0
6
13
 
14
+ Fix
15
+
7
16
  * Fixes OpenSSL::SSL::SSLError [#1](https://github.com/linjunpop/iap_verifier/pull/1)
17
+
18
+ Improvement
19
+
8
20
  * curb is not thread-safe, [httpclient](https://github.com/nahi/httpclient) does.
9
21
 
10
22
  ## v0.1.0
11
23
 
24
+ Improvement
25
+
12
26
  * Use [oj](https://github.com/ohler55/oj) to load JSON.
13
27
  * Use [curb](https://github.com/taf2/curb) as http client.
14
28
  * Support ruby 1.9 & 2.0.
data/README.md CHANGED
@@ -1,7 +1,6 @@
1
1
  # IAPVerifier
2
2
 
3
3
  [![Build Status](https://travis-ci.org/linjunpop/iap_verifier.svg)](https://travis-ci.org/linjunpop/iap_verifier)
4
- [![Dependency Status](http://img.shields.io/gemnasium/linjunpop/iap_verifier.svg)](https://gemnasium.com/linjunpop/iap_verifier)
5
4
  [![Code Climate](http://img.shields.io/codeclimate/github/linjunpop/iap_verifier.svg)](https://codeclimate.com/github/linjunpop/iap_verifier)
6
5
  [![Gem Version](http://img.shields.io/gem/v/iap_verifier.svg)](https://rubygems.org/gems/iap_verifier)
7
6
  [![License](http://img.shields.io/:license-mit-blue.svg)](http://linjunpop.mit-license.org)
@@ -18,9 +18,6 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_runtime_dependency 'httpclient'
22
- spec.add_runtime_dependency 'oj'
23
-
24
21
  spec.add_development_dependency "bundler", "~> 1.6"
25
22
  spec.add_development_dependency "rake"
26
23
  spec.add_development_dependency "pry"
@@ -1,7 +1,6 @@
1
1
  require "iap_verifier/version"
2
2
  require 'iap_verifier/error'
3
3
  require 'iap_verifier/request'
4
- require 'iap_verifier/request_data'
5
4
  require 'iap_verifier/response_data'
6
5
  require 'iap_verifier/receipt'
7
6
 
@@ -18,17 +18,17 @@ module IAPVerifier
18
18
  end
19
19
  end
20
20
 
21
- class MalformedReceiptData < Standard
21
+ class MalformedResponseData < Standard
22
22
  def initialize(data)
23
23
  @data = data
24
24
  end
25
25
 
26
26
  def message
27
- "Malformed Receipt data: #{@data}"
27
+ "Malformed response from Apple Server: #{data}"
28
28
  end
29
29
  end
30
30
 
31
- class InvalidResponseData < Standard
31
+ class InvalidReceiptData < Standard
32
32
  MESSAGES = {
33
33
  21000 => "App store could not read",
34
34
  21002 => "Data was malformed",
@@ -3,10 +3,6 @@ module IAPVerifier
3
3
  def initialize(response_data)
4
4
  @receipt_data = response_data['receipt']
5
5
 
6
- unless @receipt_data.is_a?(Hash)
7
- raise Error::MalformedReceiptData
8
- end
9
-
10
6
  @receipt_data.each do |key, value|
11
7
  define_singleton_method key.to_s, -> { value }
12
8
  end
@@ -1,7 +1,3 @@
1
- require 'json'
2
- require 'httpclient'
3
- require 'oj'
4
-
5
1
  module IAPVerifier
6
2
  class Request
7
3
  PRODUCTION_URL = "https://buy.itunes.apple.com/verifyReceipt"
@@ -12,7 +8,9 @@ module IAPVerifier
12
8
  raise Error::EmptyReceipt.new
13
9
  end
14
10
 
15
- @request_data = RequestData.new(receipt)
11
+ @request_data = {
12
+ 'receipt-data' => receipt
13
+ }
16
14
  end
17
15
 
18
16
  def response
@@ -32,15 +30,21 @@ module IAPVerifier
32
30
  end
33
31
 
34
32
  def verify(request_data, url)
35
- http = HTTPClient.new
36
- http.ssl_config.ssl_version = :TLSv1
37
- response = http.post(
38
- url,
39
- body: request_data.to_json,
40
- header: {'Content-Type' => "application/json", 'Accept' => 'application/json'}
41
- )
42
-
43
- ResponseData.new(Oj.load(response.content))
33
+ uri = URI(url)
34
+ http = Net::HTTP.new(uri.host, uri.port)
35
+ http.use_ssl = true
36
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
37
+
38
+ request = Net::HTTP::Post.new(uri.request_uri)
39
+
40
+ request['Accept'] = "application/json"
41
+ request['Content-Type'] = "application/json"
42
+
43
+ request.body = request_data.to_json
44
+
45
+ response = http.request(request)
46
+
47
+ ResponseData.new(response.body)
44
48
  end
45
49
  end
46
50
  end
@@ -1,10 +1,16 @@
1
+ require 'json'
2
+
1
3
  module IAPVerifier
2
4
  class ResponseData
3
- SUCCESS_RESPONSE_CODE = 0
4
- SANDBOX_RECEIPT_CODE = 21007
5
+ SUCCESS_RESPONSE_CODE = 0.freeze
6
+ SANDBOX_RECEIPT_CODE = 21007.freeze
7
+
8
+ def initialize(json)
9
+ @response_data = JSON.parse(json)
5
10
 
6
- def initialize(response_data)
7
- @response_data = response_data
11
+ raise Error::MalformedResponseData.new(json) unless @response_data.is_a?(Hash)
12
+ rescue JSON::ParserError
13
+ raise Error::MalformedResponseData.new(json)
8
14
  end
9
15
 
10
16
  def valid?
@@ -19,7 +25,7 @@ module IAPVerifier
19
25
  if valid?
20
26
  Receipt.new(@response_data)
21
27
  else
22
- raise Error::InvalidResponseData.new(status_code)
28
+ raise Error::InvalidReceiptData.new(status_code)
23
29
  end
24
30
  end
25
31
 
@@ -1,3 +1,3 @@
1
1
  module IAPVerifier
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -5,14 +5,14 @@ require 'iap_verifier/error'
5
5
 
6
6
  class ResponseDataTest < MiniTest::Test
7
7
  def test_success_response_json
8
- success_response_json = { 'status' => 0, 'receipt' => { } }
8
+ success_response_json = { 'status' => 0, 'receipt' => { } }.to_json
9
9
  response_data = IAPVerifier::ResponseData.new(success_response_json)
10
10
 
11
11
  assert_equal true, response_data.valid?
12
12
  end
13
13
 
14
14
  def test_error_response_json
15
- error_response_json = { 'status' => 21002 }
15
+ error_response_json = { 'status' => 21002 }.to_json
16
16
  response_data = IAPVerifier::ResponseData.new(error_response_json)
17
17
 
18
18
  assert_equal false, response_data.valid?
@@ -20,7 +20,7 @@ class ResponseDataTest < MiniTest::Test
20
20
 
21
21
  def test_sandbox_response_json
22
22
  # sandbox receipt sent to production server
23
- sandbox_response_json = { 'status' => 21007 }
23
+ sandbox_response_json = { 'status' => 21007 }.to_json
24
24
  response_data = IAPVerifier::ResponseData.new(sandbox_response_json)
25
25
 
26
26
  assert_equal false, response_data.valid?
@@ -28,17 +28,22 @@ class ResponseDataTest < MiniTest::Test
28
28
  end
29
29
 
30
30
  def test_receipt
31
- response_data = IAPVerifier::ResponseData.new({ 'receipt' => { } })
31
+ response_data = IAPVerifier::ResponseData.new({ 'receipt' => { } }.to_json)
32
32
  response_data.stubs(:valid?).returns(true)
33
33
 
34
34
  assert_equal true, response_data.receipt.instance_of?(IAPVerifier::Receipt)
35
35
  end
36
36
 
37
- def test_invalid_response_data
38
- assert_raises(IAPVerifier::Error::InvalidResponseData) do
39
- response_data = IAPVerifier::ResponseData.new({ 'receipt' => { } })
40
- response_data.stubs(:valid?).returns(false)
37
+ def test_malformed_response_data
38
+ assert_raises(IAPVerifier::Error::MalformedResponseData) do
39
+ IAPVerifier::ResponseData.new('')
40
+ end
41
+ end
41
42
 
43
+ def text_invalid_receipt_data
44
+ assert_raises(IAPVerifier::Error::InvalidReceiptData) do
45
+ IAPVerifier::ResponseData.new({ 'receipt' => { } }.to_json)
46
+ response_data.stubs(:valid?).returns(false)
42
47
  response_data.receipt
43
48
  end
44
49
  end
metadata CHANGED
@@ -1,43 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iap_verifier
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jun Lin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-30 00:00:00.000000000 Z
11
+ date: 2015-01-29 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: httpclient
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: oj
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '0'
41
13
  - !ruby/object:Gem::Dependency
42
14
  name: bundler
43
15
  requirement: !ruby/object:Gem::Requirement
@@ -155,13 +127,11 @@ files:
155
127
  - lib/iap_verifier/error.rb
156
128
  - lib/iap_verifier/receipt.rb
157
129
  - lib/iap_verifier/request.rb
158
- - lib/iap_verifier/request_data.rb
159
130
  - lib/iap_verifier/response_data.rb
160
131
  - lib/iap_verifier/version.rb
161
132
  - test/fixtures/base64_receipt
162
133
  - test/fixtures/vcr_cassettes/valid_receipt.yml
163
134
  - test/receipt_test.rb
164
- - test/request_data_test.rb
165
135
  - test/request_test.rb
166
136
  - test/response_data_test.rb
167
137
  - test/test_helper.rb
@@ -193,7 +163,6 @@ test_files:
193
163
  - test/fixtures/base64_receipt
194
164
  - test/fixtures/vcr_cassettes/valid_receipt.yml
195
165
  - test/receipt_test.rb
196
- - test/request_data_test.rb
197
166
  - test/request_test.rb
198
167
  - test/response_data_test.rb
199
168
  - test/test_helper.rb
@@ -1,17 +0,0 @@
1
- module IAPVerifier
2
- class RequestData
3
- def initialize(receipt)
4
- @receipt = receipt
5
- end
6
-
7
- def to_h
8
- {
9
- 'receipt-data' => @receipt
10
- }
11
- end
12
-
13
- def to_json
14
- to_h.to_json
15
- end
16
- end
17
- end
@@ -1,13 +0,0 @@
1
- require 'test_helper'
2
- require 'iap_verifier/request_data'
3
-
4
- class RequestDataTest < MiniTest::Test
5
- def test_base64_receipt
6
- receipt = File.read(File.expand_path("../fixtures/base64_receipt", __FILE__))
7
-
8
- expected = {
9
- 'receipt-data' => receipt
10
- }
11
- assert_equal expected, IAPVerifier::RequestData.new(receipt).to_h
12
- end
13
- end