Module: PgEventstore::Web::Subscriptions::Helpers

Defined in:
lib/pg_eventstore/web/subscriptions/helpers.rb

Instance Method Summary collapse

Instance Method Details

#alive?(interval, last_updated_at) ⇒ Boolean

Parameters:

  • interval (Integer)
  • last_updated_at (Time)

Returns:

  • (Boolean)


112
113
114
115
# File 'lib/pg_eventstore/web/subscriptions/helpers.rb', line 112

def alive?(interval, last_updated_at)
  # -1 is added as a margin to prevent false-positive result
  last_updated_at > Time.now.utc - interval - 1
end

#colored_state(state, interval, updated_at) ⇒ String

Returns html status.

Parameters:

  • state (String)
  • updated_at (Time)

Returns:

  • (String)

    html status



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
# File 'lib/pg_eventstore/web/subscriptions/helpers.rb', line 81

def colored_state(state, interval, updated_at)
  text_class =
    case state
    when RunnerState::STATES[:running]
      alive?(interval, updated_at) ? 'text-success' : 'text-warning'
    when RunnerState::STATES[:dead]
      'text-danger'
    else
      'text-info'
    end

  if alive?(interval, updated_at)
    "      <span class=\"\#{text_class}\">\#{state}</span>\n    HTML\n  else\n    title = <<~TEXT\n      Something is wrong. Last update was more than \#{interval} seconds ago(\#{updated_at}).\n    TEXT\n    <<~HTML\n      <span class=\"\#{text_class} text-nowrap\">\n        \#{state}\n        <i class=\"fa fa-question-circle\" data-toggle=\"tooltip\" title=\"\#{title}\"></i>\n      </span>\n    HTML\n  end\nend\n"

#delete_all_subscriptions_url(ids) ⇒ String

Parameters:

  • ids (Array<Integer>)

Returns:

  • (String)


119
120
121
122
# File 'lib/pg_eventstore/web/subscriptions/helpers.rb', line 119

def delete_all_subscriptions_url(ids)
  encoded_params = Rack::Utils.build_nested_query(ids: ids)
  url("/delete_all_subscriptions?#{encoded_params}")
end

#delete_event_url(global_position) ⇒ String

Parameters:

  • global_position (Integer)

Returns:

  • (String)


126
127
128
# File 'lib/pg_eventstore/web/subscriptions/helpers.rb', line 126

def delete_event_url(global_position)
  url("/delete_event/#{global_position}")
end

#delete_stream_url(stream_attrs) ⇒ String

Parameters:

  • stream_attrs (Hash)

Returns:

  • (String)


132
133
134
135
# File 'lib/pg_eventstore/web/subscriptions/helpers.rb', line 132

def delete_stream_url(stream_attrs)
  encoded_params = Rack::Utils.build_nested_query(stream_attrs)
  url("/delete_stream?#{encoded_params}")
end

#subscription_cmd(cmd_name) ⇒ String

Returns command name.

Parameters:

  • cmd_name (String)

    command name

Returns:

  • (String)

    command name



64
65
66
67
68
# File 'lib/pg_eventstore/web/subscriptions/helpers.rb', line 64

def subscription_cmd(cmd_name)
  validate_subscription_cmd(cmd_name)

  cmd_name
end

#subscription_cmd_url(set_id, id, cmd) ⇒ String

Parameters:

  • set_id (Integer)
  • id (Integer)
  • cmd (String)

Returns:

  • (String)


35
36
37
# File 'lib/pg_eventstore/web/subscriptions/helpers.rb', line 35

def subscription_cmd_url(set_id, id, cmd)
  url("/subscription_cmd/#{set_id}/#{id}/#{cmd}")
end

#subscriptions_set_cmd(cmd_name) ⇒ String

Returns command name.

Parameters:

  • cmd_name (String)

    command name

Returns:

  • (String)

    command name



48
49
50
51
52
# File 'lib/pg_eventstore/web/subscriptions/helpers.rb', line 48

def subscriptions_set_cmd(cmd_name)
  validate_subscriptions_set_cmd(cmd_name)

  cmd_name
end

#subscriptions_set_cmd_url(id, cmd) ⇒ String

Parameters:

  • id (Integer)
  • cmd (String)

Returns:

  • (String)


42
43
44
# File 'lib/pg_eventstore/web/subscriptions/helpers.rb', line 42

def subscriptions_set_cmd_url(id, cmd)
  url("/subscriptions_set_cmd/#{id}/#{cmd}")
end

#subscriptions_stateString?

Returns:

  • (String, nil)


27
28
29
# File 'lib/pg_eventstore/web/subscriptions/helpers.rb', line 27

def subscriptions_state
  params[:state] if PgEventstore::RunnerState::STATES.values.include?(params[:state])
end

#subscriptions_state_url(state:, **params) ⇒ String

Parameters:

  • state (String)

Returns:

  • (String)


18
19
20
21
22
23
24
# File 'lib/pg_eventstore/web/subscriptions/helpers.rb', line 18

def subscriptions_state_url(state:, **params)
  params = params.compact
  return url("/subscriptions/#{state}") if params.empty?

  encoded_params = Rack::Utils.build_nested_query(params)
  url("/subscriptions/#{state}?#{encoded_params}")
end

#subscriptions_url(set_name: nil) ⇒ String

Parameters:

  • set_name (String, nil) (defaults to: nil)

Returns:

  • (String)


9
10
11
12
13
14
# File 'lib/pg_eventstore/web/subscriptions/helpers.rb', line 9

def subscriptions_url(set_name: nil)
  return url('/subscriptions') unless set_name

  encoded_params = Rack::Utils.build_nested_query(set_name: set_name)
  url("/subscriptions?#{encoded_params}")
end

#validate_subscription_cmd(cmd_name) ⇒ void

This method returns an undefined value.

Parameters:

  • cmd_name (String)

Raises:

  • (RuntimeError)

    in case if command class is not found



73
74
75
76
# File 'lib/pg_eventstore/web/subscriptions/helpers.rb', line 73

def validate_subscription_cmd(cmd_name)
  cmd_class = SubscriptionRunnerCommands.command_class(cmd_name)
  raise "Subscription command #{cmd_name.inspect} does not exist" unless cmd_class.known_command?
end

#validate_subscriptions_set_cmd(cmd_name) ⇒ void

This method returns an undefined value.

Parameters:

  • cmd_name (String)

Raises:

  • (RuntimeError)

    in case if command class is not found



57
58
59
60
# File 'lib/pg_eventstore/web/subscriptions/helpers.rb', line 57

def validate_subscriptions_set_cmd(cmd_name)
  cmd_class = SubscriptionFeederCommands.command_class(cmd_name)
  raise "SubscriptionsSet command #{cmd_name.inspect} does not exist" unless cmd_class.known_command?
end