Class: Apex::Server
- Inherits:
-
Object
- Object
- Apex::Server
- Includes:
- DelegateInterface
- Defined in:
- lib/apex/server.rb
Instance Attribute Summary collapse
-
#port ⇒ Object
Returns the value of attribute port.
Class Method Summary collapse
- .content(name = :main) ⇒ Object
- .delete(path, args = {}, &block) ⇒ Object
-
.get(path, args = {}, &block) ⇒ Object
Class methods *************************.
- .layout(name = :main, &block) ⇒ Object
- .layouts ⇒ Object
- .patch(path, args = {}, &block) ⇒ Object
- .port(port_number = nil) ⇒ Object
- .post(path, args = {}, &block) ⇒ Object
- .put(path, args = {}, &block) ⇒ Object
- .routes ⇒ Object
Instance Method Summary collapse
- #add_app_handlers ⇒ Object
- #add_static_handler ⇒ Object
- #apply_layout(response, name) ⇒ Object
- #layouts ⇒ Object
- #on_launch ⇒ Object
- #routes ⇒ Object
- #server ⇒ Object
- #start ⇒ Object
- #start_server ⇒ Object
- #stop ⇒ Object
Methods included from DelegateInterface
#applicationDidFinishLaunching
Instance Attribute Details
#port ⇒ Object
Returns the value of attribute port.
5 6 7 |
# File 'lib/apex/server.rb', line 5 def port @port end |
Class Method Details
.content(name = :main) ⇒ Object
143 144 145 |
# File 'lib/apex/server.rb', line 143 def self.content(name=:main) "%CONTENT-#{name}%" end |
.delete(path, args = {}, &block) ⇒ Object
122 123 124 |
# File 'lib/apex/server.rb', line 122 def self.delete(path, args={}, &block) routes[:delete][path] = { handler: block, layout: args[:layout], response_type: args[:response_type] } end |
.get(path, args = {}, &block) ⇒ Object
Class methods *************************
106 107 108 |
# File 'lib/apex/server.rb', line 106 def self.get(path, args={}, &block) routes[:get][path] = { handler: block, layout: args[:layout], response_type: args[:response_type] } end |
.layout(name = :main, &block) ⇒ Object
139 140 141 |
# File 'lib/apex/server.rb', line 139 def self.layout(name=:main, &block) layouts[name] = block end |
.layouts ⇒ Object
130 131 132 |
# File 'lib/apex/server.rb', line 130 def self.layouts @layouts ||= {} end |
.patch(path, args = {}, &block) ⇒ Object
118 119 120 |
# File 'lib/apex/server.rb', line 118 def self.patch(path, args={}, &block) routes[:patch][path] = { handler: block, layout: args[:layout], response_type: args[:response_type] } end |
.port(port_number = nil) ⇒ Object
134 135 136 137 |
# File 'lib/apex/server.rb', line 134 def self.port(port_number=nil) @port = port_number if port_number @port || 8080 end |
.post(path, args = {}, &block) ⇒ Object
110 111 112 |
# File 'lib/apex/server.rb', line 110 def self.post(path, args={}, &block) routes[:post][path] = { handler: block, layout: args[:layout], response_type: args[:response_type] } end |
.put(path, args = {}, &block) ⇒ Object
114 115 116 |
# File 'lib/apex/server.rb', line 114 def self.put(path, args={}, &block) routes[:put][path] = { handler: block, layout: args[:layout], response_type: args[:response_type] } end |
.routes ⇒ Object
126 127 128 |
# File 'lib/apex/server.rb', line 126 def self.routes @routes ||= { get: {}, post: {}, put: {}, patch: {}, delete: {} } end |
Instance Method Details
#add_app_handlers ⇒ Object
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 |
# File 'lib/apex/server.rb', line 30 def add_app_handlers verb_to_request_class = {:post => GCDWebServerURLEncodedFormRequest} [ :get, :post, :put, :patch, :delete ].each do |verb| self.server.addDefaultHandlerForMethod(verb.to_s.upcase, requestClass: (verb_to_request_class[verb] ? verb_to_request_class[verb] : GCDWebServerRequest), processBlock: -> (raw_request) { layout = false request = Request.new(raw_request) if (routes_verb = self.routes[verb]) && (request_path = routes_verb[request.path]) && (response_block = request_path[:handler]) request_args = [request].first(response_block.arity) response = response_block.call(*request_args) layout = request_path[:layout] unless response_type = request_path[:response_type] case response # Auto detect when Hash response_type = :json else response_type = :html # TODO, do the the rest of the types end end case response_type when :html GCDWebServerDataResponse.responseWithHTML(apply_layout(response, layout)) when :json GCDWebServerDataResponse.responseWithJSONObject(apply_layout(response, layout)) # TODO, do the the rest of the types end else file = NSBundle.mainBundle.pathForResource("assets", ofType: nil) + request.raw.path if File.exists?(file) ext = File.extname(file) if MimeTypes.full_list.keys.include?(ext) response = File.read(file) GCDWebServerDataResponse.responseWithData(response.to_data, contentType: MimeTypes.for(ext)) else GCDWebServerDataResponse.responseWithHTML(apply_layout("<h1>404 not found</h1>", layout)) end else GCDWebServerDataResponse.responseWithHTML(apply_layout("<h1>404 not found</h1>", layout)) end end } ) end end |
#add_static_handler ⇒ Object
87 88 89 90 |
# File 'lib/apex/server.rb', line 87 def add_static_handler public_path = NSBundle.mainBundle.pathForResource("assets", ofType:nil) self.server.addGETHandlerForBasePath("/", directoryPath:public_path, indexFilename:nil, cacheAge:3600, allowRangeRequests:false) end |
#apply_layout(response, name) ⇒ Object
83 84 85 |
# File 'lib/apex/server.rb', line 83 def apply_layout(response, name) layouts[name] ? layouts[name].call.to_s.gsub(self.class.content(:main), response) : response end |
#layouts ⇒ Object
26 27 28 |
# File 'lib/apex/server.rb', line 26 def layouts self.class.layouts end |
#on_launch ⇒ Object
7 8 9 |
# File 'lib/apex/server.rb', line 7 def on_launch start_server end |
#routes ⇒ Object
22 23 24 |
# File 'lib/apex/server.rb', line 22 def routes self.class.routes end |
#server ⇒ Object
18 19 20 |
# File 'lib/apex/server.rb', line 18 def server @server ||= GCDWebServer.new end |
#start ⇒ Object
92 93 94 |
# File 'lib/apex/server.rb', line 92 def start server.startWithPort port, bonjourName: nil end |
#start_server ⇒ Object
11 12 13 14 15 16 |
# File 'lib/apex/server.rb', line 11 def start_server return true if RUBYMOTION_ENV == "test" add_static_handler add_app_handlers start end |
#stop ⇒ Object
96 97 98 |
# File 'lib/apex/server.rb', line 96 def stop server.stop end |