spackle-ruby 0.0.10 → 0.0.12

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
  SHA256:
3
- metadata.gz: bceed0a6e18422d4f123a62f516c7b1916bcba6461d414aa40c127bb46d81b5e
4
- data.tar.gz: 23519eb5e17fd09b6a39c811e782b8370dd082977ad26571fa0771c521200c5c
3
+ metadata.gz: 18048e2400b535e381a4cf3130d671c5489df5453834133fb2a577ad0a175cb1
4
+ data.tar.gz: 7c98ce5a2d61102e20d079e000002b3f2faa4704efdb39638dedf6f61d491d48
5
5
  SHA512:
6
- metadata.gz: 23cf93c13b32c59c7eb3f648593bf675775a9c64b61aead3feac79737114966c8852ff6c9dac923fa00251661f2f13b0c0827ce03300dbe9c17204caa470961a
7
- data.tar.gz: d2542df5584b5246fb342e8331df34e7c3d716cb84aeb9b9a913167d103c639474d065dd90a063df2714a08b9665ebe15d7615147d36519baf689ce893c990db
6
+ metadata.gz: fa08056244492c4697b5f36f43d2acd2c64c943a7597a36d2406115f6e0426ee9ebd3f823ddc75c4050f9bf671b56b4d8f17726fc0d9a1d60668ce20918ba2ba
7
+ data.tar.gz: 779445743a20139ac77fcc9e1b8022bd4ac2bf75350cb5feb7ada99c6d5d278459e507a9c27f51110b700e8d4b8844773ad9213fe770b20b6f1aa6a373d64216
@@ -6,30 +6,48 @@ require 'logger'
6
6
  module Spackle
7
7
  class DynamoDBStore < BaseStore
8
8
  @client = nil
9
- @store_config = {}
9
+ @credentials = nil
10
10
 
11
- def initialize(client = nil, store_config = nil)
12
- @store_config = store_config || {}
13
- @client = client || bootstrap_client
11
+ def initialize()
12
+ @client = bootstrap_client
14
13
  end
15
14
 
16
15
  def get_customer_data(id)
17
16
  result = get_item(id)
18
17
 
19
18
  if result.nil? or result.item.nil?
20
- raise SpackleError.new "Customer #{id} not found"
19
+ return fetch_state_from_api(id)
21
20
  end
22
21
 
23
22
  JSON.parse(result.item['State'])
24
23
  end
25
24
 
25
+ def fetch_state_from_api(id)
26
+ Util.log_warn("Customer #{id} not found in DynamoDB. Fetching from API...")
27
+ uri = URI(Spackle.api_base + '/customers/' + id + '/state')
28
+ https = Net::HTTP.new(uri.host, uri.port)
29
+ https.use_ssl = true
30
+
31
+ request = Net::HTTP::Get.new(uri.path)
32
+ request['Authorization'] = 'Bearer ' + Spackle.api_key
33
+
34
+ response = https.request(request)
35
+ if response.code != '200'
36
+ puts response.code
37
+ puts response.body
38
+ raise SpackleError.new "Customer #{id} not found"
39
+ end
40
+
41
+ JSON.parse(response.body)
42
+ end
43
+
26
44
  private
27
45
 
28
46
  def get_item(id)
29
47
  return @client.get_item({
30
- table_name: @store_config['table_name'],
48
+ table_name: @credentials.session['adapter']['table_name'],
31
49
  key: {
32
- AccountId: @store_config['identity_id'],
50
+ AccountId: @credentials.session['adapter']['identity_id'],
33
51
  CustomerId: "#{id}:#{Spackle.version}"
34
52
  }
35
53
  })
@@ -44,57 +62,56 @@ module Spackle
44
62
  raise SpackleError.new 'API key not set'
45
63
  end
46
64
 
47
- uri = URI(Spackle.api_base + '/sessions')
48
- https = Net::HTTP.new(uri.host, uri.port)
49
- https.use_ssl = true
50
-
51
- request = Net::HTTP::Post.new(uri.path)
52
- request['Authorization'] = 'Bearer ' + Spackle.api_key
53
-
54
- response = https.request(request)
55
- data = JSON.parse(response.body)
56
- Util.log_debug("Created session: #{data}")
57
-
58
- @store_config = data['adapter']
59
- credentials = SpackleCredentials.new(
60
- @store_config['role_arn'],
61
- @store_config['token'],
62
- @store_config['region'],
63
- )
64
-
65
+ @credentials = SpackleAWSCredentials.new
65
66
  Aws::DynamoDB::Client.new(
66
- credentials: credentials,
67
+ region: @credentials.session['adapter']['region'],
68
+ credentials: @credentials,
67
69
  )
68
70
  end
69
71
  end
70
72
 
71
- class SpackleCredentials
73
+ class SpackleAWSCredentials
72
74
  include Aws::CredentialProvider
73
75
  include Aws::RefreshingCredentials
74
76
 
75
- @region = nil
76
- @role_arn = nil
77
- @token = nil
77
+ @client = nil
78
+ @session = {}
78
79
 
79
- def initialize(role_arn, token, region)
80
- @region = region
81
- @role_arn = role_arn
82
- @token = token
80
+ def initialize()
81
+ @session = create_session
83
82
  @client = Aws::STS::Client.new(
84
- region: @region,
83
+ region: @session['adapter']['region'],
85
84
  credentials: false,
86
85
  )
87
86
  super()
88
87
  end
89
88
 
89
+ def session
90
+ @session
91
+ end
92
+
90
93
  private
91
94
 
95
+ def create_session
96
+ uri = URI(Spackle.api_base + '/sessions')
97
+ https = Net::HTTP.new(uri.host, uri.port)
98
+ https.use_ssl = true
99
+
100
+ request = Net::HTTP::Post.new(uri.path)
101
+ request['Authorization'] = 'Bearer ' + Spackle.api_key
102
+
103
+ response = https.request(request)
104
+ data = JSON.parse(response.body)
105
+ Util.log_debug("Created session: #{data}")
106
+ data
107
+ end
108
+
92
109
  def refresh
93
110
  Util.log_debug('Refreshing DynamoDB credentials...')
94
111
  c = @client.assume_role_with_web_identity({
95
- role_arn: @role_arn,
112
+ role_arn: @session['adapter']['role_arn'],
96
113
  role_session_name: Base64.strict_encode64(SecureRandom.uuid),
97
- web_identity_token: @token
114
+ web_identity_token: @session['adapter']['token'],
98
115
  }).credentials
99
116
  @credentials = Aws::Credentials.new(
100
117
  c.access_key_id,
data/spackle.gemspec CHANGED
@@ -2,7 +2,7 @@ $LOAD_PATH.unshift(::File.join(::File.dirname(__FILE__), "lib"))
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "spackle-ruby"
5
- s.version = "0.0.10"
5
+ s.version = "0.0.12"
6
6
  s.summary = "Spackle Ruby gem"
7
7
  s.description = "Spackle is the easiest way to integrate your Ruby app with Stripe Billing. " \
8
8
  "See https://www.spackle.so for details."
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spackle-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Spackle
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-04-02 00:00:00.000000000 Z
11
+ date: 2023-04-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk
@@ -112,7 +112,7 @@ metadata:
112
112
  github_repo: ssh://github.com/spackleso/spackle-ruby
113
113
  homepage_uri: https://docs.spackle.so/ruby
114
114
  source_code_uri: https://github.com/spackleso/spackle-ruby
115
- post_install_message:
115
+ post_install_message:
116
116
  rdoc_options: []
117
117
  require_paths:
118
118
  - lib
@@ -127,8 +127,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
127
127
  - !ruby/object:Gem::Version
128
128
  version: '0'
129
129
  requirements: []
130
- rubygems_version: 3.0.3.1
131
- signing_key:
130
+ rubygems_version: 3.4.10
131
+ signing_key:
132
132
  specification_version: 4
133
133
  summary: Spackle Ruby gem
134
134
  test_files: []