Sha256: c859c3a4422c2ccf8a2e4b57a85a67c0fb1bfc5abc193bd6a2c41bd603d51c58

Contents?: true

Size: 1.39 KB

Versions: 4

Compression:

Stored size: 1.39 KB

Contents

# coding: utf-8
# frozen_string_literal: true

require 'sinatra/base'
require 'multi_json'

module Stealth
  class Server < Sinatra::Base

    def self.get_or_post(url, &block)
      get(url, &block)
      post(url, &block)
    end

    get '/' do
      <<~WELCOME
        <html>
          <head>
            <title>Stealth</title>
          </head>
          <body>
            <center>
              <a href='https://hellostealth.org'>
                <img src='http://assets.blackops.nyc/stealth/logo.svg' height='120' alt='Stealth Logo' aria-label='hellostealth.org' />
              </a>
            </center>
          </body>
        </html>
      WELCOME
    end

    get_or_post '/incoming/:service' do
      Stealth::Logger.l(topic: "incoming", message: "Received webhook from #{params[:service]}")

      # JSON params need to be parsed and added to the params
      if request.env['CONTENT_TYPE']&.match(/application\/json/i)
        json_params = MultiJson.load(request.body.read)
        params.merge!(json_params)
      end

      dispatcher = Stealth::Dispatcher.new(
        service: params[:service],
        params: params,
        headers: get_helpers_from_request(request)
      )

      dispatcher.coordinate
    end

    private

      def get_helpers_from_request(request)
        request.env.reject do |header, value|
          header.match(/rack\.|puma\.|sinatra\./)
        end
      end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
stealth-1.1.6 lib/stealth/server.rb
stealth-1.1.5 lib/stealth/server.rb
stealth-1.1.4 lib/stealth/server.rb
stealth-1.1.3 lib/stealth/server.rb