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

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

Instance Method Summary collapse

Instance Method Details

#configure!Object



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
146
147
148
# File 'lib/usher/interface/sinatra.rb', line 101

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



92
93
94
# File 'lib/usher/interface/sinatra.rb', line 92

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

#new(*args, &bk) ⇒ Object



54
55
56
57
# File 'lib/usher/interface/sinatra.rb', line 54

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

#reset!Object



96
97
98
99
# File 'lib/usher/interface/sinatra.rb', line 96

def reset!
  router.reset!
  super
end

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



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

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



82
83
84
85
86
87
88
89
90
# File 'lib/usher/interface/sinatra.rb', line 82

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\$_\+!\*\',]+',
                        :detailed_failure => true)
  block_given? ? yield(@router) : @router
end