lib/rocketio.rb in rocketio-0.0.0 vs lib/rocketio.rb in rocketio-0.0.1
- old
+ new
@@ -1,5 +1,133 @@
-require "rocketio/version"
+# core
+require 'uri'
+require 'stringio'
+require 'json'
+require 'cgi'
+require 'digest'
+require 'forwardable'
+# gems
+require 'rack'
+require 'mustache'
+require 'tilt'
-module Rocketio
- # Your code goes here...
+module RocketIO
+ extend self
+
+ ENVIRONMENTS = {
+ 'development' => :development,
+ 'testing' => :testing,
+ 'stage' => :stage,
+ 'production' => :production
+ }.freeze
+
+ environment = ENV['APP_ENV'] || ENV['RACK_ENV'] || ENVIRONMENTS.keys[0]
+ unless ENVIRONMENT = ENVIRONMENTS[environment]
+ raise(StandardError, '%s environment not supported. Use one of %s' % [environment.inspect, ENVIRONMENTS.keys*', '])
+ end
+
+ def environment
+ ENVIRONMENT
+ end
+
+ ENVIRONMENTS.each_value do |env|
+ define_method(env.to_s + '?') {ENVIRONMENT == env}
+ end
+
+ GET = 'GET'.freeze
+ POST = 'POST'.freeze
+ PUT = 'PUT'.freeze
+ DELETE = 'DELETE'.freeze
+ HEAD = 'HEAD'.freeze
+
+ REQUEST_METHODS = {
+ GET => :get,
+ POST => :post,
+ PUT => :put,
+ DELETE => :delete,
+ HEAD => :head
+ }.freeze
+
+ EMPTY_STRING = ''.freeze
+ EMPTY_STRING_PROC = proc {RocketIO::EMPTY_STRING}
+ EMPTY_ARRAY = [].freeze
+ EMPTY_HASH = {}.freeze
+
+ SLASH = '/'.freeze
+ QUERY_PREFIX = '?'.freeze
+
+ CONTENT_TYPE = 'Content-Type'.freeze
+ DEFAULT_CONTENT_TYPE = 'text/html'.freeze
+ CONTENT_LENGTH = 'Content-Length'.freeze
+ CONTENT_DISPOSITION = 'Content-Disposition'.freeze
+ APPLICATION_OCTET_STREAM = 'application/octet-stream'.freeze
+ DEFAULT_AUTH_REALM = 'AccessRestricted'.freeze
+ DEFAULT_TOKEN_AUTH_REALM = 'Application'.freeze
+
+ REQUEST_METHOD = 'REQUEST_METHOD'.freeze
+ PATH_INFO = 'PATH_INFO'.freeze
+ HTTP_ACCEPT = 'HTTP_ACCEPT'.freeze
+ REMOTE_USER = 'REMOTE_USER'.freeze
+
+ HTTP_CONNECTION = 'HTTP_CONNECTION'.freeze
+ HTTP_AUTHORIZATION_KEYS = ['HTTP_AUTHORIZATION', 'X-HTTP_AUTHORIZATION', 'X_HTTP_AUTHORIZATION'].map(&:freeze).freeze
+ HTTP_AUTHORIZATION_MOCKS = {
+ basic: 'Basic Og=='.freeze,
+ digest: 'Digest opaque="", qop="auth", uri="/"'.freeze
+ }.freeze
+ HTTP_UPGRADE = 'HTTP_UPGRADE'.freeze
+ UPGRADE = 'upgrade'.freeze
+ WEBSOCKET = 'websocket'.freeze
+ HTTP_1_1 = 'HTTP/1.1'.freeze
+ HTTP_VERSION = 'HTTP_VERSION'.freeze
+ HTTP_X_FORWARDED_HOST = 'HTTP_X_FORWARDED_HOST'.freeze
+ HTTP_IF_MATCH = 'HTTP_IF_MATCH'.freeze
+ HTTP_IF_NONE_MATCH = 'HTTP_IF_NONE_MATCH'.freeze
+ HTTP_IF_MODIFIED_SINCE = 'HTTP_IF_MODIFIED_SINCE'.freeze
+ HTTP_IF_UNMODIFIED_SINCE = 'HTTP_IF_UNMODIFIED_SINCE'.freeze
+ HTTP_X_REQUESTED_WITH = 'HTTP_X_REQUESTED_WITH'.freeze
+ XML_HTTP_REQUEST = 'XMLHttpRequest'.freeze
+
+ LOCATION = 'Location'.freeze
+ CACHE_CONTROL = 'Cache-Control'.freeze
+ EXPIRES = 'Expires'.freeze
+ LAST_MODIFIED = 'Last-Modified'.freeze
+ ETAG = 'ETag'.freeze
+ ETAG_KINDS = [:strong, :weak].freeze
+
+ DROP_BODY_RESPONSES = {204 => true, 205 => true, 304 => true}.freeze
+
+ ERROR_TEMPLATES = begin
+ path = File.expand_path('../rocketio/error_templates/', __FILE__)
+ templates = {layout: Class.new(Mustache) {self.template = File.read(path + '/layout.html').freeze}}
+ Dir[path + '/*.html'].each_with_object(templates) do |file,templates|
+ next if file =~ /layout\.html\z/
+ template = File.basename(file, File.extname(file)).to_i
+ templates[template] = Class.new(Mustache) {self.template = File.read(file).freeze}
+ end
+ end.freeze
+
+ BEFORE_FORMAT = 'before_%s'.freeze
+ AROUND_FORMAT = 'around_%s'.freeze
+ AFTER_FORMAT = 'after_%s'.freeze
+ ROUTE_INSPECT_FORMAT = '#<RocketIO::Route:%s>'.freeze
+ ENGINE_CONST_FORMAT = '%sTemplate'.freeze
+ TEMPLATE_PATH_FORMAT = '%s/%s.%s'.freeze
+
+ FOUND_TEMPLATES = {}
+ READ_TEMPLATES = {}
+ COMPILED_TEMPLATES = {}
+
+ DEFAULT_ENGINE = [Tilt::ERBTemplate, [].freeze].freeze
+
+ def controllers
+ @controllers ||= []
+ end
end
+
+require 'rocketio/util'
+require 'rocketio/exceptions'
+require 'rocketio/controller'
+require 'rocketio/router'
+require 'rocketio/application'
+
+RocketIO::Controller.map(RocketIO::SLASH)