Class: Yardie::Middleware
- Inherits:
-
Object
- Object
- Yardie::Middleware
- Defined in:
- lib/yardie/middleware/middleware.rb
Constant Summary collapse
- DEV_DELIVERY_URI =
URI('http://localhost:3000/runtime/mailbox')
- PROD_DELIVERY_PATH =
URI('https://rocky-sierra-12787.herokuapp.com/runtime/mailbox')
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app) ⇒ Middleware
constructor
A new instance of Middleware.
Constructor Details
#initialize(app) ⇒ Middleware
Returns a new instance of Middleware.
9 10 11 |
# File 'lib/yardie/middleware/middleware.rb', line 9 def initialize(app) @app = app end |
Instance Method Details
#call(env) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/yardie/middleware/middleware.rb', line 13 def call(env) return @app.call(env) if ENV['YARDIE_DISABLE'] && ENV['YARDIE_DISABLE'].casecmp('TRUE').zero? return @app.call(env) if Rails.env.development? && env['REQUEST_PATH'].eql?('/runtime/mailbox') # ignore calls to yourself to prevent infinite loops return @app.call(env) unless env['CONTENT_TYPE'].eql?('application/json') return @app.call(env) if ENV['YARDIE_AUTH_KEY'].blank? local_mode = ENV['YARDIE_LOCAL'] && ENV['YARDIE_LOCAL'].casecmp('TRUE').zero? ct = Thread.current.object_id error = false payload = { auth_key: ENV['YARDIE_AUTH_KEY'], request: { id: env['action_dispatch.request_id'], path: env['REQUEST_PATH'], query: env['QUERY_STRING'], method: env['REQUEST_METHOD'], body: nil, trace: [] }, controller: { name: nil, method: nil }, response: { status: nil, body: nil } } req = Rack::Request.new(env) if req.post? begin request_body = req.body.read payload[:request][:body] = JSON.parse(request_body.delete("\n")) rescue StandardError error = true ensure req.body.rewind end end trace = TracePoint.new(:call) do |tp| klass = tp.defined_class.to_s if tp.path.to_s.start_with?(Rails.root.to_s) && Thread.current.object_id == ct # tracepoint emits events for ALL threads, so check that the thread that caused the event to be # emitted is the one we are interested in p "#{tp.defined_class} - #{tp.method_id} - #{tp.path}" if local_mode if klass.end_with?('Controller') && !klass.start_with?('Devise') payload[:controller][:name] = tp.defined_class.to_s payload[:controller][:method] = tp.method_id.to_s end payload[:request][:trace] << { class: tp.defined_class.to_s, method: tp.method_id.to_s } end end begin trace.enable status, headers, response = @app.call(env) rescue StandardError status = 500 ensure trace.disable end payload[:response][:status] = status begin unless response.body.blank? payload[:response][:body] = JSON.parse(response.body) end rescue error = true end unless error if payload[:controller][:name].present? && payload[:controller][:method].present? # post the payload to Construction Yard servers Thread.new { uri = local_mode ? DEV_DELIVERY_URI : PROD_DELIVERY_PATH http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true if uri.scheme == 'https' req = Net::HTTP::Post.new(uri.path, 'Content-Type' => 'application/json') req.body = { payload: payload }.to_json http.request(req) } end end [status, headers, response] end |