Class: Rack::Anobik
- Inherits:
-
Object
- Object
- Rack::Anobik
- Defined in:
- lib/rack/anobik.rb
Overview
MIT-License - Anupom Syam
Constant Summary collapse
- ERR_MESSAGES =
{ :missing_method => "Method %s::#%s not supported", :missing_class => "Class %s not found in file %s.rb", :missing_file => "File %s.rb missing in directory %s", :invalid_path => "URL pattern unknown, Path %s not found", :missing_routes_file => "Routes file %s.rb is not present in directory %s", :missing_routes_class => "Class %s is missing in routes file %s.rb" }
- STATUSES =
{ :ok => 200, :bad_request => 404}
Instance Method Summary collapse
- #anobik_error(err, array = []) ⇒ Object
- #anobik_load(dirname, filename) ⇒ Object
- #anobik_load_config(filename) ⇒ Object
- #anobik_load_resource(filename) ⇒ Object
- #anobik_root ⇒ Object
- #bad_request ⇒ Object
- #call(env) ⇒ Object
-
#initialize(app, options) ⇒ Anobik
constructor
A new instance of Anobik.
- #system_error(msg) ⇒ Object
- #to_class_name(filename) ⇒ Object
Constructor Details
#initialize(app, options) ⇒ Anobik
Returns a new instance of Anobik.
70 71 72 73 74 75 76 |
# File 'lib/rack/anobik.rb', line 70 def initialize app, @app = app @url = [:url] || '' @url = '' if @url == '/' @production = [:production] $LOAD_PATH << ANOBIK_ROOT end |
Instance Method Details
#anobik_error(err, array = []) ⇒ Object
155 156 157 158 |
# File 'lib/rack/anobik.rb', line 155 def anobik_error(err, array = []) raise ERR_MESSAGES[err] % array unless @production return bad_request end |
#anobik_load(dirname, filename) ⇒ Object
177 178 179 180 181 182 183 184 185 |
# File 'lib/rack/anobik.rb', line 177 def anobik_load dirname, filename if (@production) require dirname + filename else classname = to_class_name(filename).to_sym Object.class_eval { remove_const classname } if Object.const_defined? classname Kernel.load dirname + filename + '.rb' end end |
#anobik_load_config(filename) ⇒ Object
169 170 171 |
# File 'lib/rack/anobik.rb', line 169 def anobik_load_config filename anobik_load ::Anobik::CONFIG_DIR, filename end |
#anobik_load_resource(filename) ⇒ Object
173 174 175 |
# File 'lib/rack/anobik.rb', line 173 def anobik_load_resource filename anobik_load ::Anobik::RESOURCE_DIR, filename end |
#anobik_root ⇒ Object
187 188 189 |
# File 'lib/rack/anobik.rb', line 187 def anobik_root ANOBIK_ROOT end |
#bad_request ⇒ Object
165 166 167 |
# File 'lib/rack/anobik.rb', line 165 def bad_request [STATUSES[:bad_request], @headers, ''] end |
#call(env) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/rack/anobik.rb', line 78 def call env req = Rack::Request.new(env) path = req.path_info method = req.request_method.downcase @headers = { 'Content-Type' => 'text/html' } #guard statements: unless path.index(@url) == 0 return @app.call(env) end begin anobik_load_config ::Anobik::ROUTES_FILE rescue LoadError => msg return system_error msg end routes_classname = to_class_name ::Anobik::ROUTES_FILE begin raise NameError unless Object.const_get(routes_classname).kind_of? Class rescue Exception return anobik_error(:missing_routes_class, [routes_classname, ANOBIK_ROOT + ::Anobik::CONFIG_DIR + ::Anobik::ROUTES_FILE]) end begin urls = Object.const_get(routes_classname)::URLS raise unless urls.kind_of? Hash rescue Exception urls = { "/" => "index" } end resource_filename = nil matches = nil urls.each do |regex, filename| matches = path.match(Regexp.new('^' << @url << regex << '/?$', true)) unless matches.nil? resource_filename = filename break end end if resource_filename.nil? return anobik_error(:invalid_path, [path]) end begin anobik_load_resource resource_filename rescue LoadError => msg return system_error msg end resource_classname = to_class_name resource_filename begin raise NameError unless Object.const_get(resource_classname).kind_of? Class rescue Exception return anobik_error(:missing_class, [resource_classname, ANOBIK_ROOT + ::Anobik::RESOURCE_DIR + resource_filename]) end resource = Object.const_get(resource_classname).new(){env} unless resource.respond_to?(method) return anobik_error(:missing_method, [resource_classname, method]) end body = eval 'resource.' << method << '(' << matches.captures.join(' , ') << ')' [STATUSES[:ok], @headers, body] end |
#system_error(msg) ⇒ Object
160 161 162 163 |
# File 'lib/rack/anobik.rb', line 160 def system_error msg raise msg unless @production return bad_request end |
#to_class_name(filename) ⇒ Object
150 151 152 153 |
# File 'lib/rack/anobik.rb', line 150 def to_class_name filename #shamelessly copied from Rails filename.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase } end |