Module: HttpRouter::Sinatra::Extension::ClassMethods

Defined in:
lib/http_router_sinatra.rb

Instance Method Summary collapse

Instance Method Details

#configure!Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/http_router_sinatra.rb', line 130

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
    error 405 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 sorta knows this ditty, but the request method is not allowed.</h2>
      </body>
      </html>
      HTML
    end
  end

  @_configured = true
end

#delete(path, *args, &bk) ⇒ Object



69
# File 'lib/http_router_sinatra.rb', line 69

def delete(path, *args, &bk); route 'DELETE', path, *args, &bk end

#generate(name, *params) ⇒ Object



121
122
123
# File 'lib/http_router_sinatra.rb', line 121

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

#get(path, *args, &block) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/http_router_sinatra.rb', line 59

def get(path, *args, &block)
  conditions = @conditions.dup
  route('GET', path, *args, &block)

  @conditions = conditions
  route('HEAD', path, *args, &block)
end

#head(path, *args, &bk) ⇒ Object



70
# File 'lib/http_router_sinatra.rb', line 70

def head(path, *args, &bk);   route 'HEAD',   path, *args, &bk end

#new(*args, &bk) ⇒ Object



54
55
56
57
# File 'lib/http_router_sinatra.rb', line 54

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

#post(path, *args, &bk) ⇒ Object



68
# File 'lib/http_router_sinatra.rb', line 68

def post(path, *args, &bk);   route 'POST',   path, *args, &bk end

#put(path, *args, &bk) ⇒ Object



67
# File 'lib/http_router_sinatra.rb', line 67

def put(path, *args, &bk);    route 'PUT',    path, *args, &bk end

#reset!Object



125
126
127
128
# File 'lib/http_router_sinatra.rb', line 125

def reset!
  router.reset!
  super
end

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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/http_router_sinatra.rb', line 72

def route(verb, path, options={}, &block)
  name = options.delete(:name)

  path = transform_path(path)

  define_method "#{verb} #{path}", &block
  unbound_method = instance_method("#{verb} #{path}")
  block = block.arity.zero? ?
    proc { unbound_method.bind(self).call } :
    proc { unbound_method.bind(self).call(*@block_params) }

  invoke_hook(:route_added, verb, path, block)

  route = router.add(path)

  route.matching(options[:matching]) if options.key?(:matching)

  route.send(verb.downcase.to_sym)
  route.host(options[:host]) if options.key?(:host)
  
  route.name(name) if name

  route.arbitrary_with_continue do |req, params|
    if req.testing_405?
      req.continue[true]
    else
      req.rack_request.env['sinatra.instance'].instance_eval do
        handled = false
        @block_params = req.params
        (@params ||= {}).merge!(params)
        pass_block = catch(:pass) do
          route_eval(&block)
          handled = true
        end
        req.continue[handled]
      end
    end
  end

  route.to(block)
  route
end

#router {|@router| ... } ⇒ Object

Yields:



115
116
117
118
119
# File 'lib/http_router_sinatra.rb', line 115

def router
  @router ||= HttpRouter.new
  yield(@router) if block_given?
  @router
end