cherrypie-rails-sdk 0.2.1 → 0.2.5
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/README.md +37 -6
- data/lib/cherrypie_rails_sdk.rb +34 -27
- data/lib/cherrypie_rails_sdk/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 291c6c48336f17f755789775225981c8894bf5d5bb899ecb2d19d6c5ed60870c
|
4
|
+
data.tar.gz: b2defc7724da013353601e660ddf25c3d8b8d5de3490f980462974aace4b079b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3ef8d8434dd87c38099c2a0bd10993e28a428f7620ff2d7840242e1165760c2a472f4f2b64b123becb16956d60285125032f48ee64304526facdb6843e3f7f3b
|
7
|
+
data.tar.gz: c72751c72136bde7c27a3e04fd7e7ae3836d1354b082b1b7903b89796b7d8ec4fdd85423fe98ef459c271fd669f44396b550d60e47344c05226c00c88cc16be3
|
data/README.md
CHANGED
@@ -1,15 +1,13 @@
|
|
1
1
|
# CherrypieRailsSdk
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
Cherrypie Rails SDK.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
9
7
|
Add this line to your application's Gemfile:
|
10
8
|
|
11
9
|
```ruby
|
12
|
-
gem '
|
10
|
+
gem 'cherrypie-rails-sdk', '~> 0.2.4'
|
13
11
|
```
|
14
12
|
|
15
13
|
And then execute:
|
@@ -18,11 +16,44 @@ And then execute:
|
|
18
16
|
|
19
17
|
Or install it yourself as:
|
20
18
|
|
21
|
-
$ gem install
|
19
|
+
$ gem install cherrypie-rails-sdk
|
22
20
|
|
23
21
|
## Usage
|
24
22
|
|
25
|
-
|
23
|
+
```
|
24
|
+
# config/application.rb
|
25
|
+
|
26
|
+
require_relative "boot"
|
27
|
+
|
28
|
+
require "rails/all"
|
29
|
+
require 'cherrypie_rails_sdk'
|
30
|
+
|
31
|
+
# Require the gems listed in Gemfile, including any gems
|
32
|
+
# you've limited to :test, :development, or :production.
|
33
|
+
Bundler.require(*Rails.groups)
|
34
|
+
|
35
|
+
module HelloApp
|
36
|
+
class Application < Rails::Application
|
37
|
+
# Initialize configuration defaults for originally generated Rails version.
|
38
|
+
config.load_defaults 6.1
|
39
|
+
|
40
|
+
# Configuration for the application, engines, and railties goes here.
|
41
|
+
#
|
42
|
+
# These settings can be overridden in specific environments using the files
|
43
|
+
# in config/environments, which are processed later.
|
44
|
+
#
|
45
|
+
# config.time_zone = "Central Time (US & Canada)"
|
46
|
+
# config.eager_load_paths << Rails.root.join("extras")
|
47
|
+
|
48
|
+
config.middleware.use(CherrypieRailsSdk::Middleware, "<TOKEN>",
|
49
|
+
lambda{ |request| {
|
50
|
+
# request: ActionDispatch::Request
|
51
|
+
"entityKey"=> "1", "name"=> "RAILS"
|
52
|
+
} },
|
53
|
+
)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
```
|
26
57
|
|
27
58
|
## Development
|
28
59
|
|
data/lib/cherrypie_rails_sdk.rb
CHANGED
@@ -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"
|
21
|
-
"method"
|
22
|
-
"headers"
|
23
|
-
"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"
|
30
|
-
"body"
|
31
|
-
"statusCode"
|
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
|
-
|
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(
|
62
|
+
"request"=> _build_request_payload(request),
|
54
63
|
"response"=> _build_response_payload(@status, @headers, @response),
|
55
|
-
"responseTime"=>
|
64
|
+
"responseTime"=> response_time, # ms
|
56
65
|
}
|
57
|
-
|
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
|
{
|
@@ -69,10 +76,10 @@ module CherrypieRailsSdk
|
|
69
76
|
}
|
70
77
|
)
|
71
78
|
request.body = payload
|
72
|
-
|
73
|
-
response
|
74
|
-
|
75
|
-
|
79
|
+
# Only log non-300 statuses.
|
80
|
+
if !(response.status >= 300 && response.status <= 399)
|
81
|
+
response = http.request(request)
|
82
|
+
|
76
83
|
[@status, @headers, @response]
|
77
84
|
end
|
78
85
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cherrypie-rails-sdk
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Macro Computer Club
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-08-
|
11
|
+
date: 2021-08-16 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: CherryPie Rails SDK
|
14
14
|
email:
|