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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: afd1782fb8646ccdaa8bc47b58691931debc6df956d9e3bad457f035eeea4d4a
4
- data.tar.gz: 04d51804252a49e9561f231668f433453b1a07897731db4c0e72985caaf6b6a1
3
+ metadata.gz: 291c6c48336f17f755789775225981c8894bf5d5bb899ecb2d19d6c5ed60870c
4
+ data.tar.gz: b2defc7724da013353601e660ddf25c3d8b8d5de3490f980462974aace4b079b
5
5
  SHA512:
6
- metadata.gz: 5cb7b41e5cc61a582923f196bd904e3d4ceb5996ff51c2241196d693e6348d38714836966dfe8ccf8cb75389edf5c8522de646c2fafdf6be0d5af875562f61bc
7
- data.tar.gz: 19d8133cb1cf8bd40957bbc0c7ba57d87d99a807cc10661bf89da5c2c6d71cf59bee5cf1723644ffe89ff4df1bb5915f061b9cd84c7770012578abbd96ab046b
6
+ metadata.gz: 3ef8d8434dd87c38099c2a0bd10993e28a428f7620ff2d7840242e1165760c2a472f4f2b64b123becb16956d60285125032f48ee64304526facdb6843e3f7f3b
7
+ data.tar.gz: c72751c72136bde7c27a3e04fd7e7ae3836d1354b082b1b7903b89796b7d8ec4fdd85423fe98ef459c271fd669f44396b550d60e47344c05226c00c88cc16be3
data/README.md CHANGED
@@ -1,15 +1,13 @@
1
1
  # CherrypieRailsSdk
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/cherrypie_rails_sdk`. To experiment with that code, run `bin/console` for an interactive prompt.
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 'cherrypie_rails_sdk'
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 cherrypie_rails_sdk
19
+ $ gem install cherrypie-rails-sdk
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
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
 
@@ -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
  {
@@ -69,10 +76,10 @@ module CherrypieRailsSdk
69
76
  }
70
77
  )
71
78
  request.body = payload
72
-
73
- response = http.request(request)
74
- puts(response.body)
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CherrypieRailsSdk
4
- VERSION = "0.2.1"
4
+ VERSION = "0.2.5"
5
5
  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.1
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-12 00:00:00.000000000 Z
11
+ date: 2021-08-16 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: CherryPie Rails SDK
14
14
  email: