Sha256: 0d489e8bbdb0ad29497dd0e849a8b597270e251849a71183df825a133d6a6689

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 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='https://raw.githubusercontent.com/hellostealth/stealth/master/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: params[:service], message: 'Received webhook.')

      # 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)
      )

      headers 'Access-Control-Allow-Origin' => '*',
              'Access-Control-Allow-Methods' => ['OPTIONS', 'GET', 'POST']
      # content_type 'audio/mp3'
      content_type 'application/octet-stream'

      dispatcher.coordinate
    end

    private

      def get_helpers_from_request(request)
        request.env.select do |header, value|
          %w[HTTP_HOST].include?(header)
        end
      end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
stealth-2.0.0.beta5 lib/stealth/server.rb