Class: Apex::Server

Inherits:
Object
  • Object
show all
Includes:
DelegateInterface
Defined in:
lib/apex/server.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DelegateInterface

#applicationDidFinishLaunching

Instance Attribute Details

#portObject

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



153
154
155
# File 'lib/apex/server.rb', line 153

def self.content(name=:main)
  "%CONTENT-#{name}%"
end

.delete(paths, args = {}, &block) ⇒ Object



130
131
132
133
134
# File 'lib/apex/server.rb', line 130

def self.delete(paths, args={}, &block)
  Array(paths).each do |path|
    routes[:delete][path] = { handler: block, layout: args[:layout], response_type: args[:response_type] }
  end
end

.get(paths, args = {}, &block) ⇒ Object

Class methods *************************



106
107
108
109
110
# File 'lib/apex/server.rb', line 106

def self.get(paths, args={}, &block)
  Array(paths).each do |path|
    routes[:get][path] = { handler: block, layout: args[:layout], response_type: args[:response_type] }
  end
end

.layout(name = :main, &block) ⇒ Object



149
150
151
# File 'lib/apex/server.rb', line 149

def self.layout(name=:main, &block)
  layouts[name] = block
end

.layoutsObject



140
141
142
# File 'lib/apex/server.rb', line 140

def self.layouts
  @layouts ||= {}
end

.patch(paths, args = {}, &block) ⇒ Object



124
125
126
127
128
# File 'lib/apex/server.rb', line 124

def self.patch(paths, args={}, &block)
  Array(paths).each do |path|
    routes[:patch][path] = { handler: block, layout: args[:layout], response_type: args[:response_type] }
  end
end

.port(port_number = nil) ⇒ Object



144
145
146
147
# File 'lib/apex/server.rb', line 144

def self.port(port_number=nil)
  @port = port_number if port_number
  @port || 8080
end

.post(paths, args = {}, &block) ⇒ Object



112
113
114
115
116
# File 'lib/apex/server.rb', line 112

def self.post(paths, args={}, &block)
  Array(paths).each do |path|
    routes[:post][path] = { handler: block, layout: args[:layout], response_type: args[:response_type] }
  end
end

.put(paths, args = {}, &block) ⇒ Object



118
119
120
121
122
# File 'lib/apex/server.rb', line 118

def self.put(paths, args={}, &block)
  Array(paths).each do |path|
    routes[:put][path] = { handler: block, layout: args[:layout], response_type: args[:response_type] }
  end
end

.routesObject



136
137
138
# File 'lib/apex/server.rb', line 136

def self.routes
  @routes ||= { get: {}, post: {}, put: {}, patch: {}, delete: {} }
end

Instance Method Details

#add_app_handlersObject



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_handlerObject



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

#layoutsObject



26
27
28
# File 'lib/apex/server.rb', line 26

def layouts
  self.class.layouts
end

#on_launchObject



7
8
9
# File 'lib/apex/server.rb', line 7

def on_launch
  start_server
end

#routesObject



22
23
24
# File 'lib/apex/server.rb', line 22

def routes
  self.class.routes
end

#serverObject



18
19
20
# File 'lib/apex/server.rb', line 18

def server
  @server ||= GCDWebServer.new
end

#startObject



92
93
94
# File 'lib/apex/server.rb', line 92

def start
  server.startWithPort port, bonjourName: nil
end

#start_serverObject



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

#stopObject



96
97
98
# File 'lib/apex/server.rb', line 96

def stop
  server.stop
end