Class: Alephant::Publisher::Request::Request

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/alephant/publisher/request.rb

Constant Summary collapse

DEFAULT_CONTENT_TYPE =
{ "Content-Type" => "text/html" }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(processor, data_mapper_factory, opts) ⇒ Request

Returns a new instance of Request.



23
24
25
26
27
# File 'lib/alephant/publisher/request.rb', line 23

def initialize(processor, data_mapper_factory, opts)
  @processor           = processor
  @data_mapper_factory = data_mapper_factory
  @opts                = opts
end

Instance Attribute Details

#data_mapper_factoryObject (readonly)

Returns the value of attribute data_mapper_factory.



19
20
21
# File 'lib/alephant/publisher/request.rb', line 19

def data_mapper_factory
  @data_mapper_factory
end

#optsObject (readonly)

Returns the value of attribute opts.



19
20
21
# File 'lib/alephant/publisher/request.rb', line 19

def opts
  @opts
end

#processorObject (readonly)

Returns the value of attribute processor.



19
20
21
# File 'lib/alephant/publisher/request.rb', line 19

def processor
  @processor
end

Instance Method Details

#call(env) ⇒ Object



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
# File 'lib/alephant/publisher/request.rb', line 29

def call(env)
  req      = Rack::Request.new(env)
  response = Rack::Response.new("<h1>Not Found</h1>", 404, DEFAULT_CONTENT_TYPE)

  case req.path_info
  when /status$/
    response = Rack::Response.new('ok', 200, DEFAULT_CONTENT_TYPE)
  when /component\/(?<id>[^\/]+)$/
    component_id = $~['id']
    logger.info "Component request for: #{component_id} with options: #{req.params.inspect}"

    response = Rack::Response.new(
      template_data(component_id, req.params),
      200,
      DEFAULT_CONTENT_TYPE
    )
  end

  response.finish
rescue InvalidApiStatus => e
  error_response(e, e.status)
rescue ApiError, ConnectionFailed => e
  error_response(e, 502)
rescue InvalidComponent => e
  error_response(e, 404)
rescue Exception => e
  error_response e
end