cherrypie-rails-sdk 0.2.0 → 0.2.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c5a1a118949019d7e762faa9d34d79e6cee9de2ffe98011522bb8602bd021c9e
4
- data.tar.gz: 25ce88b08b5aa8e576427eea481c2a022030ef7832af10b2f7e80b420fc46372
3
+ metadata.gz: 8b4af19ba1aac0997f6eccba30cdab81a410952171eb662e7c81bf9e939a1c9d
4
+ data.tar.gz: c6457da0c2fd176ab0ace0fc1a3501a735afb8a3f781069ab8d2c91c7b38752d
5
5
  SHA512:
6
- metadata.gz: faa981517dc5fea5fcb5ffc9b0ae92a0a7ecdda01298fef458059c69a2ddff8a64ce7c4edca48f24315ec888d0b8d9b8176809cb83e92d80f0f2c3abfef6a124
7
- data.tar.gz: 4a5717a23eb6b779fa3961f17cf64845c3cf4c50497988247bd322ad37fd297104317fba116142154df7e8a04276fed2991385f84560c77890a94bfc7348d9ab
6
+ metadata.gz: 925c575e14f6aa22c63c137b30b5ea30919161fede79d50617bb3bd27c59a889618f2ba2413460102b2e0d4bd630ee4d85a9ed950d658ce9487d3a2fd5d17e27
7
+ data.tar.gz: 655c4fdb2d9bebe8eb04b3304d0d95c62b5993d9285f0ff5c6c9917d057d4faf01f38ed070ba8dbd10aae06a80e3377460cfd35366f7089f57631ad25882d345
data/README.md CHANGED
@@ -22,7 +22,40 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
- TODO: Write usage instructions here
25
+ ```
26
+ # config/application.rb
27
+
28
+ require_relative "boot"
29
+
30
+ require "rails/all"
31
+ require 'cherrypie_rails_sdk'
32
+
33
+ # Require the gems listed in Gemfile, including any gems
34
+ # you've limited to :test, :development, or :production.
35
+ Bundler.require(*Rails.groups)
36
+
37
+ module HelloApp
38
+ class Application < Rails::Application
39
+ # Initialize configuration defaults for originally generated Rails version.
40
+ config.load_defaults 6.1
41
+
42
+ # Configuration for the application, engines, and railties goes here.
43
+ #
44
+ # These settings can be overridden in specific environments using the files
45
+ # in config/environments, which are processed later.
46
+ #
47
+ # config.time_zone = "Central Time (US & Canada)"
48
+ # config.eager_load_paths << Rails.root.join("extras")
49
+
50
+ config.middleware.use(CherrypieRailsSdk::Middleware, "<TOKEN>",
51
+ lambda{ |request| {
52
+ # request: ActionDispatch::Request
53
+ "entityKey"=> "1", "name"=> "RAILS"
54
+ } },
55
+ )
56
+ end
57
+ end
58
+ ```
26
59
 
27
60
  ## Development
28
61
 
@@ -5,10 +5,11 @@ require_relative "cherrypie_rails_sdk/version"
5
5
  module CherrypieRailsSdk
6
6
  class Error < StandardError; end
7
7
  class Middleware
8
- def initialize(app, api_key:, api_url:)
8
+ def initialize(app, api_key, identity_function, api_url: "http://api.cherrypie.app")
9
9
  @app = app
10
10
  @api_key = api_key
11
11
  @api_url = api_url
12
+ @identity_function = identity_function
12
13
  end
13
14
 
14
15
  def call env
@@ -16,19 +17,27 @@ module CherrypieRailsSdk
16
17
  end
17
18
 
18
19
  def _build_request_payload(request)
20
+ headers = {}
21
+ request.headers.each {|key, value|
22
+ headers.merge(key: value)
23
+ }
19
24
  return {
20
- "path": request.fullpath,
21
- "method": request.method,
22
- "headers": request.headers,
23
- "body": request.body,
25
+ "path"=> request.fullpath,
26
+ "method"=> request.method,
27
+ "headers"=> headers,
28
+ "body"=> request.body,
24
29
  }
25
30
  end
26
31
 
27
32
  def _build_response_payload(status, headers, response)
33
+ body = nil
34
+ if response.respond_to?(:body)
35
+ body = response.body
36
+ end
28
37
  return {
29
- "headers": headers,
30
- "body": response.body,
31
- "statusCode": status,
38
+ "headers"=> headers,
39
+ "body"=> body,
40
+ "statusCode"=> status,
32
41
  }
33
42
  end
34
43
 
@@ -37,29 +46,27 @@ module CherrypieRailsSdk
37
46
  @status, @headers, @response = @app.call(env)
38
47
  request_ended_on = Time.now
39
48
  request = ActionDispatch::Request.new(env)
49
+
40
50
  @log_level = :info
51
+ # Rails.logger.send(@log_level, '=' * 50)
52
+ # Rails.logger.send(@log_level, "Request delta time: #{request_ended_on - request_started_on} seconds.")
53
+ # Rails.logger.send(@log_level, '=' * 50)
54
+
55
+ response_time = (request_ended_on - request_started_on) * 1000
41
56
 
42
- Rails.logger.send(@log_level, '=' * 50)
43
- Rails.logger.send(@log_level, "Request delta time: #{request_ended_on - request_started_on} seconds.")
44
- Rails.logger.send(@log_level, '=' * 50)
45
-
57
+ account = @identity_function[request]
46
58
  payload = {
47
59
  "logEntry" => {
48
- "account"=> {
49
- "entityKey"=> "1",
50
- "name"=> "RAILS",
51
- },
60
+ "account"=> account,
52
61
  "clientIP"=> request.ip,
53
- "request"=> _build_request_payload(@request),
62
+ "request"=> _build_request_payload(request),
54
63
  "response"=> _build_response_payload(@status, @headers, @response),
55
- "responseTime"=> 1000, # ms
64
+ "responseTime"=> response_time, # ms
56
65
  }
57
- }.to_json
58
-
59
-
60
- uri = URI('http://cherry-be-staging.herokuapp.com/v1/logs')
66
+ } .to_json
67
+
68
+ uri = URI("#{@api_url}/v1/logs")
61
69
  http = Net::HTTP.new(uri.host, uri.port)
62
- puts(@api_key)
63
70
  request = Net::HTTP::Post.new(
64
71
  uri.request_uri,
65
72
  {
@@ -71,8 +78,7 @@ module CherrypieRailsSdk
71
78
  request.body = payload
72
79
 
73
80
  response = http.request(request)
74
- puts(response.body)
75
-
81
+
76
82
  [@status, @headers, @response]
77
83
  end
78
84
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CherrypieRailsSdk
4
- VERSION = "0.2.0"
4
+ VERSION = "0.2.4"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cherrypie-rails-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Macro Computer Club