Class: Angelo::Base

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
ParamsParser, Celluloid::Logger
Defined in:
lib/angelo/base.rb

Constant Summary collapse

@@addr =
DEFAULT_ADDR
@@port =
DEFAULT_PORT
@@ping_time =
DEFAULT_PING_TIME

Constants included from ParamsParser

ParamsParser::AMPERSAND, ParamsParser::EMPTY_JSON, ParamsParser::EQUALS, ParamsParser::SEMICOLON

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ParamsParser

#content_type?, #form_encoded?, #json?, #parse_formencoded, #parse_post_body, #parse_query_string

Class Attribute Details

.app_fileObject

Returns the value of attribute app_file.



27
28
29
# File 'lib/angelo/base.rb', line 27

def app_file
  @app_file
end

.serverObject

Returns the value of attribute server.



27
28
29
# File 'lib/angelo/base.rb', line 27

def server
  @server
end

Instance Attribute Details

#responderObject

Returns the value of attribute responder.



23
24
25
# File 'lib/angelo/base.rb', line 23

def responder
  @responder
end

Class Method Details

.after(opts = {}, &block) ⇒ Object



78
79
80
# File 'lib/angelo/base.rb', line 78

def after opts = {}, &block
  define_method :after, &block
end

.before(opts = {}, &block) ⇒ Object



74
75
76
# File 'lib/angelo/base.rb', line 74

def before opts = {}, &block
  define_method :before, &block
end

.compile!(name, &block) ⇒ Object



59
60
61
62
63
64
# File 'lib/angelo/base.rb', line 59

def compile! name, &block
  define_method name, &block
  method = instance_method name
  remove_method name
  method
end

.content_type(type) ⇒ Object



106
107
108
# File 'lib/angelo/base.rb', line 106

def content_type type
  Responder.content_type type
end

.inherited(subclass) ⇒ Object



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
# File 'lib/angelo/base.rb', line 29

def inherited subclass

  # set app_file from caller stack
  #
  subclass.app_file = caller(1).map {|l| l.split(/:(?=|in )/, 3)[0,1]}.flatten[0]

  # bring RequestError into this namespace
  #
  subclass.class_eval 'class RequestError < Angelo::RequestError; end'

  class << subclass

    def root
      @root ||= File.expand_path '..', app_file
    end

    def view_dir
      v = self.class_variable_get(:@@views) rescue DEFAULT_VIEW_DIR
      File.join root, v
    end

    def public_dir
      p = self.class_variable_get(:@@public_dir) rescue DEFAULT_PUBLIC_DIR
      File.join root, p
    end

  end

end

.on_pong(&block) ⇒ Object



92
93
94
# File 'lib/angelo/base.rb', line 92

def on_pong &block
  WebsocketResponder.on_pong = block
end

.routesObject



66
67
68
69
70
71
72
# File 'lib/angelo/base.rb', line 66

def routes
  @routes ||= {}
  ROUTABLE.each do |m|
    @routes[m] ||= {}
  end
  @routes
end

.run(addr = @@addr, port = @@port) ⇒ Object



110
111
112
113
114
115
116
117
118
# File 'lib/angelo/base.rb', line 110

def run addr = @@addr, port = @@port
  @server = Angelo::Server.new self, addr, port
  @server.async.ping_websockets
  trap "INT" do
    @server.terminate if @server and @server.alive?
    exit
  end
  sleep
end

.task(name, &block) ⇒ Object



96
97
98
# File 'lib/angelo/base.rb', line 96

def task name, &block
  Angelo::Server.define_task name, &block
end

.websocket(path, &block) ⇒ Object



88
89
90
# File 'lib/angelo/base.rb', line 88

def websocket path, &block
  routes[:websocket][path] = WebsocketResponder.new &block
end

.websocketsObject



100
101
102
103
104
# File 'lib/angelo/base.rb', line 100

def websockets
  @websockets ||= Stash.new server
  @websockets.reject! &:closed?
  @websockets
end

Instance Method Details

#async(meth, *args) ⇒ Object



122
123
124
# File 'lib/angelo/base.rb', line 122

def async meth, *args
  self.class.server.async.__send__ meth, *args
end

#future(meth, *args) ⇒ Object



126
127
128
# File 'lib/angelo/base.rb', line 126

def future meth, *args
  self.class.server.future.__send__ meth, *args
end

#halt(status = 400, body = '') ⇒ Object



169
170
171
# File 'lib/angelo/base.rb', line 169

def halt status = 400, body = ''
  throw :halt, HALT_STRUCT.new(status, body)
end

#paramsObject



130
131
132
133
134
135
136
# File 'lib/angelo/base.rb', line 130

def params
  @params ||= case request.method
              when GET;  parse_query_string
              when POST; parse_post_body
              when PUT;  parse_post_body
              end
end

#request_headersObject



140
141
142
143
144
145
146
147
148
149
# File 'lib/angelo/base.rb', line 140

def request_headers
  @request_headers ||= Hash.new do |hash, key|
    if Symbol === key
      k = key.to_s.upcase
      k.gsub! UNDERSCORE, DASH
      rhv = request.headers.select {|header_key,v| header_key.upcase == k}
      hash[key] = rhv.values.first
    end
  end
end

#websocketsObject



138
# File 'lib/angelo/base.rb', line 138

def websockets; self.class.websockets; end