Class: Flack::App

Inherits:
Object
  • Object
show all
Defined in:
lib/flack/app/index.rb,
lib/flack/app.rb,
lib/flack/app/static.rb,
lib/flack/app/helpers.rb,
lib/flack/app/message.rb,
lib/flack/app/messages.rb,
lib/flack/app/executions.rb

Overview

/executions/*

Constant Summary collapse

METHS =
instance_methods
.collect(&:to_s)
.select { |m| m.match(/\A(get|head|put|post|delete)_.+\z/) }
.select { |m| instance_method(m).arity == 1 }
.collect { |m|
  s = m.split('_')
  if s.length == 3 && s[2] == 'suffix'
    [ m, s.shift.upcase, [ /\.#{s[0]}\z/ ] ]
  else
    [ m, s.shift.upcase, s ]
  end }
.collect(&:freeze).freeze
STATIC_FILE =
Rack::File.new(
    File.absolute_path(
      File.join(
File.dirname(__FILE__), '..', 'static')))

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, opts = {}) ⇒ App

Returns a new instance of App.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/flack/app.rb', line 15

def initialize(root, opts={})

  @root = root

  conf_path =
    root.end_with?(File::SEPARATOR + 'conf.json') ?
    root :
    File.join(@root, 'etc', 'conf.json')

  @unit = Flor::Unit.new(conf_path)

  @unit.start unless opts[:start] == false

  self.class.unit = @unit

  @unit
end

Instance Attribute Details

#unitObject (readonly)

Returns the value of attribute unit.



13
14
15
# File 'lib/flack/app.rb', line 13

def unit
  @unit
end

Class Method Details

.unitObject



38
39
40
# File 'lib/flack/app.rb', line 38

def self.unit
  @unit
end

.unit=(u) ⇒ Object



41
42
43
# File 'lib/flack/app.rb', line 41

def self.unit=(u)
  @unit = u
end

Instance Method Details

#call(env) ⇒ Object



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
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/flack/app.rb', line 45

def call(env)

  flack_path_info = (env['PATH_INFO'][1..-1] || '')
    .split('/')

  flack_path_info = [ 'index' ] if flack_path_info.empty?

  env['flack.path_info'] = flack_path_info

  METHS.each do |m|

    next if env['REQUEST_METHOD'] != m[1]
    next if flack_path_info.length != m[2].length

    match = true
    args = []

    for i in 0..(flack_path_info.length - 1)

      break unless match

      pi = flack_path_info[i]
      mi = m[2][i]

      break if mi == 'plus' || mi == 'star'

      if mi == 'i'
        match = pi.match(/\A\d+\z/)
        args << pi.to_i
      elsif mi == 's'
        args << pi
      elsif mi.is_a?(Regexp)
        match = pi.match(mi)
      else
        match = (pi == mi)
      end
    end

    next unless match

    env['flack.args'] = args
    env['flack.query_string'] = Rack::Utils.parse_query(env['QUERY_STRING'])
    env['flack.root_uri'] = determine_root_uri(env)

    return send(m[0], env)
  end

  respond_not_found(env)
end

#get_debug(env) ⇒ Object Also known as: get_debug_i



95
# File 'lib/flack/app.rb', line 95

def get_debug(env); debug_respond(env); end

#get_executions(env) ⇒ Object

GET /executions



8
9
10
11
12
13
14
# File 'lib/flack/app/executions.rb', line 8

def get_executions(env)

# TODO implement paging
  env['flack.rel'] = 'flack:executions'

  respond(env, @unit.executions.all)
end

#get_executions_i(env) ⇒ Object

GET /executions/<id>



18
19
20
21
22
23
24
25
26
27
# File 'lib/flack/app/executions.rb', line 18

def get_executions_i(env)

  env['flack.rel'] = 'flack:executions/id'

  if exe = @unit.executions[env['flack.args'][0]]
    respond(env, exe)
  else
    respond_not_found(env)
  end
end

#get_executions_s(env) ⇒ Object

GET /executions/<exid> GET /executions/<domain> GET /executions/<domain>* GET /executions/<domain>.*



34
35
36
37
38
39
40
41
42
43
# File 'lib/flack/app/executions.rb', line 34

def get_executions_s(env)

  arg = env['flack.args'][0]

  if arg.count('-') == 0
    get_executions_by_domain(env, arg)
  else
    get_executions_by_exid(env, arg)
  end
end

#get_index(env) ⇒ Object



6
7
8
9
10
# File 'lib/flack/app/index.rb', line 6

def get_index(env)

# TODO
  respond(env, nil)
end

#get_messages(env) ⇒ Object

GET /messages



7
8
9
10
11
12
13
# File 'lib/flack/app/messages.rb', line 7

def get_messages(env)

# TODO implement paging
  env['flack.rel'] = 'flack:messages'

  respond(env, @unit.messages.all)
end

#get_messages_i(env) ⇒ Object

GET /messages/<id>



16
17
18
19
20
21
22
23
24
25
# File 'lib/flack/app/messages.rb', line 16

def get_messages_i(env)

  env['flack.rel'] = 'flack:messages'

  if exe = @unit.messages[env['flack.args'][0]]
    respond(env, exe)
  else
    respond_not_found(env)
  end
end

#get_messages_s(env) ⇒ Object

GET /messages/<exid>



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/flack/app/messages.rb', line 28

def get_messages_s(env)

# TODO implement paging
  arg = env['flack.args'][0]

  if Flor.point?(arg)
    get_messages_by_point(env, arg)
  else
    get_messages_by_exid(env, arg)
  end
end

#get_messages_s_s(env) ⇒ Object

GET /messages/<exid>/<point>



41
42
43
44
45
46
47
48
49
50
# File 'lib/flack/app/messages.rb', line 41

def get_messages_s_s(env)

  env['flack.rel'] = 'flack:messages/exid/point'

  exid, point = env['flack.args']

  respond(
    env,
    @unit.messages.where(exid: exid, point: point).all)
end

#post_message(env) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/flack/app/message.rb', line 6

def post_message(env)

  msg = JSON.load(env['rack.input'].read)

  pt = msg['point']

  return respond_bad_request(env, 'missing msg point') \
    unless pt
  return respond_bad_request(env, "bad msg point #{pt.inspect}") \
    unless %w[ launch cancel reply ].include?(pt)

  r = self.send("queue_#{pt}", env, msg, { point: pt })

  respond(env, r)
end

#serve_file(env) ⇒ Object Also known as: get_html_suffix, get_css_suffix, get_js_suffix



13
14
15
16
# File 'lib/flack/app/static.rb', line 13

def serve_file(env)

  STATIC_FILE.call(env)
end

#shutdownObject



33
34
35
36
# File 'lib/flack/app.rb', line 33

def shutdown

  @unit.shutdown
end