module Artoo::Api::RouteHelpers::InstanceMethods
Public Class Methods
force_encoding(data, encoding = default_encoding)
click to toggle source
# File lib/artoo/api/route_helpers.rb, line 217 def self.force_encoding(data, encoding = default_encoding) return if data == settings || data.is_a?(Tempfile) if data.respond_to? :force_encoding data.force_encoding(encoding).encode! elsif data.respond_to? :each_value data.each_value { |v| force_encoding(v, encoding) } elsif data.respond_to? :each data.each { |v| force_encoding(v, encoding) } end data end
Public Instance Methods
dispatch!(connection, req)
click to toggle source
Handle the request
# File lib/artoo/api/route_helpers.rb, line 137 def dispatch!(connection, req) resp = catch(:halt) do try_static! connection, req route! connection, req end return unless connection.response_state == :headers if resp && !resp.nil? status, body = resp begin if @is_static req.respond status, body else req.respond status, {'Content-Type' => 'application/json'}, body end rescue Errno::EAGAIN retry end else @error ||= "NOT FOUND" req.respond :not_found, {'Content-Type' => 'application/json'}, {error: @error}.to_json @error = nil end end
force_encoding(*args)
click to toggle source
Fixes encoding issues by
-
defaulting to UTF-8
-
casting params to Encoding.default_external
The latter might not be necessary if Rack handles it one day. Keep an eye on Rack’s LH #100.
# File lib/artoo/api/route_helpers.rb, line 215 def force_encoding(*args) settings.force_encoding(*args) end
halt(*response)
click to toggle source
Exit the current block, halts any further processing of the request, and returns the specified response.
# File lib/artoo/api/route_helpers.rb, line 166 def halt(*response) response = response.first if response.length == 1 throw :halt, response end
route!(connection, req) { |self, values| ... }
click to toggle source
# File lib/artoo/api/route_helpers.rb, line 183 def route!(connection, req) if routes = self.class.routes[req.method] routes.each do |pattern, keys, conditions, block| route = req.url next unless match = pattern.match(route) values = match.captures.to_a.map { |v| URI.decode_www_form_component(v) if v } if values.any? params = {} keys.zip(values) { |k,v| Array === params[k] ? params[k] << v : params[k] = v if v } @params = params end @connection = connection @req = req begin body = block ? block[self, values] : yield(self, values) halt [:ok, body] rescue Exception => e p [:e, e] end end end nil end
try_static!(connection, req)
click to toggle source
# File lib/artoo/api/route_helpers.rb, line 171 def try_static!(connection, req) fpath = req.url == '/' ? 'index.html' : req.url[1..-1] filepath = File.expand_path(fpath, self.class.static_path) if File.file?(filepath) # TODO: stream this? data = open(filepath).read @is_static = true halt :ok, data end @is_static = false end