Sha256: c81913240a97fa686d73f55c9a473a2fa0fe1660e7888cf31a1681b9ec1fa14c

Contents?: true

Size: 815 Bytes

Versions: 1

Compression:

Stored size: 815 Bytes

Contents

class HmlonsWeb::Controller
  RESPONSE_TYPES = {
    text: ['text/plain', ->(c) { c.to_s }],
    json: ['application/json', ->(c) { Oj.dump(c) }]
  }.freeze

  def call(env)
    @env = env
    @request = Rack::Request.new(env)
    @request.params.merge!(env['router.params'] || {})
    send(@action_name)
    [200, @response_headers, [@response_body]]
  end

  def self.action(action_name)
    proc { |env| new(action_name).call(env) }
  end

private
  attr_reader :request

  def initialize(action_name)
    @action_name = action_name
  end

  def params
    request.params
  end

  def response(type, content)
    @response_headers ||= {}
    @response_headers.merge!('Content-Type' => RESPONSE_TYPES[type][0])
    @response_body = RESPONSE_TYPES[type][1].call(content)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hmlons_web-0.2.1 lib/hmlons_web/controller.rb