cherrypie-rails-sdk 0.1.0 → 0.2.0

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: 18ac1918de40cc2fe4f9cde745cfb296287aead5f586f965e64c8373afe73062
4
- data.tar.gz: 34cfdc4cb98235a359499084215c5f61001580dfabf29fd1151a415384384141
3
+ metadata.gz: c5a1a118949019d7e762faa9d34d79e6cee9de2ffe98011522bb8602bd021c9e
4
+ data.tar.gz: 25ce88b08b5aa8e576427eea481c2a022030ef7832af10b2f7e80b420fc46372
5
5
  SHA512:
6
- metadata.gz: 6e0037830812c28ad4327bd60db9f9a3d9ea4dffd01f3a94f4d0e99970ae21eb3960d6fa99c5863589b31d1a4bd7fac68d85947629f0e285afc92b78c65b6bed
7
- data.tar.gz: b8886fe15164334c7f700486364e3987f74cc678fbee644e08b2d30e69338061ebaab56c040332a63a9104c76153f7d3bfe47d94a29a21d7e833876bfb2c6ca6
6
+ metadata.gz: faa981517dc5fea5fcb5ffc9b0ae92a0a7ecdda01298fef458059c69a2ddff8a64ce7c4edca48f24315ec888d0b8d9b8176809cb83e92d80f0f2c3abfef6a124
7
+ data.tar.gz: 4a5717a23eb6b779fa3961f17cf64845c3cf4c50497988247bd322ad37fd297104317fba116142154df7e8a04276fed2991385f84560c77890a94bfc7348d9ab
@@ -4,19 +4,76 @@ require_relative "cherrypie_rails_sdk/version"
4
4
 
5
5
  module CherrypieRailsSdk
6
6
  class Error < StandardError; end
7
- # Your code goes here...
8
- class Food
9
- def initialize()
10
- puts("hello world")
11
- @food = "food1"
7
+ class Middleware
8
+ def initialize(app, api_key:, api_url:)
9
+ @app = app
10
+ @api_key = api_key
11
+ @api_url = api_url
12
12
  end
13
-
14
- def portray(food)
15
- if food.downcase == @food
16
- "Gross!"
17
- else
18
- "Delicious!"
19
- end
13
+
14
+ def call env
15
+ dup._call env
16
+ end
17
+
18
+ def _build_request_payload(request)
19
+ return {
20
+ "path": request.fullpath,
21
+ "method": request.method,
22
+ "headers": request.headers,
23
+ "body": request.body,
24
+ }
25
+ end
26
+
27
+ def _build_response_payload(status, headers, response)
28
+ return {
29
+ "headers": headers,
30
+ "body": response.body,
31
+ "statusCode": status,
32
+ }
33
+ end
34
+
35
+ def _call env
36
+ request_started_on = Time.now
37
+ @status, @headers, @response = @app.call(env)
38
+ request_ended_on = Time.now
39
+ request = ActionDispatch::Request.new(env)
40
+ @log_level = :info
41
+
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
+
46
+ payload = {
47
+ "logEntry" => {
48
+ "account"=> {
49
+ "entityKey"=> "1",
50
+ "name"=> "RAILS",
51
+ },
52
+ "clientIP"=> request.ip,
53
+ "request"=> _build_request_payload(@request),
54
+ "response"=> _build_response_payload(@status, @headers, @response),
55
+ "responseTime"=> 1000, # ms
56
+ }
57
+ }.to_json
58
+
59
+
60
+ uri = URI('http://cherry-be-staging.herokuapp.com/v1/logs')
61
+ http = Net::HTTP.new(uri.host, uri.port)
62
+ puts(@api_key)
63
+ request = Net::HTTP::Post.new(
64
+ uri.request_uri,
65
+ {
66
+ 'Content-Type' => 'application/json',
67
+ 'Authorization' => "Bearer #{@api_key}",
68
+ 'Accept'=>'application/json',
69
+ }
70
+ )
71
+ request.body = payload
72
+
73
+ response = http.request(request)
74
+ puts(response.body)
75
+
76
+ [@status, @headers, @response]
20
77
  end
21
78
  end
22
79
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CherrypieRailsSdk
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
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.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Macro Computer Club