Sha256: a45d3bc13fce001277c9864541abf632e2acb3acdc06760658159107c1aa6d57

Contents?: true

Size: 1.43 KB

Versions: 2

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

require "securerandom"
require "json"

module Shatter
  module Web
    class Server
      class << self
        attr_accessor :application
      end

      def self.call(env)
        request = Rack::Request.new(env)
        if env["PATH_INFO"] == "/callbacks"
          uuid = env["QUERY_STRING"].split("=")[1]
          handle_response_for_request(uuid)
        elsif request.post?
          handle_operation_request(env)
        else
          [404, {}, []]
        end
      end

      def self.handle_operation_request(env)
        request = Rack::Request.new(env)
        params = begin
          JSON.parse(request.body.read, { symbolize_names: true })
        rescue StandardError
          {}
        end
        uuid = SecureRandom.uuid
        future = application.new.async.route(uuid, request.path, params)
        future.add_observer(:service_callback, self)
        [200, { "delay" => Shatter::Config.initial_delay, "location" => "/callbacks?uuid=#{uuid}" }, []]
      end

      def self.service_callback(_time, _value, error)
        return if error.nil?

        Shatter.logger.error error.to_s
      end

      def self.handle_response_for_request(uuid)
        response = application.new.response_for(uuid)
        return [200, {}, [response.to_json]] unless response.nil?

        [200, { "delay" => Shatter::Config.missing_result_delay, "location" => "/callbacks?uuid=#{uuid}" }, []]
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
shatter-rb-0.1.1 lib/shatter/web/server.rb
shatter-rb-0.1.0 lib/shatter/web/server.rb