Class: ActionFramework::Server
- Inherits:
-
Object
- Object
- ActionFramework::Server
- Defined in:
- lib/actionframework.rb
Class Method Summary collapse
Instance Method Summary collapse
- #autoimport ⇒ Object
- #call(env) ⇒ Object
- #get_settings ⇒ Object
- #getModelResponse(req, res) ⇒ Object
-
#initialize ⇒ Server
constructor
A new instance of Server.
- #routes(&block) ⇒ Object
- #settings {|@settings| ... } ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize ⇒ Server
Returns a new instance of Server.
23 24 25 26 27 28 29 |
# File 'lib/actionframework.rb', line 23 def initialize require 'bundler' Bundler.require(:default) @routesklass = ActionFramework::Routes.new @settings = ActionFramework::Settings.new end |
Class Method Details
.current ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/actionframework.rb', line 31 def self.current if($runningserver.nil?) $runningserver = ActionFramework::Server.new $runningserver.autoimport $runningserver else $runningserver end end |
.current=(runningserver) ⇒ Object
41 42 43 |
# File 'lib/actionframework.rb', line 41 def self.current=(runningserver) $runningserver = runningserver end |
Instance Method Details
#autoimport ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/actionframework.rb', line 45 def autoimport Dir.glob("controllers/*.rb").each do |file| require './'+file end Dir.glob("models/*.rb").each do |file| require './'+file end require './config/routes' require './config/settings' require './config/plugables' Dir.glob("initializers/*.rb").each do |file| require './'+file end end |
#call(env) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/actionframework.rb', line 64 def call env req = Rack::Request.new(env) res = Rack::Response.new # redirection redirect = @routesklass.redirect? req if(!redirect.nil?) res.redirect(redirect.to) return res.finish end # auto-api feature (only at path /api/*) reso = getModelResponse(req,res) if(!reso.nil?) return reso end controllerinfo = @routesklass.route(req.path,req.request_method) if(controllerinfo == nil) res.body = [ActionFramework::ErrorHandler.new(env,req,res,{}).send("error_404")] res.status = 404 return res.finish end controller = controllerinfo[0] matcheddata = controllerinfo[1] control = controller.split("#") result = Object.const_get(control[0]).new(env,req,res,matcheddata).send(control[1]) res.write result res.finish end |
#get_settings ⇒ Object
105 106 107 |
# File 'lib/actionframework.rb', line 105 def get_settings @settings end |
#getModelResponse(req, res) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/actionframework.rb', line 113 def getModelResponse req,res if(matcheddata = req.path.match(Regexp.new("/api/(?<modelname>(.*))"))) return nil unless @routesklass.models.include?(matcheddata[:modelname]) res["Content-type"] = "application/json" model = Object.const_get(matcheddata[:modelname].capitalize) model.instance_variable_set("@req",req) case req.request_method when "POST" return ActionFramework::ModelHelper.post model,req,res when "GET" return ActionFramework::ModelHelper.get model,res when "UPDATE" return ActionFramework::ModelHelper.update model,res when "DELETE" else end end nil end |
#routes(&block) ⇒ Object
97 98 99 |
# File 'lib/actionframework.rb', line 97 def routes &block @routesklass.instance_eval &block end |
#settings {|@settings| ... } ⇒ Object
101 102 103 |
# File 'lib/actionframework.rb', line 101 def settings yield @settings end |
#start ⇒ Object
109 110 111 |
# File 'lib/actionframework.rb', line 109 def start Rack::Server.new({:app => self,:server => @settings.server, :Port => @settings.port}).start end |