Module: Sinatra::Resilient::Route

Defined in:
lib/sinatra/resilient/route.rb,
lib/sinatra/resilient/route/version.rb

Defined Under Namespace

Modules: Helpers Classes: Error

Constant Summary collapse

Sinatra1StyleRenderer =
::Mustermann::AST::Translator.create do
  translate(:named_splat)      { "{+#{name}}"                           }
  translate(:splat)            { "*"                                    }
  translate(:char, :separator) { ::Mustermann::Sinatra.escape(payload)  }
  translate(:root)             { t(payload)                             }
  translate(:group)            { "(#{t(payload)})"                      }
  translate(:union)            { "(#{t(payload, join: "|")})" }
  translate(:optional)         { "#{t(payload)}?"                       }
  translate(Array)             { |join: ""| map { |e| t(e) }.join(join) }

  translate(:capture) do
    raise Mustermann::Error, "cannot render variables"      if node.is_a? :variable
    raise Mustermann::Error, "cannot translate constraints" if constraint || qualifier || convert

    ":#{name}"
  end
end
VERSION =
"0.3.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/sinatra/resilient/route.rb', line 51

def self.registered(app)
  app.helpers Helpers

  app.before do
    ensure_sinatra_route
  end

  app.after do
    ensure_sinatra_route
  end
end

Instance Method Details

#route(verb, path) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/sinatra/resilient/route.rb', line 63

def route(verb, path, *)
  path = Mustermann.new(path) if path.is_a?(String)
  munged_path = Sinatra1StyleRenderer.translate(path.to_ast).to_s

  condition do
    route_signature = "#{env["SCRIPT_NAME"]}#{munged_path}"
    env["sinatra.resilient.route_method"] = verb
    env["sinatra.resilient.route_signature"] = route_signature
    env["sinatra.resilient.route"] = "#{verb} #{route_signature}"
    true
  end

  super
end