spackle-ruby 0.0.12 → 0.0.14
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -1
- data/lib/spackle/spackle_configuration.rb +3 -1
- data/lib/spackle/stores/edge.rb +46 -0
- data/lib/spackle.rb +2 -5
- data/spackle.gemspec +2 -2
- metadata +17 -17
- data/lib/spackle/stores/dynamodb.rb +0 -124
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cafee13ca0d2f07ea47389d6a8dc8c48fb85ba5c32fd4e7b4dbcdec8b1ade3e5
|
4
|
+
data.tar.gz: 1bd2685e9bc6a5103cae2ab1aee788a01f99e410e4d98d5bf817b0c644f38866
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0af2295c908d9a31a31f20c87149af87c0149fae0908f836152ffd312330803dadd481c677c081f36af859516d6e0b99245dfe21d6f550b5029de1a38cbfccab
|
7
|
+
data.tar.gz: 1d0dd8dba75ed289375c0b7d2da98ab49c8d1561c7bee794c7fbae6c5cc7e1dcd6c9342b30e9bdc93bd7f72bf8274bb6c452e601714519079a951ae40adfe99e
|
data/Gemfile
CHANGED
@@ -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 =
|
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/
|
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.
|
5
|
+
s.version = "0.0.14"
|
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,12 +18,12 @@ 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
|
|
25
24
|
s.add_development_dependency "rake", "~> 13.0"
|
26
25
|
s.add_development_dependency "minitest", "~> 5.18"
|
26
|
+
s.add_development_dependency "webmock", "~> 3.18"
|
27
27
|
|
28
28
|
ignored = Regexp.union(
|
29
29
|
/\A\.editorconfig/,
|
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.
|
4
|
+
version: 0.0.14
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Spackle
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
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
|
@@ -80,6 +66,20 @@ dependencies:
|
|
80
66
|
- - "~>"
|
81
67
|
- !ruby/object:Gem::Version
|
82
68
|
version: '5.18'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: webmock
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.18'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.18'
|
83
83
|
description: Spackle is the easiest way to integrate your Ruby app with Stripe Billing.
|
84
84
|
See https://www.spackle.so for details.
|
85
85
|
email: support@spackle.so
|
@@ -97,7 +97,7 @@ files:
|
|
97
97
|
- lib/spackle/customer.rb
|
98
98
|
- lib/spackle/spackle_configuration.rb
|
99
99
|
- lib/spackle/stores/base.rb
|
100
|
-
- lib/spackle/stores/
|
100
|
+
- lib/spackle/stores/edge.rb
|
101
101
|
- lib/spackle/stores/file.rb
|
102
102
|
- lib/spackle/stores/memory.rb
|
103
103
|
- 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
|