Module: RocketIO

Extended by:
RocketIO
Included in:
RocketIO
Defined in:
lib/rocketio/controller/helpers.rb,
lib/rocketio.rb,
lib/rocketio/util.rb,
lib/rocketio/router.rb,
lib/rocketio/version.rb,
lib/rocketio/controller.rb,
lib/rocketio/application.rb,
lib/rocketio/controller/flash.rb,
lib/rocketio/controller/cookies.rb,
lib/rocketio/controller/cookies.rb,
lib/rocketio/controller/filters.rb,
lib/rocketio/controller/request.rb,
lib/rocketio/controller/response.rb,
lib/rocketio/controller/sessions.rb,
lib/rocketio/controller/websocket.rb,
lib/rocketio/controller/middleware.rb,
lib/rocketio/controller/token_auth.rb,
lib/rocketio/controller/authorization.rb,
lib/rocketio/controller/authentication.rb,
lib/rocketio/controller/error_handlers.rb

Overview

Copyright © 2004-2015 David Heinemeier Hansson Copyright © 2015 Slee Woo

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Defined Under Namespace

Modules: TokenAuth Classes: Application, Controller, Cookies, Flash, Request, Response, Router

Constant Summary collapse

ENVIRONMENTS =
{
  'development' => 'development'.freeze,
  'testing'     => 'testing'.freeze,
  'stage'       => 'stage'.freeze,
  'production'  => 'production'.freeze,
}.freeze
ENVIRONMENT =
ENVIRONMENTS[environment]
REQUEST_METHODS =
{
  (GET    = 'GET'.freeze)    => :get,
  (POST   = 'POST'.freeze)   => :post,
  (PUT    = 'PUT'.freeze)    => :put,
  (DELETE = 'DELETE'.freeze) => :delete,
  (HEAD   = 'HEAD'.freeze)   => :head
}.freeze
INDEX_METHOD =
:index
EMPTY_STRING =
''.freeze
EMPTY_STRING_PROC =
proc {RocketIO::EMPTY_STRING}
EMPTY_ARRAY =
[].freeze
EMPTY_HASH =
{}.freeze
SLASH =
'/'.freeze
QUERY_PREFIX =
'?'.freeze
PATH_SPLITTER =
/[^\/]+/.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
APPLICATION_JSON =
'application/json'.freeze
APPLICATION_JSON_REGEXP =
/application\/json/i
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_CONTENT_TYPE =
'CONTENT_TYPE'.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="%s"'.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
RACK_INPUT =
'rack.input'.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
INHERITABLE_SETUPS =
[
  :before,
  :around,
  :after,
  :basic_auth,
  :digest_auth,
  :token_auth,
  :error_handlers,
  :middleware,
  :sessions,
]
VERSION =
'0.4.4'.freeze

Instance Method Summary collapse

Instance Method Details

#caller_to_dirname(caller) ⇒ Object



68
69
70
# File 'lib/rocketio/util.rb', line 68

def caller_to_dirname caller
  ::File.dirname(caller[0].split(/:\d+:in\s+`/)[0])
end

#controllersObject



133
134
135
# File 'lib/rocketio.rb', line 133

def controllers
  @controllers ||= []
end

#environmentObject



27
28
29
# File 'lib/rocketio.rb', line 27

def environment
  ENVIRONMENT
end

#error_renderer(error_code, without_layout = nil, context = {}) ⇒ Object



62
63
64
65
66
# File 'lib/rocketio/util.rb', line 62

def error_renderer error_code, without_layout = nil, context = {}
  response = RocketIO::ERROR_TEMPLATES[error_code].render(context)
  return response if without_layout
  RocketIO::ERROR_TEMPLATES[:layout].render(context.merge(yield: response))
end

#path_params(parameters) ⇒ Object



56
57
58
59
60
# File 'lib/rocketio/util.rb', line 56

def path_params parameters
  parameters.each_with_index.each_with_object({}) do |(param,i),o|
    o[param[1]] = (i .. (param[0] == :rest ? -parameters[i .. -1].size : i)).freeze
  end.freeze
end

#path_params_expected(parameters) ⇒ Array

determines which of given parameters are required/optional/splat and based on this info deduct the min/max required arguments the method can be called with.

Parameters:

  • parameters (Array)

Returns:

  • (Array)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rocketio/util.rb', line 34

def path_params_expected parameters
  min, max = 0, parameters.size

  unlimited = false
  parameters.each_with_index do |param, i|

    increment = param.first == :req

    if (next_param = parameters.values_at(i+1).first)
      increment = true if next_param[0] == :req
    end

    if param.first == :rest
      increment = false
      unlimited = true
    end
    min += 1 if increment
  end
  max = :* if unlimited
  {min: min, max: max}.freeze
end

#rootify_path(*chunks) ⇒ String

  • ensure leading slash

  • remove trailing slash

  • remove double slashes

given path may consist of multiple chunks passed as arguments given chunks are flattened so the method can accept Array arguments.

Parameters:

  • *chunks (String, Symbol)

Returns:

  • (String)


12
13
14
15
16
# File 'lib/rocketio/util.rb', line 12

def rootify_path *chunks
  RocketIO::SLASH + File.join(*chunks.flatten.map!(&:to_s)).
    gsub(/\/+/, RocketIO::SLASH).
    gsub(/\A\/|\/\z/, RocketIO::EMPTY_STRING)
end

#underscore(x) ⇒ String

convert CamelCase string/symbol into underscored_name

Parameters:

  • x (String, Symbol)

Returns:

  • (String)


22
23
24
25
26
27
# File 'lib/rocketio/util.rb', line 22

def underscore x
  x.to_s.
    gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
    gsub(/([a-z\d])([A-Z])/, '\1_\2').
    downcase
end