Class: Webservice::Base

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/webservice/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#content_type, #error, #headers, #not_found, #redirect_to, #send_file

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



225
226
227
# File 'lib/webservice/base.rb', line 225

def env
  @env
end

#handlerObject (readonly)

default response_handler/respond_with handler magic



227
228
229
# File 'lib/webservice/base.rb', line 227

def handler
  @handler
end

#paramsObject (readonly)

Returns the value of attribute params.



224
225
226
# File 'lib/webservice/base.rb', line 224

def params
  @params
end

#requestObject (readonly)

Returns the value of attribute request.



222
223
224
# File 'lib/webservice/base.rb', line 222

def request
  @request
end

#responseObject (readonly)

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 )    ## note self.call(env) lets you use =>  run Base instead of run Base.new
  ## puts "calling #{self.name}.call"
  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

Returns:

  • (Boolean)


204
# File 'lib/webservice/base.rb', line 204

def development?() environment == :development; end

.environmentObject



198
199
200
201
202
# File 'lib/webservice/base.rb', line 198

def environment
  ## include APP_ENV why? why not?
  ##   todo -- cache value? why why not?  (see/follow sinatara set machinery ??)
  (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}"

  ## note: for now use the sintatra-style patterns (with mustermann)
  fallback_routes[method] << [Mustermann::Sinatra.new(pattern), block]
end

.fallback_routesObject



191
192
193
194
195
# File 'lib/webservice/base.rb', line 191

def fallback_routes
  ## note: !!! use @@ NOT just @ e.g.
  ##   routes get shared/used by all classes/subclasses
  @@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

Returns:

  • (Boolean)


205
# File 'lib/webservice/base.rb', line 205

def production?()  environment == :production;  end

.prototypeObject



139
140
141
142
143
144
# File 'lib/webservice/base.rb', line 139

def prototype
  ## puts "calling #{self.name}.prototype"
  @prototype ||= self.new
  ## pp @prototype
  ## @prototype
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}"

  ## note: for now use the sintatra-style patterns (with mustermann)
  routes[method] << [Mustermann::Sinatra.new(pattern), block]
end

.routesObject



172
173
174
# File 'lib/webservice/base.rb', line 172

def routes
  @routes ||= Hash.new { |hash, key| hash[key]=[] }
end

.run!Object

convenience method



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}"  # note: assumes self is class
  app    = self     ## note: use self; will be derived class (e.g. App and not Base)
  port   = 4567
  Rack::Handler::WEBrick.run( app, Port:port ) do |server|
    ## todo: add traps here - why, why not??
  end
end

.test?Boolean

Returns:

  • (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 )    ## for now "hard-coded"; make it a setting later - why? why not?

  ### move cors headers to responseHandler to initialize!!!! - why? why not??
  ## (auto-)add (merge in) cors headers
  ##   todo: move into a before filter ??  lets you overwrite headers - needed - why? why not??
  headers '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.header.merge!( args.detect{ |arg| arg.is_a?(Hash) } || {} )
  response.body = [args.detect{ |arg| arg.is_a?(String) } || '']
  throw :halt, response
end