Class: Buttons::Application
- Inherits:
-
Object
- Object
- Buttons::Application
- Includes:
- Config
- Defined in:
- lib/buttons/application.rb
Instance Method Summary collapse
- #add_route(http_method, path, params) ⇒ Object
- #call(environment) ⇒ Object
-
#initialize(&block) ⇒ Application
constructor
A new instance of Application.
- #render(request, response, params) ⇒ Object
- #start ⇒ Object
- #use(middleware, options = nil) ⇒ Object
Methods included from Config
Constructor Details
#initialize(&block) ⇒ Application
Returns a new instance of Application.
8 9 10 11 |
# File 'lib/buttons/application.rb', line 8 def initialize(&block) @router ||= Buttons::Router.new @middlewares ||= [] end |
Instance Method Details
#add_route(http_method, path, params) ⇒ Object
25 26 27 |
# File 'lib/buttons/application.rb', line 25 def add_route(http_method, path, params) @router.add_route(http_method, path, params) end |
#call(environment) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/buttons/application.rb', line 29 def call(environment) rack_app = lambda { |env| request = ::Rack::Request.new(env) response = ::Rack::Response.new route = @router.recognize(request) if route render(request, response, route) else [404, {"Content-Type" => 'text/plain'}, "not found: #{request.path}\n\n" + "All routes: \n#{@router.print_routes}" ] end } @middlewares.each do |middleware, | rack_app = if middleware.new(rack_app, ) else middleware.new(rack_app) end end rack_app.call environment end |
#render(request, response, params) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/buttons/application.rb', line 55 def render(request, response, params) , method_name = params[:button], params[:method] response['Content-Type'] = 'text/javascript' content = .new(request).call_method(method_name) if conversion = params.delete(:convert) content = content.send(conversion) end response.write(content) response.finish end |
#start ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/buttons/application.rb', line 17 def start unless app_root.nil? Dir[app_root('app', '**', '*.rb')].each do |ruby_file| require ruby_file end end end |
#use(middleware, options = nil) ⇒ Object
13 14 15 |
# File 'lib/buttons/application.rb', line 13 def use(middleware, = nil) @middlewares << [middleware, ] end |