Class: Wedge::Middleware::Responder
Instance Attribute Summary collapse
-
#app ⇒ Object
Returns the value of attribute app.
-
#env ⇒ Object
Returns the value of attribute env.
-
#extension ⇒ Object
Returns the value of attribute extension.
-
#wedge_path ⇒ Object
Returns the value of attribute wedge_path.
Instance Method Summary collapse
-
#initialize(app, env) ⇒ Responder
constructor
A new instance of Responder.
- #respond ⇒ Object
- #wedge(*args, &block) ⇒ Object
Constructor Details
#initialize(app, env) ⇒ Responder
Returns a new instance of Responder.
20 21 22 |
# File 'lib/wedge/middleware.rb', line 20 def initialize(app, env) @app = app; @env = env end |
Instance Attribute Details
#app ⇒ Object
Returns the value of attribute app.
18 19 20 |
# File 'lib/wedge/middleware.rb', line 18 def app @app end |
#env ⇒ Object
Returns the value of attribute env.
18 19 20 |
# File 'lib/wedge/middleware.rb', line 18 def env @env end |
#extension ⇒ Object
Returns the value of attribute extension.
18 19 20 |
# File 'lib/wedge/middleware.rb', line 18 def extension @extension end |
#wedge_path ⇒ Object
Returns the value of attribute wedge_path.
18 19 20 |
# File 'lib/wedge/middleware.rb', line 18 def wedge_path @wedge_path end |
Instance Method Details
#respond ⇒ Object
24 25 26 27 28 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 54 55 56 57 58 59 60 61 62 63 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 |
# File 'lib/wedge/middleware.rb', line 24 def respond if path =~ Wedge.assets_url_regex @wedge_path, @extension = $1, $2 body, headers, status = [], {}, 200 case extension when 'map' body << ::Wedge.source_map(wedge_path) when 'rb' if wedge_path =~ /^wedge/ path = ::Wedge.config.path.gsub(/\/wedge.rb$/, '') body << File.read("#{path}/#{wedge_path}.rb") else body << File.read("#{ROOT_PATH}/#{wedge_path}.rb") end if Wedge.config.debug when 'call' body_data = request.body.read data = request.params begin # try json data.merge!(body_data ? JSON.parse(body_data) : {}) rescue begin # try form data data.merge!(body_data ? Rack::Utils.parse_query(body_data) : {}) rescue # no data end end data = data.indifferent name = data.delete(:__wedge_name__) method_called = data.delete(:__wedge_method__) method_args = data.delete(:__wedge_args__) if method_args == '__wedge_data__' && data method_args = [data] res = Wedge.scope!(self)[name].send(method_called, *method_args) || '' else # This used to send things like init, we need a better way to # send client config data to the server # res = scope.wedge(name, data).send(method_called, *method_args) || '' res = Wedge.scope!(self)[name].send(method_called, *method_args) || '' end # headers["WEDGE-CSRF-TOKEN"] = scope.csrf_token if scope.methods.include? :csrf_token if res.is_a? Hash headers["Content-Type"] = 'application/json; charset=UTF-8' body << res.to_json else body << res.to_s end else headers['Content-Type'] = 'application/javascript; charset=UTF-8' if Wedge.config.debug body << "#{Wedge.javascript(wedge_path)}\n//# sourceMappingURL=#{Wedge.assets_url}/#{wedge_path}.map" else body << Wedge.javascript(wedge_path) end end [status, headers, body.join] else response.finish end end |