spackle-ruby 0.0.11 → 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: d370af3cb13da25635dff5eaee17972e6f540b3ab3ad949097abc0279ca64f88
4
- data.tar.gz: 13fd6753a5dc9f8672cc5705cb13f8ebb8796b56ab685bed48921acc170f119d
3
+ metadata.gz: a9db8095ffa764bd7168fd58dd693be699a3935bfec20f94b47083f48ac7c6c3
4
+ data.tar.gz: f9c1ac487e16061759e4c617e49f7962567f32fd0559b5917b9de106c53af9fd
5
5
  SHA512:
6
- metadata.gz: c1ddcc3b1d6b16e37d4a8374d057ef6c312b2a9cac1934740bc8d237f038b1b5613a637d290ceadcc057a4f6176c07e152810a7577a0996c788d7c1c04e1b7d9
7
- data.tar.gz: 1f4a29bddbb7c8d3ba73ebd5a760d424afa308b6f1c19819ef159d6a23dd32f4af329b8256561f067d0ee08df451762164dec8c5dc162aacbd30a6dbb1d7acbe
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.11"
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.11
4
+ version: 0.0.13
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-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
@@ -112,7 +98,7 @@ metadata:
112
98
  github_repo: ssh://github.com/spackleso/spackle-ruby
113
99
  homepage_uri: https://docs.spackle.so/ruby
114
100
  source_code_uri: https://github.com/spackleso/spackle-ruby
115
- post_install_message:
101
+ post_install_message:
116
102
  rdoc_options: []
117
103
  require_paths:
118
104
  - lib
@@ -127,8 +113,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
127
113
  - !ruby/object:Gem::Version
128
114
  version: '0'
129
115
  requirements: []
130
- rubygems_version: 3.0.3.1
131
- signing_key:
116
+ rubygems_version: 3.4.10
117
+ signing_key:
132
118
  specification_version: 4
133
119
  summary: Spackle Ruby gem
134
120
  test_files: []
@@ -1,108 +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
- @store_config = {}
10
-
11
- def initialize(client = nil, store_config = nil)
12
- @store_config = store_config || {}
13
- @client = client || bootstrap_client
14
- end
15
-
16
- def get_customer_data(id)
17
- result = get_item(id)
18
-
19
- if result.nil? or result.item.nil?
20
- raise SpackleError.new "Customer #{id} not found"
21
- end
22
-
23
- JSON.parse(result.item['State'])
24
- end
25
-
26
- private
27
-
28
- def get_item(id)
29
- return @client.get_item({
30
- table_name: @store_config['table_name'],
31
- key: {
32
- AccountId: @store_config['identity_id'],
33
- CustomerId: "#{id}:#{Spackle.version}"
34
- }
35
- })
36
- rescue StandardError
37
- return nil
38
- end
39
-
40
- def bootstrap_client
41
- Util.log_debug('Bootstrapping DynamoDB client...')
42
-
43
- if Spackle.api_key.nil?
44
- raise SpackleError.new 'API key not set'
45
- end
46
-
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
- Aws::DynamoDB::Client.new(
66
- region: @store_config['region'],
67
- credentials: credentials,
68
- )
69
- end
70
- end
71
-
72
- class SpackleCredentials
73
- include Aws::CredentialProvider
74
- include Aws::RefreshingCredentials
75
-
76
- @region = nil
77
- @role_arn = nil
78
- @token = nil
79
-
80
- def initialize(role_arn, token, region)
81
- @region = region
82
- @role_arn = role_arn
83
- @token = token
84
- @client = Aws::STS::Client.new(
85
- region: @region,
86
- credentials: false,
87
- )
88
- super()
89
- end
90
-
91
- private
92
-
93
- def refresh
94
- Util.log_debug('Refreshing DynamoDB credentials...')
95
- c = @client.assume_role_with_web_identity({
96
- role_arn: @role_arn,
97
- role_session_name: Base64.strict_encode64(SecureRandom.uuid),
98
- web_identity_token: @token
99
- }).credentials
100
- @credentials = Aws::Credentials.new(
101
- c.access_key_id,
102
- c.secret_access_key,
103
- c.session_token
104
- )
105
- @expiration = c.expiration
106
- end
107
- end
108
- end