query_guard 0.4.0 → 0.4.2
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 +4 -4
- data/lib/query_guard/client.rb +40 -0
- data/lib/query_guard/version.rb +1 -1
- data/lib/query_guard.rb +37 -25
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f2c67da58e48c28519c8d0f89931b9dd5174e4b991ccfb0e6f3fb1d040559d94
|
|
4
|
+
data.tar.gz: 7b166e12b34ef4d75665902c24a1867f1184cc20d9eef794bc253c23e903ae9b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9db12bc645692c9dddd754c471b474b2bb005a7cc996dfb5409105acc4b7d860612ae1e44901ea7cf6e47aa4dff814a32af4094b9d002ca8b619527f073956f1
|
|
7
|
+
data.tar.gz: 0ce2d383626be81268fe1e1f2352104241c7ca219316c960f04265eff15560053270cc07b0e3f0d847f949235a9593ba09241910017a6322df80509b17641101
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
require "net/http"
|
|
3
|
+
require "uri"
|
|
4
|
+
require "json"
|
|
5
|
+
|
|
6
|
+
module QueryGuard
|
|
7
|
+
class Client
|
|
8
|
+
DEFAULT_TIMEOUT = 5 # seconds
|
|
9
|
+
|
|
10
|
+
def initialize(base_url:, api_key:, project:, env:)
|
|
11
|
+
@base_url = base_url.sub(%r{/\z}, "") rescue ''
|
|
12
|
+
@api_key = api_key
|
|
13
|
+
@project = project
|
|
14
|
+
@env = env
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# Example call used by your Subscriber/Middleware
|
|
18
|
+
def post(path, payload)
|
|
19
|
+
uri = URI.parse("#{@base_url}#{path}")
|
|
20
|
+
req = Net::HTTP::Post.new(uri)
|
|
21
|
+
req["Content-Type"] = "application/json"
|
|
22
|
+
req["Authorization"] = "Bearer #{@api_key}" if @api_key
|
|
23
|
+
req.body = JSON.generate(payload.merge(project: @project, env: @env))
|
|
24
|
+
|
|
25
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
26
|
+
http.use_ssl = (uri.scheme == "https")
|
|
27
|
+
http.open_timeout = DEFAULT_TIMEOUT
|
|
28
|
+
http.read_timeout = DEFAULT_TIMEOUT
|
|
29
|
+
|
|
30
|
+
res = http.request(req)
|
|
31
|
+
unless res.is_a?(Net::HTTPSuccess)
|
|
32
|
+
warn "[QueryGuard] POST #{uri} -> #{res.code} #{res.body}"
|
|
33
|
+
end
|
|
34
|
+
res
|
|
35
|
+
rescue => e
|
|
36
|
+
warn "[QueryGuard] HTTP error: #{e.class}: #{e.message}"
|
|
37
|
+
nil
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
data/lib/query_guard/version.rb
CHANGED
data/lib/query_guard.rb
CHANGED
|
@@ -5,42 +5,54 @@ require "query_guard/version"
|
|
|
5
5
|
require "query_guard/config"
|
|
6
6
|
require "query_guard/subscriber"
|
|
7
7
|
require "query_guard/middleware"
|
|
8
|
+
require "query_guard/client"
|
|
8
9
|
|
|
9
10
|
module QueryGuard
|
|
10
|
-
|
|
11
|
-
|
|
11
|
+
class << self
|
|
12
|
+
attr_accessor :client
|
|
13
|
+
# Keep config as a normal module ivar; no mattr_*
|
|
14
|
+
def config
|
|
15
|
+
@config ||= Config.new
|
|
16
|
+
end
|
|
12
17
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
18
|
+
def configure
|
|
19
|
+
yield(config)
|
|
20
|
+
# Build a reusable HTTP client (whatever your Client class is)
|
|
21
|
+
@client = Client.new(
|
|
22
|
+
base_url: config.base_url,
|
|
23
|
+
api_key: config.api_key,
|
|
24
|
+
project: config.project,
|
|
25
|
+
env: config.env
|
|
26
|
+
)
|
|
27
|
+
self
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def install!(app = nil)
|
|
31
|
+
# Ensure config exists even if user didn't call configure
|
|
32
|
+
config
|
|
33
|
+
|
|
34
|
+
# Install SQL subscriber once
|
|
35
|
+
Subscriber.install!(config)
|
|
23
36
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
37
|
+
# Insert middleware
|
|
38
|
+
if defined?(Rails) && Rails.respond_to?(:application) && Rails.application
|
|
39
|
+
Rails.application.config.middleware.use(QueryGuard::Middleware, config)
|
|
40
|
+
elsif app
|
|
41
|
+
app.use(QueryGuard::Middleware, config)
|
|
42
|
+
end
|
|
27
43
|
|
|
28
|
-
|
|
29
|
-
if defined?(Rails) && Rails.respond_to?(:application) && Rails.application
|
|
30
|
-
# Use a Railtie-less insert for safety if called early
|
|
31
|
-
Rails.application.config.middleware.use(QueryGuard::Middleware, config)
|
|
32
|
-
elsif app
|
|
33
|
-
app.use(QueryGuard::Middleware, config)
|
|
44
|
+
self
|
|
34
45
|
end
|
|
35
|
-
self
|
|
36
46
|
end
|
|
37
47
|
end
|
|
38
48
|
|
|
39
49
|
# Auto-install for Rails via Railtie
|
|
40
50
|
if defined?(Rails::Railtie)
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
51
|
+
module QueryGuard
|
|
52
|
+
class Railtie < Rails::Railtie
|
|
53
|
+
initializer "query_guard.install" do |app|
|
|
54
|
+
QueryGuard.install!(app)
|
|
55
|
+
end
|
|
44
56
|
end
|
|
45
57
|
end
|
|
46
58
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: query_guard
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.4.
|
|
4
|
+
version: 0.4.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Chitradevi36
|
|
@@ -59,6 +59,7 @@ files:
|
|
|
59
59
|
- Rakefile
|
|
60
60
|
- config/initializers/query_guard.rb
|
|
61
61
|
- lib/query_guard.rb
|
|
62
|
+
- lib/query_guard/client.rb
|
|
62
63
|
- lib/query_guard/config.rb
|
|
63
64
|
- lib/query_guard/middleware.rb
|
|
64
65
|
- lib/query_guard/subscriber.rb
|