Class: Statements::Server
- Inherits:
-
Object
- Object
- Statements::Server
- Defined in:
- lib/statements/server.rb
Class Method Summary collapse
Instance Method Summary collapse
- #call(env) ⇒ Object
- #get_accounts_js(request) ⇒ Object
- #html(html) ⇒ Object
-
#initialize ⇒ Server
constructor
A new instance of Server.
- #js(script) ⇒ Object
- #json(data) ⇒ Object
- #post_colour_json(request) ⇒ Object
- #post_search_html(request) ⇒ Object
-
#serve(type, str) ⇒ Object
noinspection RubyStringKeysInHashInspection.
Constructor Details
#initialize ⇒ Server
Returns a new instance of Server.
7 8 9 |
# File 'lib/statements/server.rb', line 7 def initialize end |
Class Method Details
.render(template, obj = nil) ⇒ Object
46 47 48 49 50 |
# File 'lib/statements/server.rb', line 46 def self.render(template, obj = nil) @templates ||= {} @templates[template] ||= ERB.new(File.read File.("../views/#{template}.erb", __FILE__)) @templates[template].result (obj || self).instance_eval { binding } end |
Instance Method Details
#call(env) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/statements/server.rb', line 11 def call(env) request = Rack::Request.new(env) verb = request.request_method.downcase path = request.path_info[1..-1].split('/') handler_name = "#{verb}_#{path.first || 'index'}".gsub('.', '_') args = [request] + path[1..-1] method = respond_to?(handler_name) && method(handler_name) if method && method.arity == args.length __send__ handler_name, *args else [404, {}, ['Not found']] end end |
#get_accounts_js(request) ⇒ Object
42 43 44 |
# File 'lib/statements/server.rb', line 42 def get_accounts_js(request) js "window.accounts = #{Account.to_json}" end |
#html(html) ⇒ Object
38 39 40 |
# File 'lib/statements/server.rb', line 38 def html(html) serve 'text/html; charset=UTF-8', html end |
#js(script) ⇒ Object
34 35 36 |
# File 'lib/statements/server.rb', line 34 def js(script) serve 'application/x-javascript', script end |
#json(data) ⇒ Object
30 31 32 |
# File 'lib/statements/server.rb', line 30 def json(data) serve 'application/json', JSON.generate(data, quirks_mode: true) end |
#post_colour_json(request) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/statements/server.rb', line 56 def post_colour_json(request) input = JSON.parse(request.body.read) transaction = Transaction.find(input['id']) rescue false if transaction transaction.colour = input['colour'] transaction.save json success: true else 400 end end |
#post_search_html(request) ⇒ Object
52 53 54 |
# File 'lib/statements/server.rb', line 52 def post_search_html(request) html self.class.render 'search', Search.new(JSON.parse request.body.read) end |
#serve(type, str) ⇒ Object
noinspection RubyStringKeysInHashInspection
26 27 28 |
# File 'lib/statements/server.rb', line 26 def serve(type, str) [200, {'Content-Type' => type, 'Content-Length' => str.length.to_s}, [str]] end |