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

Defined in:
lib/pg_eventstore/web/subscriptions/helpers.rb,
sig/pg_eventstore/web/subscriptions/helpers.rbs

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

@param state

@param updated_at

@return — html status

Parameters:

  • state (String)
  • interval (Integer)
  • updated_at (Time)

Returns:

  • (String)


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)
    <<~HTML
      <span class="#{text_class}">#{state}</span>
    HTML
  else
    title = <<~TEXT
      Something is wrong. Last update was more than #{interval} seconds ago(#{updated_at}).
    TEXT
    <<~HTML
      <span class="#{text_class} text-nowrap">
        #{state}
        <i class="fa fa-question-circle" data-toggle="tooltip" title="#{title}"></i>
      </span>
    HTML
  end
end

#delete_all_subscriptions_url(ids) ⇒ String

@param ids

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:)
  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

@param cmd_name — command name

@return — command name

Parameters:

  • cmd_name (String)

Returns:

  • (String)


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

@param set_id

@param id

@param cmd

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

@param cmd_name — command name

@return — command name

Parameters:

  • cmd_name (String)

Returns:

  • (String)


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

@param id

@param cmd

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)
  • state: (String)
  • params (Object)

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

@param set_name

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:)
  url("/subscriptions?#{encoded_params}")
end

#validate_subscription_cmd(cmd_name) ⇒ void

This method returns an undefined value.

@param cmd_name

Parameters:

  • cmd_name (String)


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.

@param cmd_name

Parameters:

  • cmd_name (String)


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