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

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

Instance Method Summary collapse

Instance Method Details

#configure!Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/http_router/interface/sinatra.rb', line 98

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

#generate(name, *params) ⇒ Object



89
90
91
# File 'lib/http_router/interface/sinatra.rb', line 89

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

#new(*args, &bk) ⇒ Object



56
57
58
59
# File 'lib/http_router/interface/sinatra.rb', line 56

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

#reset!Object



93
94
95
96
# File 'lib/http_router/interface/sinatra.rb', line 93

def reset!
  router.reset!
  super
end

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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/http_router/interface/sinatra.rb', line 61

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

  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.request_method(verb)
  route.host(options[:host]) if options.key?(:host)
  
  route.name(name) if name
  route.to(block)
  route
end

#routerObject



84
85
86
87
# File 'lib/http_router/interface/sinatra.rb', line 84

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