Class: ERewriter

Inherits:
Object
  • Object
show all
Includes:
EConstants, EUtils, Rack::Utils
Defined in:
lib/e-core/rewriter.rb

Constant Summary

Constants included from EConstants

EConstants::CONTENT_TYPE__DEFAULT, EConstants::CONTENT_TYPE__EVENT_STREAM, EConstants::ENV__AUTHORIZATION_KEYS, EConstants::ENV__ESPRESSO_ACTION, EConstants::ENV__ESPRESSO_FORMAT, EConstants::ENV__ESPRESSO_PATH_INFO, EConstants::ENV__HTTP_ACCEPT, EConstants::ENV__HTTP_HOST, EConstants::ENV__HTTP_IF_MODIFIED_SINCE, EConstants::ENV__HTTP_IF_NONE_MATCH, EConstants::ENV__HTTP_IF_UNMODIFIED_SINCE, EConstants::ENV__HTTP_X_FORWARDED_HOST, EConstants::ENV__HTTP_X_REQUESTED_WITH, EConstants::ENV__PATH_INFO, EConstants::ENV__QUERY_STRING, EConstants::ENV__RACK_ENV, EConstants::ENV__REMOTE_USER, EConstants::ENV__REQUEST_METHOD, EConstants::ENV__REQUEST_URI, EConstants::ENV__SCRIPT_NAME, EConstants::ENV__SERVER_NAME, EConstants::ENV__SERVER_PORT, EConstants::ENV__XML_HTTP_REQUEST, EConstants::HEADER__AUTHENTICATE, EConstants::HEADER__CACHE_CONTROL, EConstants::HEADER__CONTENT_DISPOSITION, EConstants::HEADER__CONTENT_TYPE, EConstants::HEADER__EXPIRES, EConstants::HEADER__LAST_MODIFIED, EConstants::HEADER__LOCATION, EConstants::HEADER__TRANSFER_ENCODING, EConstants::HTTP__DEFAULT_PORT, EConstants::HTTP__DEFAULT_REQUEST_METHOD, EConstants::HTTP__DEFAULT_SERVER, EConstants::HTTP__REQUEST_METHODS, EConstants::INDEX_ACTION, EConstants::PATH_MODIFIERS, EConstants::PATH_RULES, EConstants::RACK__WEBSOCKET, EConstants::STATUS__BAD_REQUEST, EConstants::STATUS__NOT_FOUND, EConstants::STATUS__NOT_IMPLEMENTED, EConstants::STATUS__OK, EConstants::STATUS__PERMANENT_REDIRECT, EConstants::STATUS__PROTECTED, EConstants::STATUS__REDIRECT, EConstants::STATUS__SERVER_ERROR, EConstants::VIEW__DEFAULT_ENGINE, EConstants::VIEW__DEFAULT_ENGINE_NAME, EConstants::VIEW__DEFAULT_PATH, EConstants::VIEW__ENGINE_BY_SYM, EConstants::VIEW__ENGINE_MAPPER, EConstants::VIEW__EXTRA_ENGINES, EConstants::VIEW__EXT_BY_ENGINE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EUtils

action_to_route, build_path, canonical_to_route, class_to_route, deRESTify_action, encode_token_auth_credentials, extract_hosts, indifferent_hash, indifferent_params, is_app?, method_arity, normalize_path, register_extra_engines!, rootify_url, route_to_regexp, underscore

Constructor Details

#initialize(*matches, &proc) ⇒ ERewriter

Returns a new instance of ERewriter.



8
9
10
# File 'lib/e-core/rewriter.rb', line 8

def initialize *matches, &proc
  @matches, @proc = matches, proc
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



6
7
8
# File 'lib/e-core/rewriter.rb', line 6

def env
  @env
end

#requestObject (readonly)

Returns the value of attribute request.



6
7
8
# File 'lib/e-core/rewriter.rb', line 6

def request
  @request
end

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/e-core/rewriter.rb', line 12

def call env
  @env, @request = env, ERequest.new(env)
  @status, @headers, @body =
    STATUS__BAD_REQUEST, {"Content-Type" => "text/plain"}, []

  catch :__e__rewriter__halt_symbol__ do
    self.instance_exec *@matches, &@proc
  end
  [@status, @headers, @body]
end

#halt(*args) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/e-core/rewriter.rb', line 55

def halt *args
  args.each do |a|
    case a
      when Array
        @status, @headers, @body = a
      when Fixnum
        @status = a
      when Hash
        @headers.update a
      else
        @body = [a]
    end
  end
  @status ||= STATUS__OK
  throw :__e__rewriter__halt_symbol__
end

#pass(*args) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/e-core/rewriter.rb', line 33

def pass *args
  ctrl = (args.size > 0 && is_app?(args.first) && args.shift) ||
    raise(ArgumentError, "Controller missing. Please provide it as first argument when calling `pass' inside a rewrite rule block.")

  action = args.shift
  route = ctrl[action] ||
    raise(ArgumentError, '%s controller does not respond to %s action' % [ctrl, action.inspect])

  env[ENV__SCRIPT_NAME] = route
  env[ENV__REQUEST_URI] = env[ENV__PATH_INFO] = ''
  env[ENV__ESPRESSO_PATH_INFO] = nil

  if args.size > 0
    path, params = '/', {}
    args.each { |a| a.is_a?(Hash) ? params.update(a) : path << a.to_s << '/' }
    env[ENV__PATH_INFO] = env[ENV__REQUEST_URI] = path
    params.size > 0 &&
      env.update(ENV__QUERY_STRING => build_nested_query(params))
  end
  @status, @headers, @body = ctrl.new(action).call(env)
end

#permanent_redirect(location) ⇒ Object



28
29
30
31
# File 'lib/e-core/rewriter.rb', line 28

def permanent_redirect location
  redirect location
  @status = STATUS__PERMANENT_REDIRECT
end

#redirect(location) ⇒ Object



23
24
25
26
# File 'lib/e-core/rewriter.rb', line 23

def redirect location
  @status = STATUS__REDIRECT
  @headers[HEADER__LOCATION] = location
end