Class: Webservice::Metal

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

Overview

bare bones core (use base for more built-in functionality)

Direct Known Subclasses

Base

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#content_type, #error, #headers, #not_found, #redirect, #send_file

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



205
206
207
# File 'lib/webservice/metal.rb', line 205

def env
  @env
end

#paramsObject (readonly)

Returns the value of attribute params.



204
205
206
# File 'lib/webservice/metal.rb', line 204

def params
  @params
end

#requestObject (readonly)

Returns the value of attribute request.



202
203
204
# File 'lib/webservice/metal.rb', line 202

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



203
204
205
# File 'lib/webservice/metal.rb', line 203

def response
  @response
end

Class Method Details

.call(env) ⇒ Object

note self.call(env) lets you use => run Base instead of run Base.new



139
140
141
142
# File 'lib/webservice/metal.rb', line 139

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



165
# File 'lib/webservice/metal.rb', line 165

def delete( pattern, &block)  route( DELETE,  pattern, &block ); end

.development?Boolean

Returns:

  • (Boolean)


186
# File 'lib/webservice/metal.rb', line 186

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

.environmentObject



180
181
182
183
184
# File 'lib/webservice/metal.rb', line 180

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

.get(pattern, &block) ⇒ Object

Note: for now defining a ‘GET` handler also automatically defines a `HEAD` handler (follows sinatra convention)



157
158
159
160
# File 'lib/webservice/metal.rb', line 157

def get( pattern, &block )
  route( GET,   pattern, &block )
  route( HEAD,  pattern, &block )
end

.head(pattern, &block) ⇒ Object



166
# File 'lib/webservice/metal.rb', line 166

def head( pattern, &block)    route( HEAD,    pattern, &block ); end

.options(pattern, &block) ⇒ Object



167
# File 'lib/webservice/metal.rb', line 167

def options( pattern, &block) route( OPTIONS, pattern, &block ); end

.patch(pattern, &block) ⇒ Object



163
# File 'lib/webservice/metal.rb', line 163

def patch( pattern, &block)   route( PATCH,   pattern, &block ); end

.post(pattern, &block) ⇒ Object



162
# File 'lib/webservice/metal.rb', line 162

def post( pattern, &block)    route( POST,    pattern, &block ); end

.production?Boolean

Returns:

  • (Boolean)


187
# File 'lib/webservice/metal.rb', line 187

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

.prototypeObject



144
145
146
147
148
149
# File 'lib/webservice/metal.rb', line 144

def prototype
  ## puts "calling #{self.name}.prototype"
  @prototype ||= self.new
  ## pp @prototype
  ## @prototype
end

.put(pattern, &block) ⇒ Object



164
# File 'lib/webservice/metal.rb', line 164

def put( pattern, &block)     route( PUT,     pattern, &block ); end

.route(method, pattern, &block) ⇒ Object



169
170
171
172
173
174
# File 'lib/webservice/metal.rb', line 169

def route( method, pattern, &block )
  puts "[debug] Webservice::Metal.#{method.downcase} - add route #{method} '#{pattern}' to #<#{self.name}:#{self.object_id}> : #{self.class.name}"

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

.routesObject



176
177
178
# File 'lib/webservice/metal.rb', line 176

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

.run!Object

convenience method



191
192
193
194
195
196
197
198
# File 'lib/webservice/metal.rb', line 191

def run!
  puts "[debug] Webservice::Metal.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)


188
# File 'lib/webservice/metal.rb', line 188

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

Instance Method Details

#call(env) ⇒ Object



208
209
210
# File 'lib/webservice/metal.rb', line 208

def call( env )
  dup.call!( env )
end

#call!(env) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/webservice/metal.rb', line 212

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

  catch(:halt) do
    ## call before if defined in derived (sub)classes
    before  if respond_to? :before

    route!
  end

  @response.finish
end

#halt(*args) ⇒ Object



231
232
233
234
235
236
# File 'lib/webservice/metal.rb', line 231

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           ## todo/check response arg used - what for??
end