tarpon 0.1.1 → 0.2.0

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
  SHA256:
3
- metadata.gz: b8e5a49cefe2bedd26a02e959b6c8eb24aebb37ff5b434cc9ab2491449d2db68
4
- data.tar.gz: ab2217bc86dbbf8277dc66f73368e9e6cde96c435e82c37a681dd93708c2aa34
3
+ metadata.gz: c6a65859f989fffb6bd8b475ba7c4b9868437d5fdcf625e0d4707a787c4143da
4
+ data.tar.gz: 35bd6987ce3cf9eda1a035a48c90bdc07e687de617c7fedd671ec53dcaf0695f
5
5
  SHA512:
6
- metadata.gz: d6da4a1ee4c6f89cb585db622ab2a79be2fbea74b365a9f7967d197adbfbd116b555936b010a22064d1c50b99f9912e7b03dda6c3285680f9018407cfe226a66
7
- data.tar.gz: fcf05f255081a6754289c3c459b0b86f5b83578681a786391cdf5229b108b926caa202f5663972c8b2c36fdbd74cbc9b2ad2069fbce515af249fa7c806aaea7d
6
+ metadata.gz: 3c2bc8ca48ddf4240fab201af578eb8408b6d446274232f1ee939157af3a098ed9ca6b22ddf39b54b3da0b65feac0ee7400edb5fe15978a778229507b5ccd7b0
7
+ data.tar.gz: 0f171966fa51d6ff53b0ad1e2e886107e46d7f1947061a3d37163a00926b5bb278894a8a6cefc03bab0e31aebbb56f2a28674fbf080ae98cfba3fc5f413e76a4
data/lib/tarpon/errors.rb CHANGED
@@ -5,4 +5,5 @@ module Tarpon
5
5
  class TimeoutError < Error; end
6
6
  class InvalidCredentialsError < Error; end
7
7
  class ServerError < Error; end
8
+ class NotFoundError < Error; end
8
9
  end
@@ -14,36 +14,51 @@ module Tarpon
14
14
 
15
15
  def perform(method:, path:, key:, headers: {}, body: nil)
16
16
  HTTP
17
- .auth("Bearer #{translate_key(key)}")
18
- .headers(headers.merge(DEFAULT_HEADERS))
19
17
  .timeout(Client.timeout)
18
+ .auth("Bearer #{api_key(key)}")
19
+ .headers(headers.merge(DEFAULT_HEADERS))
20
20
  .send(method, "#{Client.base_uri}#{path}", json: body&.compact)
21
- .yield_self { |response| handle_response(response) }
21
+ .yield_self { |http_response| handle_response(http_response) }
22
22
  rescue HTTP::TimeoutError => e
23
23
  raise Tarpon::TimeoutError, e
24
24
  end
25
25
 
26
- def translate_key(key)
27
- Client.send("#{key}_api_key")
26
+ private
27
+
28
+ def api_key(type)
29
+ Client.send("#{type}_api_key")
28
30
  end
29
31
 
30
- private
32
+ def parse_body(http_response)
33
+ return {} if http_response.body.empty?
31
34
 
32
- def handle_response(response)
33
- case response.code
34
- when 401
35
- raise Tarpon::InvalidCredentialsError,
36
- 'Invalid credentials, fix your API keys'
37
- when 500..505
38
- raise Tarpon::ServerError,
39
- 'RevenueCat failed to fulfill the request'
35
+ JSON.parse(http_response.body, symbolize_names: true)
36
+ end
37
+
38
+ def create_response(http_response)
39
+ Tarpon::Response.new(http_response.status, parse_body(http_response))
40
+ end
41
+
42
+ def handle_response(http_response)
43
+ case http_response.code
44
+ when 200..299
45
+ create_response(http_response)
40
46
  else
41
- Tarpon::Response.new(response.status, parse_body(response.body))
47
+ handle_error(http_response)
42
48
  end
43
49
  end
44
50
 
45
- def parse_body(body)
46
- JSON.parse(body, symbolize_names: true)
51
+ def handle_error(http_response)
52
+ case http_response.code
53
+ when 401
54
+ raise Tarpon::InvalidCredentialsError, 'Invalid credentials, fix your API keys'
55
+ when 500..599
56
+ raise Tarpon::ServerError, 'RevenueCat failed to fulfill the request'
57
+ when 404
58
+ raise Tarpon::NotFoundError
59
+ else
60
+ create_response(http_response)
61
+ end
47
62
  end
48
63
  end
49
64
  end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tarpon
4
+ module Request
5
+ class Subscriber
6
+ class Entitlement < Base
7
+ def initialize(subscriber_path:, entitlement_identifier:)
8
+ @subscriber_path = subscriber_path
9
+ @entitlement_identifier = entitlement_identifier
10
+ end
11
+
12
+ def grant_promotional(duration:, start_time_ms: nil)
13
+ body = {
14
+ duration: duration,
15
+ start_time_ms: start_time_ms
16
+ }
17
+
18
+ perform(method: :post, path: "#{path}/promotional", key: :secret, body: body)
19
+ end
20
+
21
+ def revoke_promotional
22
+ perform(method: :post, path: "#{path}/revoke_promotionals", key: :secret)
23
+ end
24
+
25
+ private
26
+
27
+ def path
28
+ "#{@subscriber_path}/entitlements/#{@entitlement_identifier}"
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tarpon
4
+ module Request
5
+ class Subscriber
6
+ class Subscription < Base
7
+ def initialize(subscriber_path:, product_id:)
8
+ @subscriber_path = subscriber_path
9
+ @product_id = product_id
10
+ end
11
+
12
+ def defer(expiry_time_ms:)
13
+ body = { expiry_time_ms: expiry_time_ms }
14
+
15
+ perform(method: :post, path: "#{path}/defer", key: :secret, body: body)
16
+ end
17
+
18
+ private
19
+
20
+ def path
21
+ "#{@subscriber_path}/subscriptions/#{@product_id}"
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -30,51 +30,6 @@ module Tarpon
30
30
  def path
31
31
  "/subscribers/#{@app_user_id}"
32
32
  end
33
-
34
- class Entitlement < Base
35
- def initialize(subscriber_path:, entitlement_identifier:)
36
- @subscriber_path = subscriber_path
37
- @entitlement_identifier = entitlement_identifier
38
- end
39
-
40
- def grant_promotional(duration:, start_time_ms: nil)
41
- body = {
42
- duration: duration,
43
- start_time_ms: start_time_ms
44
- }
45
-
46
- perform(method: :post, path: "#{path}/promotional", key: :secret, body: body)
47
- end
48
-
49
- def revoke_promotional
50
- perform(method: :post, path: "#{path}/revoke_promotionals", key: :secret)
51
- end
52
-
53
- private
54
-
55
- def path
56
- "#{@subscriber_path}/entitlements/#{@entitlement_identifier}"
57
- end
58
- end
59
-
60
- class Subscription < Base
61
- def initialize(subscriber_path:, product_id:)
62
- @subscriber_path = subscriber_path
63
- @product_id = product_id
64
- end
65
-
66
- def defer(expiry_time_ms:)
67
- body = { expiry_time_ms: expiry_time_ms }
68
-
69
- perform(method: :post, path: "#{path}/defer", key: :secret, body: body)
70
- end
71
-
72
- private
73
-
74
- def path
75
- "#{@subscriber_path}/subscriptions/#{@product_id}"
76
- end
77
- end
78
33
  end
79
34
  end
80
35
  end
@@ -10,6 +10,10 @@ module Tarpon
10
10
  @subscriber = @raw[:subscriber].nil? ? nil : Entity::Subscriber.new(@raw[:subscriber])
11
11
  end
12
12
 
13
+ def message
14
+ @raw[:message]
15
+ end
16
+
13
17
  def success?
14
18
  @status.success?
15
19
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Tarpon
4
- VERSION = '0.1.1'
4
+ VERSION = '0.2.0'
5
5
  end
data/lib/tarpon.rb CHANGED
@@ -7,6 +7,8 @@ require 'tarpon/entity/subscriber'
7
7
  require 'tarpon/errors'
8
8
  require 'tarpon/request/base'
9
9
  require 'tarpon/request/subscriber'
10
+ require 'tarpon/request/subscriber/entitlement'
11
+ require 'tarpon/request/subscriber/subscription'
10
12
  require 'tarpon/request/receipt'
11
13
  require 'tarpon/response'
12
14
  require 'tarpon/version'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tarpon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Igor Belo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-02-19 00:00:00.000000000 Z
11
+ date: 2020-02-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: http
@@ -125,6 +125,8 @@ files:
125
125
  - lib/tarpon/request/base.rb
126
126
  - lib/tarpon/request/receipt.rb
127
127
  - lib/tarpon/request/subscriber.rb
128
+ - lib/tarpon/request/subscriber/entitlement.rb
129
+ - lib/tarpon/request/subscriber/subscription.rb
128
130
  - lib/tarpon/response.rb
129
131
  - lib/tarpon/version.rb
130
132
  homepage: https://github.com/fishbrain/tarpon