Class: Webservice::Base
- Inherits:
-
Object
show all
- Includes:
- Helpers
- Defined in:
- lib/webservice/base.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
-
.call(env) ⇒ Object
note self.call(env) lets you use => run Base instead of run Base.new.
-
.delete(pattern, &block) ⇒ Object
-
.development? ⇒ Boolean
-
.environment ⇒ Object
-
.fallback_route(method, pattern, &block) ⇒ Object
support for “builtin” fallback routes.
-
.fallback_routes ⇒ Object
-
.get(pattern, &block) ⇒ Object
Note: for now defining a ‘GET` handler also automatically defines a `HEAD` handler (follows sinatra convention).
-
.head(pattern, &block) ⇒ Object
-
.options(pattern, &block) ⇒ Object
-
.patch(pattern, &block) ⇒ Object
-
.post(pattern, &block) ⇒ Object
-
.production? ⇒ Boolean
-
.prototype ⇒ Object
-
.put(pattern, &block) ⇒ Object
-
.route(method, pattern, &block) ⇒ Object
-
.routes ⇒ Object
-
.run! ⇒ Object
-
.test? ⇒ Boolean
Instance Method Summary
collapse
Methods included from Helpers
#content_type, #error, #headers, #not_found, #redirect_to, #send_file
Instance Attribute Details
#env ⇒ Object
Returns the value of attribute env.
225
226
227
|
# File 'lib/webservice/base.rb', line 225
def env
@env
end
|
#handler ⇒ Object
default response_handler/respond_with handler magic
227
228
229
|
# File 'lib/webservice/base.rb', line 227
def handler
@handler
end
|
#params ⇒ Object
Returns the value of attribute params.
224
225
226
|
# File 'lib/webservice/base.rb', line 224
def params
@params
end
|
#request ⇒ Object
Returns the value of attribute request.
222
223
224
|
# File 'lib/webservice/base.rb', line 222
def request
@request
end
|
#response ⇒ Object
Returns the value of attribute response.
223
224
225
|
# File 'lib/webservice/base.rb', line 223
def response
@response
end
|
Class Method Details
.call(env) ⇒ Object
note self.call(env) lets you use => run Base instead of run Base.new
134
135
136
137
|
# File 'lib/webservice/base.rb', line 134
def call( env ) prototype.call( env )
end
|
.delete(pattern, &block) ⇒ Object
160
|
# File 'lib/webservice/base.rb', line 160
def delete( pattern, &block) route( DELETE, pattern, &block ); end
|
.development? ⇒ Boolean
204
|
# File 'lib/webservice/base.rb', line 204
def development?() environment == :development; end
|
.environment ⇒ Object
198
199
200
201
202
|
# File 'lib/webservice/base.rb', line 198
def environment
(ENV['APP_ENV'] || ENV['RACK_ENV'] || :development).to_sym
end
|
.fallback_route(method, pattern, &block) ⇒ Object
support for “builtin” fallback routes
e.g. use like
fallback_route GET, '/' do
"Hello, World!"
end
184
185
186
187
188
189
|
# File 'lib/webservice/base.rb', line 184
def fallback_route( method, pattern, &block )
puts "[debug] Webservice::Base.#{method.downcase} - add (fallback) route #{method} '#{pattern}' to #<#{self.name}:#{self.object_id}> : #{self.class.name}"
fallback_routes[method] << [Mustermann::Sinatra.new(pattern), block]
end
|
.fallback_routes ⇒ Object
191
192
193
194
195
|
# File 'lib/webservice/base.rb', line 191
def fallback_routes
@@fallback_routes ||= Hash.new { |hash, key| hash[key]=[] }
end
|
.get(pattern, &block) ⇒ Object
Note: for now defining a ‘GET` handler also automatically defines a `HEAD` handler (follows sinatra convention)
152
153
154
155
|
# File 'lib/webservice/base.rb', line 152
def get( pattern, &block )
route( GET, pattern, &block )
route( HEAD, pattern, &block )
end
|
.head(pattern, &block) ⇒ Object
161
|
# File 'lib/webservice/base.rb', line 161
def head( pattern, &block) route( HEAD, pattern, &block ); end
|
.options(pattern, &block) ⇒ Object
162
|
# File 'lib/webservice/base.rb', line 162
def options( pattern, &block) route( OPTIONS, pattern, &block ); end
|
.patch(pattern, &block) ⇒ Object
158
|
# File 'lib/webservice/base.rb', line 158
def patch( pattern, &block) route( PATCH, pattern, &block ); end
|
.post(pattern, &block) ⇒ Object
157
|
# File 'lib/webservice/base.rb', line 157
def post( pattern, &block) route( POST, pattern, &block ); end
|
.production? ⇒ Boolean
205
|
# File 'lib/webservice/base.rb', line 205
def production?() environment == :production; end
|
.prototype ⇒ Object
139
140
141
142
143
144
|
# File 'lib/webservice/base.rb', line 139
def prototype
@prototype ||= self.new
end
|
.put(pattern, &block) ⇒ Object
159
|
# File 'lib/webservice/base.rb', line 159
def put( pattern, &block) route( PUT, pattern, &block ); end
|
.route(method, pattern, &block) ⇒ Object
164
165
166
167
168
169
|
# File 'lib/webservice/base.rb', line 164
def route( method, pattern, &block )
puts "[debug] Webservice::Base.#{method.downcase} - add route #{method} '#{pattern}' to #<#{self.name}:#{self.object_id}> : #{self.class.name}"
routes[method] << [Mustermann::Sinatra.new(pattern), block]
end
|
.routes ⇒ Object
172
173
174
|
# File 'lib/webservice/base.rb', line 172
def routes
@routes ||= Hash.new { |hash, key| hash[key]=[] }
end
|
.run! ⇒ Object
210
211
212
213
214
215
216
217
|
# File 'lib/webservice/base.rb', line 210
def run!
puts "[debug] Webservice::Base.run! - self = #<#{self.name}:#{self.object_id}> : #{self.class.name}" app = self port = 4567
Rack::Handler::WEBrick.run( app, Port:port ) do |server|
end
end
|
.test? ⇒ Boolean
206
|
# File 'lib/webservice/base.rb', line 206
def test?() environment == :test; end
|
Instance Method Details
#call(env) ⇒ Object
230
231
232
|
# File 'lib/webservice/base.rb', line 230
def call( env )
dup.call!( env )
end
|
#call!(env) ⇒ Object
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
# File 'lib/webservice/base.rb', line 234
def call!( env )
env['PATH_INFO'] = '/' if env['PATH_INFO'].empty?
@request = Rack::Request.new( env )
@response = Rack::Response.new
@params = request.params
@env = env
@handler = ResponseHandler.new( self )
'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Headers' => 'Authorization,Accepts,Content-Type,X-CSRF-Token,X-Requested-With',
'Access-Control-Allow-Methods' => 'GET,POST,PUT,DELETE,OPTIONS'
route_eval
@response.finish
end
|
#halt(*args) ⇒ Object
257
258
259
260
261
262
|
# File 'lib/webservice/base.rb', line 257
def halt( *args )
response.status = args.detect{ |arg| arg.is_a?(Fixnum) } || 200
response..merge!( args.detect{ |arg| arg.is_a?(Hash) } || {} )
response.body = [args.detect{ |arg| arg.is_a?(String) } || '']
throw :halt, response
end
|