Class: Shatter::Web::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/shatter/web/server.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.applicationObject

Returns the value of attribute application.



10
11
12
# File 'lib/shatter/web/server.rb', line 10

def application
  @application
end

Class Method Details

.call(env) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/shatter/web/server.rb', line 13

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

.handle_operation_request(env) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/shatter/web/server.rb', line 25

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

.handle_response_for_request(uuid) ⇒ Object



44
45
46
47
48
49
# File 'lib/shatter/web/server.rb', line 44

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

.service_callback(_time, _value, error) ⇒ Object



38
39
40
41
42
# File 'lib/shatter/web/server.rb', line 38

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

  Shatter.logger.error error.to_s
end