Class: Webservice::Base

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

Constant Summary collapse

HTTP_VERBS =
%w(GET POST PATCH PUT DELETE HEAD OPTIONS)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



46
47
48
# File 'lib/webservice/base.rb', line 46

def env
  @env
end

#paramsObject (readonly)

Returns the value of attribute params.



45
46
47
# File 'lib/webservice/base.rb', line 45

def params
  @params
end

#requestObject (readonly)

Returns the value of attribute request.



43
44
45
# File 'lib/webservice/base.rb', line 43

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



44
45
46
# File 'lib/webservice/base.rb', line 44

def response
  @response
end

Class Method Details

.methodObject

todo/check: all verbs needed! (supported) - why, why not??



14
15
16
17
18
19
20
21
# File 'lib/webservice/base.rb', line 14

HTTP_VERBS.each do |method|
  define_method( method.downcase ) do |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
end

.routesObject



24
25
26
# File 'lib/webservice/base.rb', line 24

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

.run!Object

convenience method



30
31
32
33
34
35
36
37
# File 'lib/webservice/base.rb', line 30

def run!
  puts "[debug] Webservice::Base.run! - self = #<#{self.name}:#{self.object_id}> : #{self.class.name}"  # note: assumes self is class
  app    = self.new   ## note: use self; will be derived class (e.g. App and not Base)
  port   = 4567
  Rack::Handler.get('webrick').run( app, Port:port ) do |server|
    ## todo: add traps here - why, why not??
  end
end

Instance Method Details

#call(env) ⇒ Object



48
49
50
# File 'lib/webservice/base.rb', line 48

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

#call!(env) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/webservice/base.rb', line 52

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
  route_eval
  @response.finish
end

#halt(*args) ⇒ Object



63
64
65
66
67
68
# File 'lib/webservice/base.rb', line 63

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