Class: Unmagic::Icon::Web
- Inherits:
-
Object
- Object
- Unmagic::Icon::Web
- Defined in:
- lib/unmagic/icon/web.rb
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.app ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/unmagic/icon/web.rb', line 16 def self.app @app ||= Rack::Builder.new do public_path = File.("web/public", __dir__) use Rack::Static, urls: [ "/public" ], root: File.dirname(public_path) run Unmagic::Icon::Web.new end.to_app end |
.call(env) ⇒ Object
12 13 14 |
# File 'lib/unmagic/icon/web.rb', line 12 def self.call(env) app.call(env) end |
Instance Method Details
#call(env) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/unmagic/icon/web.rb', line 24 def call(env) @request = Rack::Request.new(env) @libraries = Unmagic::Icon::Library::Registry.all case @request.path_info when "/" # Redirect to first library first_library = @libraries.first.name.to_param if first_library.nil? [ 404, { "content-type" => "text/plain" }, [ "No libraries found" ] ] else [ 302, { "location" => url(first_library, @request.params) }, [] ] end else library_name = @request.path_info.delete_prefix("/") begin @selected_library = Unmagic::Icon::Library::Registry.find(library_name) rescue Unmagic::Icon::LibraryNotError return [ 404, { "content-type" => "text/plain" }, [ "Library not found: #{@selected_library}" ] ] end template_path = File.("web/views/layout.html.erb", __dir__) template = ERB.new(File.read(template_path)) html = template.result(binding) [ 200, { "content-type" => "text/html; charset=utf-8" }, [ html ] ] end end |
#escape(text) ⇒ Object
54 55 56 |
# File 'lib/unmagic/icon/web.rb', line 54 def escape(text) CGI.escapeHTML(text.to_s) end |
#url(path_parts, params_hash = nil) ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/unmagic/icon/web.rb', line 58 def url(path_parts, params_hash = nil) url = [ @request.env["SCRIPT_NAME"], *path_parts ].compact.join("/") url = url.gsub(/\/\//, "/") if params_hash "#{url}?#{Rack::Utils.build_query(params_hash)}" else url end end |