initial commit
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module FO::HTTP
|
||||
module Error
|
||||
HTTP_ERROR_TYPES = {
|
||||
NotFound: {
|
||||
status: 404,
|
||||
msg: 'We could not find the requested resource...',
|
||||
name: 'Bad Request'
|
||||
},
|
||||
BadRequest: {
|
||||
status: 400,
|
||||
msg: 'We were unable to process your request, check your input and try again later',
|
||||
name: 'Bad Request'
|
||||
},
|
||||
InternalServer: {
|
||||
status: 500,
|
||||
msg: 'Something went very wrong on our end!',
|
||||
name: 'Internal Server Error'
|
||||
}
|
||||
}
|
||||
|
||||
class Base < StandardError
|
||||
attr_reader :status, :msg, :name
|
||||
end
|
||||
|
||||
FO::HTTP::Error::HTTP_ERROR_TYPES.each { |k,v|
|
||||
clazz = Class.new(FO::HTTP::Error::Base) do
|
||||
define_method(:initialize) do
|
||||
super(v[:msg])
|
||||
v.each { |attr, attr_value| self.set }
|
||||
@status = v[:status]
|
||||
@msg = v[:msg]
|
||||
@name = v[:name]
|
||||
end
|
||||
end
|
||||
FO::HTTP::Error.const_set(k, clazz)
|
||||
}
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,37 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rack'
|
||||
require 'json'
|
||||
|
||||
require_relative 'response'
|
||||
|
||||
module FO::HTTP
|
||||
class Request < Rack::Request
|
||||
def content
|
||||
if get? || options?
|
||||
return nil
|
||||
end
|
||||
|
||||
begin
|
||||
body = body.read
|
||||
if media_type.match?(/^application\/json/i)
|
||||
return json
|
||||
elsif media_type.match?(/^application\/form-data/i) ||
|
||||
media_type.match?(/^application\/x-www-form-urlencoded/i)
|
||||
return params
|
||||
end
|
||||
body
|
||||
ensure
|
||||
body.rewind
|
||||
end
|
||||
end
|
||||
|
||||
def path_params
|
||||
self.env['router.params']
|
||||
end
|
||||
|
||||
def new_response(status)
|
||||
FO::HTTP::Response.new(self).status(status)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,50 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rack'
|
||||
require 'json'
|
||||
|
||||
require_relative '../renderer'
|
||||
|
||||
module FO::HTTP
|
||||
class Response < Rack::Response
|
||||
attr_reader :request
|
||||
|
||||
def initialize(request)
|
||||
super('')
|
||||
@request = request
|
||||
end
|
||||
|
||||
def json(thingy)
|
||||
body(JSON.generate(thingy))
|
||||
content_type('application/json; charset=utf-8')
|
||||
self
|
||||
end
|
||||
|
||||
def template(template_name, locals = {})
|
||||
path = File.expand_path("#{__dir__}/../views/#{template_name}.erb")
|
||||
layout_name = locals['layout'].nil? ? 'default' : locals['layout']
|
||||
layout_path = File.expand_path("#{__dir__}/../views/layouts/#{layout_name}.erb")
|
||||
|
||||
body(FO::Renderer.new(layout_path, path, locals).render)
|
||||
|
||||
content_type('text/html; charset=utf-8')
|
||||
self
|
||||
end
|
||||
|
||||
def status(s)
|
||||
@status = s
|
||||
self
|
||||
end
|
||||
|
||||
def body(body)
|
||||
write(body)
|
||||
self
|
||||
end
|
||||
|
||||
def content_type(type)
|
||||
set_header('Content-Type', type)
|
||||
self
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,21 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module FO::HTTP
|
||||
class Session
|
||||
def initialize(env)
|
||||
@env = env
|
||||
end
|
||||
|
||||
def hash
|
||||
@env['rack.session']
|
||||
end
|
||||
|
||||
def set!(k,v)
|
||||
hash[k] = v
|
||||
end
|
||||
|
||||
def get(k)
|
||||
hash[k]
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user