qodex-rails 0.1.0 → 0.1.2

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: 5d080fca50aa62a0fd87df635167b95cc00c08daf13c66ece7967b623bd40d4f
4
+ data.tar.gz: bc4bbe33dad3f8cabb516452d7f15953db58fb60419dc66cfe3ece4b6e2298c0
5
5
  SHA512:
6
- metadata.gz: 6d0cda2b4ece0ae0a1ff8c080e4b730af8535468d768692dd1c65dd320f04f6c277a2c6cdbad3f64521af7e20a19807b66b42320d7a21ee4e7e3b5da201e89d6
7
- data.tar.gz: 4e29909270124a83e4f73ee104a61e06d787d8700d28bf7646805f799bb5e2747d5ee4a93e4e8d1632b2cd3ddc5911a21284252179613c2b0be7efa62db9b35d
6
+ metadata.gz: 7ddfeddfa0c02ee396284a0d606a70120266b9483734d69b06fb9f771152b02e29697b9799cb2586f8c48f43491ebe3f4f41b5def8a0bcc04dbeacdc09716d02
7
+ data.tar.gz: '09ef8c37d89a0ec3fb07759e79320dff24d415ff0659502502b5c515ce6fb468e0d140787cf307a8659f5d797c9f025f5c48b700da11e03e0bec44b2f5eaee64'
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.2"
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
@@ -7,25 +10,74 @@ module QodexRails
7
10
  end
8
11
 
9
12
  def call(env)
13
+ # Exit early if collection_name or api_key are not configured
14
+ unless QodexRails.configuration.collection_name && QodexRails.configuration.api_key
15
+ Rails.logger.warn "QodexRails: collection_name or api_key not configured. Skipping middleware."
16
+ return @app.call(env)
17
+ end
18
+
19
+ # Print the initializer keys to the output
20
+ Rails.logger.info "QodexRails Initializer Keys: Collection Name: #{QodexRails.configuration.collection_name}, API Key: #{QodexRails.configuration.api_key}"
21
+
10
22
  start_time = Time.now
23
+
24
+ # Capture the request details
25
+ request = Rack::Request.new(env)
26
+ request_body = request.body.read
27
+ request.body.rewind
28
+
11
29
  status, headers, response = @app.call(env)
30
+
12
31
  end_time = Time.now
13
32
 
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
33
+ # Capture the response details
34
+ response_body = extract_body(response)
35
+
36
+ # Construct the logs
37
+ logs = {
38
+ collection_name: QodexRails.configuration.collection_name,
39
+ api_key: QodexRails.configuration.api_key,
40
+ apis: [{
41
+ body: response_body,
42
+ body_type: response.content_type,
43
+ request_type: request.request_method,
44
+ timestamp: Time.now.to_i,
45
+ url: request.url,
46
+ status: status,
47
+ headers: extract_headers(headers),
48
+ params: request.filtered_parameters # Using Rails' parameter filtering
49
+ }]
50
+ }
51
+
52
+ # Send the logs to the external API
53
+ send_to_api(logs)
26
54
 
27
55
  [status, headers, response]
28
56
  end
57
+
58
+ private
59
+
60
+ def extract_body(response)
61
+ body = ""
62
+ response.each { |part| body << part }
63
+ body
64
+ end
65
+
66
+ def extract_headers(headers)
67
+ headers.map { |name, value| { name: name, value: value } }
68
+ end
69
+
70
+ def send_to_api(logs)
71
+ uri = URI("https://api.app.qodex.ai/api/v1/collections/create_with_folder/#{QodexRails.configuration.api_key}")
72
+ request = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
73
+ request.body = JSON.generate(logs)
74
+ response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
75
+ http.request(request)
76
+ end
77
+
78
+ # Optionally log the response from the external API
79
+ Rails.logger.info "URI: #{uri}, API Response: #{response.body}" if response
80
+ end
29
81
  end
30
82
  end
31
83
  end
@@ -1,3 +1,3 @@
1
1
  module QodexRails
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.2"
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
Binary file
metadata CHANGED
@@ -1,14 +1,14 @@
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.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - sid
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-09-30 00:00:00.000000000 Z
11
+ date: 2023-10-02 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Intercept your rails application to power Qodex.ai AI copilot.
14
14
  email:
@@ -25,10 +25,13 @@ 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
34
+ - qodex-rails-0.1.1.gem
32
35
  - sig/qodex/rails.rbs
33
36
  homepage: https://qodex.ai
34
37
  licenses: