Class: Apex::Server
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
#applicationDidFinishLaunching
Class Method Details
.content(name = :main) ⇒ Object
122
123
124
|
# File 'lib/apex/server.rb', line 122
def self.content(name=:main)
"%CONTENT-#{name}%"
end
|
.delete(path, args = {}, &block) ⇒ Object
101
102
103
|
# File 'lib/apex/server.rb', line 101
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 *************************
85
86
87
|
# File 'lib/apex/server.rb', line 85
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
118
119
120
|
# File 'lib/apex/server.rb', line 118
def self.layout(name=:main, &block)
layouts[name] = block
end
|
.layouts ⇒ Object
109
110
111
|
# File 'lib/apex/server.rb', line 109
def self.layouts
@layouts ||= {}
end
|
.patch(path, args = {}, &block) ⇒ Object
97
98
99
|
# File 'lib/apex/server.rb', line 97
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
113
114
115
116
|
# File 'lib/apex/server.rb', line 113
def self.port(port_number=nil)
@port = port_number if port_number
@port || 8080
end
|
.post(path, args = {}, &block) ⇒ Object
89
90
91
|
# File 'lib/apex/server.rb', line 89
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
93
94
95
|
# File 'lib/apex/server.rb', line 93
def self.put(path, args={}, &block)
routes[:put][path] = { handler: block, layout: args[:layout], response_type: args[:response_type] }
end
|
.routes ⇒ Object
105
106
107
|
# File 'lib/apex/server.rb', line 105
def self.routes
@routes ||= { get: {}, post: {}, put: {}, patch: {}, delete: {} }
end
|
Instance Method Details
#add_app_handlers ⇒ Object
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
|
# File 'lib/apex/server.rb', line 28
def add_app_handlers
[ :get, :post, :put, :patch, :delete ].each do |verb|
self.server.addDefaultHandlerForMethod(verb.to_s.upcase,
requestClass: 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 when Hash
response_type = :json
else
response_type = :html
end
end
case response_type
when :html
GCDWebServerDataResponse.responseWithHTML(apply_layout(response, layout))
when :json
GCDWebServerDataResponse.responseWithJSONObject(apply_layout(response, layout))
end
else
response = "<h1>404 not found</h1>"
GCDWebServerDataResponse.responseWithHTML(apply_layout(response, layout))
end
}
)
end
end
|
#add_static_handler ⇒ Object
74
75
76
77
|
# File 'lib/apex/server.rb', line 74
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
70
71
72
|
# File 'lib/apex/server.rb', line 70
def apply_layout(response, name)
layouts[name] ? layouts[name].call.to_s.gsub(self.class.content(:main), response) : response
end
|
#layouts ⇒ Object
24
25
26
|
# File 'lib/apex/server.rb', line 24
def layouts
self.class.layouts
end
|
#on_launch ⇒ Object
5
6
7
|
# File 'lib/apex/server.rb', line 5
def on_launch
start_server
end
|
#routes ⇒ Object
20
21
22
|
# File 'lib/apex/server.rb', line 20
def routes
self.class.routes
end
|
#server ⇒ Object
16
17
18
|
# File 'lib/apex/server.rb', line 16
def server
@server ||= GCDWebServer.new
end
|
#start ⇒ Object
79
80
81
|
# File 'lib/apex/server.rb', line 79
def start
server.startWithPort self.class.port, bonjourName: nil
end
|
#start_server ⇒ Object
9
10
11
12
13
14
|
# File 'lib/apex/server.rb', line 9
def start_server
return true if RUBYMOTION_ENV == "test"
add_static_handler
add_app_handlers
start
end
|