Module: Usher::Interface::Sinatra::Extension::ClassMethods

Defined in:
lib/usher/interface/sinatra.rb

Instance Method Summary collapse

Instance Method Details

#configure!Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/usher/interface/sinatra.rb', line 92

def configure!
  configure :development do
    error 404 do
      content_type 'text/html'

      (<<-HTML).gsub(/^ {17}/, '')
      <!DOCTYPE html>
      <html>
      <head>
        <style type="text/css">
        body { text-align:center;font-family:helvetica,arial;font-size:22px;
          color:#888;margin:20px}
        #c {margin:0 auto;width:500px;text-align:left}
        </style>
      </head>
      <body>
        <h2>Sinatra doesn't know this ditty.</h2>
        <div id="c">
          Try this:
          <pre>#{request.request_method.downcase} '#{request.path_info}' do\n  "Hello World"\nend</pre>
        </div>
      </body>
      </html>
      HTML
    end
  end

  @_configured = true
end

#generate(name, *params) ⇒ Object



83
84
85
# File 'lib/usher/interface/sinatra.rb', line 83

def generate(name, *params)
  router.generator.generate(name, *params)
end

#new(*args, &bk) ⇒ Object



46
47
48
49
# File 'lib/usher/interface/sinatra.rb', line 46

def new(*args, &bk)
  configure! unless @_configured
  super(*args, &bk)
end

#reset!Object



87
88
89
90
# File 'lib/usher/interface/sinatra.rb', line 87

def reset!
  router.reset!
  super
end

#route(verb, path, options = {}, &block) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/usher/interface/sinatra.rb', line 51

def route(verb, path, options={}, &block)
  path.gsub!(/\/\?$/, "(/)")
  name = options.delete(:name)
  options[:conditions] ||= {}
  options[:conditions][:request_method] = verb
  options[:conditions][:host] = options.delete(:host) if options.key?(:host)

  define_method "#{verb} #{path}", &block
  unbound_method = instance_method("#{verb} #{path}")
  block =
    if block.arity != 0
      lambda { unbound_method.bind(self).call(*@block_params) }
    else
      lambda { unbound_method.bind(self).call }
    end

  invoke_hook(:route_added, verb, path, block)

  route = router.add_route(path, options).to(block)
  route.name(name) if name
  route
end

#routerObject



74
75
76
77
78
79
80
81
# File 'lib/usher/interface/sinatra.rb', line 74

def router
  @router ||= Usher.new(:request_methods => [:request_method, :host, :port, :scheme],
                        :ignore_trailing_delimiters => true,
                        :generator => Usher::Util::Generators::URL.new,
                        :delimiters => ['/', '.', '-'],
                        :valid_regex => '[0-9A-Za-z\$_\+!\*\',]+')
  block_given? ? yield(@router) : @router
end