Class: Angelo::Base

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

Defined Under Namespace

Classes: WebsocketsArray

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



67
68
69
# File 'lib/angelo/base.rb', line 67

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

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



63
64
65
# File 'lib/angelo/base.rb', line 63

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

.compile!(name, &block) ⇒ Object



48
49
50
51
52
53
# File 'lib/angelo/base.rb', line 48

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

.content_type(type) ⇒ Object



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

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

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

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

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

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

end

.on_pong(&block) ⇒ Object



81
82
83
# File 'lib/angelo/base.rb', line 81

def on_pong &block
  WebsocketResponder.on_pong = block
end

.routesObject



55
56
57
58
59
60
61
# File 'lib/angelo/base.rb', line 55

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

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



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

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

.socket(path, &block) ⇒ Object



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

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

.task(name, &block) ⇒ Object



85
86
87
# File 'lib/angelo/base.rb', line 85

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

.websocketsObject



89
90
91
92
93
# File 'lib/angelo/base.rb', line 89

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

Instance Method Details

#async(meth, *args) ⇒ Object



111
112
113
# File 'lib/angelo/base.rb', line 111

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

#future(meth, *args) ⇒ Object



115
116
117
# File 'lib/angelo/base.rb', line 115

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

#paramsObject



119
120
121
122
123
124
125
# File 'lib/angelo/base.rb', line 119

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



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

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



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

def websockets; self.class.websockets; end