qodex-rails 0.1.0 → 0.1.1

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: e9473a5451651b8898e4c25ebea7a43970bc709572a9e97f65b6d455ab2fda4e
4
- data.tar.gz: 9c8cf36a49b5f344405b06d62fa1b1a91942aa085923b5b06722d51c82a87b34
3
+ metadata.gz: 0cc9a5ccd335641d6c77c77ec917e86f4546b9ad6b17111b5a3ea52bc79faa3c
4
+ data.tar.gz: b5bc6ee69f2d251c98cc72c22957d01f0f58437909b47f031e91d1b31dd44de6
5
5
  SHA512:
6
- metadata.gz: 6d0cda2b4ece0ae0a1ff8c080e4b730af8535468d768692dd1c65dd320f04f6c277a2c6cdbad3f64521af7e20a19807b66b42320d7a21ee4e7e3b5da201e89d6
7
- data.tar.gz: 4e29909270124a83e4f73ee104a61e06d787d8700d28bf7646805f799bb5e2747d5ee4a93e4e8d1632b2cd3ddc5911a21284252179613c2b0be7efa62db9b35d
6
+ metadata.gz: 17ebca6fdc4e5349277352f388181e6e5a75164c3acaa43a52db8c3f39ae257e1b3a1f48459eaf0e89aa507b050c49b7a1f563e157a63f8375355dee65f4afee
7
+ data.tar.gz: 9e2e6bd01d2d2eb2be4073aac3403154edf8a8ca68db91ab06bf5c84405aa589c927885572f4dfbe18d846e1081e6ba3c688766c48bc8dbf1bdd1dbec592fc72
data/README.md CHANGED
@@ -14,6 +14,15 @@ If bundler is not being used to manage dependencies, install the gem by executin
14
14
 
15
15
  $ gem install qodex-rails
16
16
 
17
+ ## Configuration
18
+ # config/initializers/qodex_rails.rb
19
+
20
+ QodexRails.configure do |config|
21
+ config.collection_name = 'Your_Collection_Name' # Name of the collection where logs will be stored
22
+ config.api_key = 'Your_API_Key' # API key for authentication
23
+ end
24
+
25
+
17
26
  ## Usage
18
27
 
19
28
  TODO: Write usage instructions here
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Qodex
4
4
  module Rails
5
- VERSION = "0.1.0"
5
+ VERSION = "0.1.1"
6
6
  end
7
7
  end
@@ -0,0 +1,10 @@
1
+ module QodexRails
2
+ class Configuration
3
+ attr_accessor :collection_name, :api_key
4
+
5
+ def initialize
6
+ @collection_name = nil
7
+ @api_key = nil
8
+ end
9
+ end
10
+ end
@@ -1,3 +1,6 @@
1
+ require 'net/http'
2
+ require 'json'
3
+
1
4
  module QodexRails
2
5
  module Middleware
3
6
  class RequestLogger
@@ -8,24 +11,64 @@ module QodexRails
8
11
 
9
12
  def call(env)
10
13
  start_time = Time.now
14
+
15
+ # Capture the request details
16
+ request = Rack::Request.new(env)
17
+ request_body = request.body.read
18
+ request.body.rewind
19
+
11
20
  status, headers, response = @app.call(env)
21
+
12
22
  end_time = Time.now
13
23
 
14
- request = Rack::Request.new(env)
15
- filtered_params = request.filtered_parameters # Uses Rails' parameter filtering
16
-
17
- @mutex.synchronize do # Ensure thread-safe logging
18
- Rails.logger.info <<~LOG
19
- Request to #{request.path_info}
20
- Method: #{request.request_method}
21
- Parameters: #{filtered_params.inspect}
22
- Response Status: #{status}
23
- Time Taken: #{(end_time - start_time) * 1000} ms
24
- LOG
25
- end
24
+ # Capture the response details
25
+ response_body = extract_body(response)
26
+
27
+ # Construct the logs
28
+ logs = {
29
+ collection_name: QodexRails.configuration.collection_name,
30
+ api_key: QodexRails.configuration.api_key,
31
+ apis: [{
32
+ body: response_body,
33
+ body_type: response.content_type,
34
+ request_type: request.request_method,
35
+ timestamp: Time.now.to_i,
36
+ url: request.url,
37
+ status: status,
38
+ headers: extract_headers(headers),
39
+ params: request.filtered_parameters # Using Rails' parameter filtering
40
+ }]
41
+ }
42
+
43
+ # Send the logs to the external API
44
+ send_to_api(logs)
26
45
 
27
46
  [status, headers, response]
28
47
  end
48
+
49
+ private
50
+
51
+ def extract_body(response)
52
+ body = ""
53
+ response.each { |part| body << part }
54
+ body
55
+ end
56
+
57
+ def extract_headers(headers)
58
+ headers.map { |name, value| { name: name, value: value } }
59
+ end
60
+
61
+ def send_to_api(logs)
62
+ uri = URI("#{ENV['API_URL']}/api/v1/collections/create_with_folder/#{ENV['API_KEY']}")
63
+ request = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
64
+ request.body = JSON.generate(logs)
65
+ response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
66
+ http.request(request)
67
+ end
68
+
69
+ # Optionally log the response from the external API
70
+ Rails.logger.info "API Response: #{response.body}" if response
71
+ end
29
72
  end
30
73
  end
31
74
  end
@@ -1,3 +1,3 @@
1
1
  module QodexRails
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/lib/qodex-rails.rb CHANGED
@@ -1,9 +1,24 @@
1
1
  require "qodex-rails/version"
2
2
  require "qodex-rails/middleware"
3
+ require "qodex-rails/configuration" # Require the new configuration file
3
4
 
4
5
  module QodexRails
5
6
  class Error < StandardError; end
6
7
 
8
+ class << self
9
+ attr_writer :configuration
10
+
11
+ # Get or initialize the configuration
12
+ def configuration
13
+ @configuration ||= Configuration.new
14
+ end
15
+
16
+ # Allow block-style configuration
17
+ def configure
18
+ yield(configuration)
19
+ end
20
+ end
21
+
7
22
  class Railtie < Rails::Railtie
8
23
  initializer "qodex-rails.insert_middleware" do |app|
9
24
  app.config.middleware.use Middleware::RequestLogger
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qodex-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - sid
@@ -25,10 +25,12 @@ files:
25
25
  - README.md
26
26
  - Rakefile
27
27
  - lib/qodex-rails.rb
28
+ - lib/qodex-rails/configuration.rb
28
29
  - lib/qodex-rails/middleware.rb
29
30
  - lib/qodex-rails/version.rb
30
31
  - lib/qodex/rails.rb
31
32
  - lib/qodex/rails/version.rb
33
+ - qodex-rails-0.1.0.gem
32
34
  - sig/qodex/rails.rbs
33
35
  homepage: https://qodex.ai
34
36
  licenses: