Class: Webservice::Base

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

Instance Attribute Summary

Attributes inherited from Metal

#env, #params, #request, #response

Instance Method Summary collapse

Methods inherited from Metal

call, #call, #call!, delete, development?, environment, get, #halt, head, options, patch, post, production?, prototype, put, route, routes, run!, test?

Methods included from Helpers

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

Instance Method Details

#beforeObject

note: before (filter) for now is just a method (NOT a chain for blocks, etc.);

override method to change before (filter)


11
12
13
14
15
16
17
18
# File 'lib/webservice/base/base.rb', line 11

def before
   ### 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'
end

#dump_routesObject

fallback helpers



81
82
83
84
85
# File 'lib/webservice/base/base.rb', line 81

def dump_routes    ## todo/check - rename to build_routes/show_routes/etc. - why? why not?
  buf = ""
  walk_routes_for( buf, self.class )
  buf
end

#dump_versionObject



105
106
107
108
109
110
111
112
113
# File 'lib/webservice/base/base.rb', line 105

def dump_version
  ## single line version string
  buf = "  "   # note: start with two leading spaces (indent)
  buf << "webservice/#{VERSION} "
  buf << "(#{self.class.environment}), "
  buf << "rack/#{Rack::RELEASE} (#{Rack::VERSION.join('.')}) - "
  buf << "ruby/#{RUBY_VERSION} (#{RUBY_RELEASE_DATE}/#{RUBY_PLATFORM})"
  buf
end

#handle_response(obj, opts = {}) ⇒ Object

note: for now use “plugable” response handler

rename to respond_with or something? why? why not??
 make it a "stateless" function e.g. just retrun tripled [status, headers, body] - why? why not??


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

def handle_response( obj, opts={} )
  handler   = ResponseHandler.new( self )    ## for now "hard-coded"; make it a setting later - why? why not?
  handler.handle_response( obj )   ## prepare response
end

#walk_routes_for(buf, base = self.class) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/webservice/base/base.rb', line 87

def walk_routes_for( buf, base=self.class )

  buf << "  Routes >#{base.name}<:\n\n"

  base.routes.each do |method,routes|
    buf << "    #{method}:\n"
    routes.each do |pattern,block|
      buf << "      #{pattern.to_s}\n"
    end
  end

  if base.superclass.respond_to? :routes
    buf << "\n\n"
    walk_routes_for( buf, base.superclass )
  end
end