spackle-ruby 0.0.12 → 0.0.13

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: 18048e2400b535e381a4cf3130d671c5489df5453834133fb2a577ad0a175cb1
4
- data.tar.gz: 7c98ce5a2d61102e20d079e000002b3f2faa4704efdb39638dedf6f61d491d48
3
+ metadata.gz: a9db8095ffa764bd7168fd58dd693be699a3935bfec20f94b47083f48ac7c6c3
4
+ data.tar.gz: f9c1ac487e16061759e4c617e49f7962567f32fd0559b5917b9de106c53af9fd
5
5
  SHA512:
6
- metadata.gz: fa08056244492c4697b5f36f43d2acd2c64c943a7597a36d2406115f6e0426ee9ebd3f823ddc75c4050f9bf671b56b4d8f17726fc0d9a1d60668ce20918ba2ba
7
- data.tar.gz: 779445743a20139ac77fcc9e1b8022bd4ac2bf75350cb5feb7ada99c6d5d278459e507a9c27f51110b700e8d4b8844773ad9213fe770b20b6f1aa6a373d64216
6
+ metadata.gz: c9551e7c797ad58938d4de81dc5aca6016cf344b72fdca62a2d24f90d7124ff5d02c5497ebdcd77fbd13a8ba6c9abbeff0edfe365ff7196b07541de8be2e6806
7
+ data.tar.gz: 37408f32c5a15421157b4ab3987cf7b01eda0c341136e33772890476549a76df1694a157a5c71901b0ec23bf627ab7d86b644bf947f13dbfe21fe4e4197e706e
data/Gemfile CHANGED
@@ -4,7 +4,6 @@ source "https://rubygems.org"
4
4
 
5
5
  git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
6
6
 
7
- gem "aws-sdk", "~> 3"
8
7
  gem "rake", "~> 13.0"
9
8
  gem "nokogiri", "~> 1.13"
10
9
  gem "minitest", "~> 5.18"
@@ -4,11 +4,13 @@ module Spackle
4
4
  class SpackleConfiguration
5
5
  attr_accessor :api_key
6
6
  attr_accessor :api_base
7
+ attr_accessor :edge_base
7
8
  attr_reader :logger
8
9
  attr_reader :version
9
10
 
10
11
  def initialize
11
12
  @api_base = 'https://api.spackle.so/v1'
13
+ @edge_base = 'https://us-west-2.edge.spackle.so'
12
14
  @log_level = Logger::WARN
13
15
  @logger = Logger.new(STDOUT, level: @log_level)
14
16
  @version = 1
@@ -26,7 +28,7 @@ module Spackle
26
28
 
27
29
  def store()
28
30
  if @store == nil
29
- @store = DynamoDBStore.new
31
+ @store = EdgeStore.new
30
32
  end
31
33
  @store
32
34
  end
@@ -0,0 +1,46 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ require 'logger'
4
+
5
+ module Spackle
6
+ class EdgeStore < BaseStore
7
+ def initialize()
8
+ edge_uri = URI(Spackle.edge_base)
9
+ @edge_http = Net::HTTP.new(edge_uri.host, edge_uri.port)
10
+ @edge_http.use_ssl = true
11
+
12
+ api_uri = URI(Spackle.api_base)
13
+ @api_http = Net::HTTP.new(api_uri.host, api_uri.port)
14
+ @api_http.use_ssl = true
15
+ end
16
+
17
+ def get_customer_data(id)
18
+ request = Net::HTTP::Get.new(Spackle.edge_base + '/customers/' + id + '/state')
19
+ request['Authorization'] = 'Bearer ' + Spackle.api_key
20
+ request['X-Spackle-Schema-Version'] = Spackle.version.to_s
21
+
22
+ response = @edge_http.request(request)
23
+ if response.code != '200'
24
+ return fetch_state_from_api(id)
25
+ end
26
+
27
+ JSON.parse(response.body)
28
+ end
29
+
30
+ def fetch_state_from_api(id)
31
+ Util.log_warn("Customer #{id} not found. Fetching from API...")
32
+ request = Net::HTTP::Get.new(Spackle.api_base + '/customers/' + id + '/state')
33
+ request['Authorization'] = 'Bearer ' + Spackle.api_key
34
+ request['X-Spackle-Schema-Version'] = Spackle.version.to_s
35
+
36
+ response = @api_http.request(request)
37
+ if response.code != '200'
38
+ puts response.code
39
+ puts response.body
40
+ raise SpackleError.new "Customer #{id} not found"
41
+ end
42
+
43
+ JSON.parse(response.body)
44
+ end
45
+ end
46
+ end
data/lib/spackle.rb CHANGED
@@ -7,7 +7,7 @@ require 'spackle/util'
7
7
  require 'spackle/waiters'
8
8
 
9
9
  require 'spackle/stores/base'
10
- require 'spackle/stores/dynamodb'
10
+ require 'spackle/stores/edge'
11
11
  require 'spackle/stores/file'
12
12
  require 'spackle/stores/memory'
13
13
 
@@ -27,16 +27,13 @@ module Spackle
27
27
 
28
28
  def_delegators :@config, :api_key, :api_key=
29
29
  def_delegators :@config, :api_base, :api_base=
30
+ def_delegators :@config, :edge_base, :edge_base=
30
31
  def_delegators :@config, :log_level, :log_level=
31
32
  def_delegators :@config, :store, :store=
32
33
  def_delegators :@config, :logger, :logger=
33
34
  def_delegators :@config, :version, :version=
34
35
  end
35
36
 
36
- def self.bootstrap
37
- Spackle.store
38
- end
39
-
40
37
  class SpackleError < StandardError
41
38
  end
42
39
  end
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.12"
5
+ s.version = "0.0.13"
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."
@@ -18,7 +18,6 @@ Gem::Specification.new do |s|
18
18
  "source_code_uri" => "https://github.com/spackleso/spackle-ruby",
19
19
  }
20
20
 
21
- s.add_dependency "aws-sdk", "~> 3"
22
21
  s.add_dependency "nokogiri", "~> 1.13"
23
22
  s.add_dependency "stripe", "~> 8.3"
24
23
 
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spackle-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.12
4
+ version: 0.0.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Spackle
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-04-09 00:00:00.000000000 Z
11
+ date: 2023-07-23 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: aws-sdk
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '3'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '3'
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: nokogiri
29
15
  requirement: !ruby/object:Gem::Requirement
@@ -97,7 +83,7 @@ files:
97
83
  - lib/spackle/customer.rb
98
84
  - lib/spackle/spackle_configuration.rb
99
85
  - lib/spackle/stores/base.rb
100
- - lib/spackle/stores/dynamodb.rb
86
+ - lib/spackle/stores/edge.rb
101
87
  - lib/spackle/stores/file.rb
102
88
  - lib/spackle/stores/memory.rb
103
89
  - lib/spackle/util.rb
@@ -1,124 +0,0 @@
1
- require 'aws-sdk'
2
- require 'net/http'
3
- require 'json'
4
- require 'logger'
5
-
6
- module Spackle
7
- class DynamoDBStore < BaseStore
8
- @client = nil
9
- @credentials = nil
10
-
11
- def initialize()
12
- @client = bootstrap_client
13
- end
14
-
15
- def get_customer_data(id)
16
- result = get_item(id)
17
-
18
- if result.nil? or result.item.nil?
19
- return fetch_state_from_api(id)
20
- end
21
-
22
- JSON.parse(result.item['State'])
23
- end
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
-
44
- private
45
-
46
- def get_item(id)
47
- return @client.get_item({
48
- table_name: @credentials.session['adapter']['table_name'],
49
- key: {
50
- AccountId: @credentials.session['adapter']['identity_id'],
51
- CustomerId: "#{id}:#{Spackle.version}"
52
- }
53
- })
54
- rescue StandardError
55
- return nil
56
- end
57
-
58
- def bootstrap_client
59
- Util.log_debug('Bootstrapping DynamoDB client...')
60
-
61
- if Spackle.api_key.nil?
62
- raise SpackleError.new 'API key not set'
63
- end
64
-
65
- @credentials = SpackleAWSCredentials.new
66
- Aws::DynamoDB::Client.new(
67
- region: @credentials.session['adapter']['region'],
68
- credentials: @credentials,
69
- )
70
- end
71
- end
72
-
73
- class SpackleAWSCredentials
74
- include Aws::CredentialProvider
75
- include Aws::RefreshingCredentials
76
-
77
- @client = nil
78
- @session = {}
79
-
80
- def initialize()
81
- @session = create_session
82
- @client = Aws::STS::Client.new(
83
- region: @session['adapter']['region'],
84
- credentials: false,
85
- )
86
- super()
87
- end
88
-
89
- def session
90
- @session
91
- end
92
-
93
- private
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
-
109
- def refresh
110
- Util.log_debug('Refreshing DynamoDB credentials...')
111
- c = @client.assume_role_with_web_identity({
112
- role_arn: @session['adapter']['role_arn'],
113
- role_session_name: Base64.strict_encode64(SecureRandom.uuid),
114
- web_identity_token: @session['adapter']['token'],
115
- }).credentials
116
- @credentials = Aws::Credentials.new(
117
- c.access_key_id,
118
- c.secret_access_key,
119
- c.session_token
120
- )
121
- @expiration = c.expiration
122
- end
123
- end
124
- end