cherrypie-rails-sdk 0.1.0 → 0.2.3

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: c8d85bc66fbe22cda53c392c416a6432e1c1f6a6853ac09cfb5f2e829b1d11f2
4
+ data.tar.gz: bdecc49fdd467bcdd2fea007d129a435965ea67f6e5b87d72c55e23184389fe8
5
5
  SHA512:
6
- metadata.gz: 6e0037830812c28ad4327bd60db9f9a3d9ea4dffd01f3a94f4d0e99970ae21eb3960d6fa99c5863589b31d1a4bd7fac68d85947629f0e285afc92b78c65b6bed
7
- data.tar.gz: b8886fe15164334c7f700486364e3987f74cc678fbee644e08b2d30e69338061ebaab56c040332a63a9104c76153f7d3bfe47d94a29a21d7e833876bfb2c6ca6
6
+ metadata.gz: 8bdfbe7370dbe5a1722275ef7b6e983ff0092aa7c20bf5653f2a78ee3b9b952bc9eed6447dccbcbfe294c00a2d89e5ee83cda525228c48c8554833d9f1e35cd5
7
+ data.tar.gz: ce87042d09062ea3b02f64ff09858e63f9c793240efacf3352bdf16aacb8c1b5ce5d16b439d50a8cfd088dc46b4d0c173f126c3e1a2807f0afe705a6bd93a487
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
 
@@ -4,19 +4,79 @@ 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, identity_function, api_url: "http://api.cherrypie.app")
9
+ @app = app
10
+ @api_key = api_key
11
+ @api_url = api_url
12
+ @identity_function = identity_function
12
13
  end
14
+
15
+ def call env
16
+ dup._call env
17
+ end
18
+
19
+ def _build_request_payload(request)
20
+ headers = {}
21
+ request.headers.each {|key, value|
22
+ headers.merge(key: value)
23
+ }
24
+ return {
25
+ "path"=> request.fullpath,
26
+ "method"=> request.method,
27
+ "headers"=> headers,
28
+ "body"=> request.body,
29
+ }
30
+ end
31
+
32
+ def _build_response_payload(status, headers, response)
33
+ body = response.respond_to?(:body)
34
+ return {
35
+ "headers"=> headers,
36
+ "body"=> body,
37
+ "statusCode"=> status,
38
+ }
39
+ end
40
+
41
+ def _call env
42
+ request_started_on = Time.now
43
+ @status, @headers, @response = @app.call(env)
44
+ request_ended_on = Time.now
45
+ request = ActionDispatch::Request.new(env)
46
+
47
+ @log_level = :info
48
+ # Rails.logger.send(@log_level, '=' * 50)
49
+ # Rails.logger.send(@log_level, "Request delta time: #{request_ended_on - request_started_on} seconds.")
50
+ # Rails.logger.send(@log_level, '=' * 50)
51
+
52
+ response_time = (request_ended_on - request_started_on) * 1000
53
+
54
+ account = @identity_function[request]
55
+ payload = {
56
+ "logEntry" => {
57
+ "account"=> account,
58
+ "clientIP"=> request.ip,
59
+ "request"=> _build_request_payload(request),
60
+ "response"=> _build_response_payload(@status, @headers, @response),
61
+ "responseTime"=> response_time, # ms
62
+ }
63
+ } .to_json
64
+
65
+ uri = URI("#{@api_url}/v1/logs")
66
+ http = Net::HTTP.new(uri.host, uri.port)
67
+ request = Net::HTTP::Post.new(
68
+ uri.request_uri,
69
+ {
70
+ 'Content-Type' => 'application/json',
71
+ 'Authorization' => "Bearer #{@api_key}",
72
+ 'Accept'=>'application/json',
73
+ }
74
+ )
75
+ request.body = payload
76
+
77
+ response = http.request(request)
13
78
 
14
- def portray(food)
15
- if food.downcase == @food
16
- "Gross!"
17
- else
18
- "Delicious!"
19
- end
79
+ [@status, @headers, @response]
20
80
  end
21
81
  end
22
82
  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.3"
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.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Macro Computer Club