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/pointers.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 }
.sort
.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.



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

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

  Flack.on_unit_created(@unit) if Flack.respond_to?(:on_unit_created)

  @unit
end

Instance Attribute Details

#unitObject (readonly)

Returns the value of attribute unit.



15
16
17
# File 'lib/flack/app.rb', line 15

def unit
  @unit
end

Class Method Details

.unitObject



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

def self.unit
  @unit
end

.unit=(u) ⇒ Object



45
46
47
# File 'lib/flack/app.rb', line 45

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

Instance Method Details

#call(env) ⇒ Object



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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/flack/app.rb', line 49

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)

rescue => err

  $stderr.puts '=' * 80
  $stderr.puts Time.now.to_s
  $stderr.puts '-' * 80
  $stderr.puts err.inspect
  $stderr.puts err.backtrace
  $stderr.puts '-' * 80
  PP.pp(env, $stderr)
  $stderr.puts ('=' * 79) + '.'

  respond_internal_server_error(env, err)
end

#delete_executions_s(env) ⇒ Object

DELETE /executions/<exid>



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
94
95
# File 'lib/flack/app/executions.rb', line 66

def delete_executions_s(env)

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

  return respond_not_found(env) \
    unless @unit.executions.where(exid: exid).count > 0

  r = { exid: exid, counts: {} }
  cs = r[:counts]

  @unit.storage.db.transaction do

    cs[:messages] = @unit.messages.where(exid: exid).count
    cs[:executions] = @unit.executions.where(exid: exid).count
    cs[:pointers] = @unit.pointers.where(exid: exid).count
    cs[:timers] = @unit.timers.where(exid: exid).count
    cs[:traps] = @unit.traps.where(exid: exid).count
      #
      # not sure if the DB adapter returns the DELETE count,
      # so counting first

    @unit.messages.where(exid: exid).delete
    @unit.executions.where(exid: exid).delete
    @unit.pointers.where(exid: exid).delete
    @unit.timers.where(exid: exid).delete
    @unit.traps.where(exid: exid).delete
  end

  respond(env, r)
end

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



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

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

#get_executions(env) ⇒ Object

GET /executions GET /executions?exid=<exid_prefix> GET /executions?dexid=<date_exid_prefix>



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

def get_executions(env)

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

  statuses = query_values(env, 'statuses', 'status')
  exid = query_value(env, 'exid')
  dexid = query_value(env, 'dexid')

  q = @unit.executions
    #
  q = q.where(status: statuses) if statuses
  q = q.where(Sequel.like(:exid, "#{exid}%")) if exid
  q = q.where(Sequel.like(:exid, "%-#{dexid}%")) if dexid
    #
  q = q.order(:exid)

  if query_value(env, 'count')
    respond(env, { count: q.count })
  else
    respond(env, q.all)
  end
end

#get_executions_i(env) ⇒ Object

GET /executions/<id>



37
38
39
40
41
42
43
44
45
46
# File 'lib/flack/app/executions.rb', line 37

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>.*



53
54
55
56
57
58
59
60
61
62
# File 'lib/flack/app/executions.rb', line 53

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



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

def get_index(env)

# TODO
  respond(env, nil)
end

#get_messages(env) ⇒ Object

GET /messages



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

def get_messages(env)

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

# TODO {?exid,dexid,count} like for /executions and /pointers

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

#get_messages_i(env) ⇒ Object

GET /messages/<id>



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

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>



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/flack/app/messages.rb', line 31

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>



44
45
46
47
48
49
50
51
52
53
# File 'lib/flack/app/messages.rb', line 44

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

#get_pointers(env) ⇒ Object

GET /pointers GET /pointers?exid=<exid_prefix> GET /pointers?dexid=<date_exid_prefix>



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/flack/app/pointers.rb', line 11

def get_pointers(env)

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

  types = query_values(env, 'types', 'type')

  exid = query_value(env, 'exid')
  dexid = query_value(env, 'dexid')

  q = @unit.pointers
    #
  q = q.where(type: types) if types
  q = q.where(Sequel.like(:exid, "#{exid}%")) if exid
  q = q.where(Sequel.like(:exid, "%-#{dexid}%")) if dexid
    #
  q = q.order(:exid)

  if query_value(env, 'count')
    respond(env, { count: q.count })
  else
    respond(env, q.all)
  end
end

#get_pointers_s(env) ⇒ Object

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



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

def get_pointers_s(env)

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

  types = query_values(env, 'types', 'type')

  if arg.count('-') == 0
    get_pointers_by_domain(env, arg, types)
  else
    get_pointers_by_exid(env, arg, types)
  end
end

#post_message(env) ⇒ Object



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

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



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

def serve_file(env)

  STATIC_FILE.call(env)
end

#shutdownObject



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

def shutdown

  @unit.shutdown
end