summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar √Čtienne Deparis 2020-03-23 10:38:41 +0100
committerGravatar √Čtienne Deparis 2020-03-23 10:38:41 +0100
commitf7bc188ba657fc1e476047a13cd8686c63c1872c (patch)
treec1018efb2245975ed15efe3af305d09ec74b50da
parent0a6fc8bb70cd595f4556a93ceb8840a8994f608a (diff)
downloadstatus-f7bc188ba657fc1e476047a13cd8686c63c1872c.tar.gz
status-f7bc188ba657fc1e476047a13cd8686c63c1872c.tar.xz
status-f7bc188ba657fc1e476047a13cd8686c63c1872c.zip
chore: Refactor some method names and use pure ruby when possible
-rw-r--r--lib/mattermost_helper.rb29
-rw-r--r--status_app.rb2
-rw-r--r--views/list.slim8
-rw-r--r--views/status.slim6
4 files changed, 25 insertions, 20 deletions
diff --git a/lib/mattermost_helper.rb b/lib/mattermost_helper.rb
index c57f757..42a150a 100644
--- a/lib/mattermost_helper.rb
+++ b/lib/mattermost_helper.rb
@@ -8,7 +8,7 @@ module MattermostHelper # rubocop:disable Metrics/ModuleLength
return word + ' :white_check_mark:' if state == 'present'
# Status is absent
line = [word, ':no_entry_sign:']
- eta = key_value(status, 'eta')
+ eta = key_value_unless_blank(status, 'eta')
line.insert(1, "for #{eta}") if eta
line.join(' ')
end
@@ -19,7 +19,7 @@ module MattermostHelper # rubocop:disable Metrics/ModuleLength
end
def send_mattermost_notification
- hook_url = key_value(@config['mattermost'] || {}, 'hook_url')
+ hook_url = @config.dig('mattermost', 'hook_url')
return unless hook_url && @current_status['handle']
if ENV['RACK_ENV'] == 'development'
warn mattermost_hook_message
@@ -83,24 +83,29 @@ module MattermostHelper # rubocop:disable Metrics/ModuleLength
{ 'text' => help_body }
end
- def mattermost_tabular_format_line_value(value, key)
+ def mattermost_table_format_line_value(value, key)
return '' if value == ''
- return "[#{value}](tel:#{value.gsub(' ', '')})" if key == 'phone'
+ return "[#{value}](tel:#{value.delete(' ')})" if key == 'phone'
return "[#{key}](#{value})" if value =~ /^https?:\/\//
value
end
- def mattermost_tabular_line(status, custom_fields)
+ def mattermost_custom_values_table_line(status, custom_fields)
if status['status'] == 'present'
- custom_values = custom_fields.map do |f|
- mattermost_tabular_format_line_value(
- key_value(status['details'], f) || '', f
+ return custom_fields.map do |f|
+ mattermost_table_format_line_value(
+ status.dig('details', f) || '', f
)
end
- else
- custom_values = Array.new(custom_fields.length, ' ')
end
- name = key_value(status, 'name') || key_value(status, 'handle') || ''
+ Array.new(custom_fields.length, ' ')
+ end
+
+ def mattermost_table_line(status, custom_fields)
+ custom_values = mattermost_custom_values_table_line(status, custom_fields)
+ name = key_value_unless_blank(status, 'name') \
+ || key_value_unless_blank(status, 'handle') \
+ || ''
line = [
name, format_mattermost_status(status), status['email']
] + custom_values
@@ -112,7 +117,7 @@ module MattermostHelper # rubocop:disable Metrics/ModuleLength
cols = ['Name', 'Status', 'Email'] + custom_fields.map(&:capitalize)
table = ["| #{cols.join(' | ')} |", "|-|-|-|#{'-|' * cols.length}"]
fetch_all_statuses(filter).each do |status|
- table << mattermost_tabular_line(status, custom_fields)
+ table << mattermost_table_line(status, custom_fields)
end
table
end
diff --git a/status_app.rb b/status_app.rb
index a39ae93..a273c4b 100644
--- a/status_app.rb
+++ b/status_app.rb
@@ -23,7 +23,7 @@ class StatusApp < Sinatra::Base
include SqliteHelper
include Main
- def key_value(details, key)
+ def key_value_unless_blank(details, key)
value = details[key]&.strip
return value if value && value != ''
nil
diff --git a/views/list.slim b/views/list.slim
index 352b464..fb6b639 100644
--- a/views/list.slim
+++ b/views/list.slim
@@ -15,23 +15,23 @@ ul#presence
li.statusbox
p.label
i class='fa fa-caret-right opener'
- - if key_value(status, 'name')
+ - if key_value_unless_blank(status, 'name')
span.email = status['name']
- else
span.email = status['email']
span.status class=status['status'] = status['status']
- - if status['status'] == 'absent' && key_value(status, 'eta')
+ - if status['status'] == 'absent' && key_value_unless_blank(status, 'eta')
span.eta
i> class='fa fa-clock-o'
= convert_emojis(status['eta'])
p.info
- - if key_value(status, 'name')
+ - if key_value_unless_blank(status, 'name')
a.detail href="mailto:#{status['email']}" = status['email']
- if status['status'] == 'present'
- status['details'].each do |k, v|
- if v.strip != ''
- if k == 'phone'
- a.detail href="tel:#{v.gsub(' ', '')}" = v
+ a.detail href="tel:#{v.delete(' ')}" = v
- elsif v =~ /^https?:\/\//
a.detail href=v = k
- else
diff --git a/views/status.slim b/views/status.slim
index 3c9caab..35f6a71 100644
--- a/views/status.slim
+++ b/views/status.slim
@@ -3,7 +3,7 @@ header: include menu
p#presence
| Current status:
span.status class=@my_status = @my_status
- - if @my_status == 'absent' && key_value(@current_status, 'eta')
+ - if @my_status == 'absent' && key_value_unless_blank(@current_status, 'eta')
span.eta
i> class='fa fa-clock-o'
= convert_emojis(@current_status['eta'])
@@ -51,7 +51,7 @@ form action=url('/details') method='post'
input(type='text' id='handle_field' name='handle'
placeholder=@config['handle_field']['placeholder']
value=@current_status['handle'])
- - if key_value(@config['handle_field'], 'help')
+ - if @config.dig('handle_field', 'help')
span.help == @config['handle_field']['help']
- @config['custom_fields'].each do |detail|
@@ -61,7 +61,7 @@ form action=url('/details') method='post'
id="#{detail['name']}_field"
value=@details[detail['name']]
placeholder=detail['placeholder'])
- - if key_value(detail, 'help')
+ - if detail['help']
span.help == detail['help']
p
input type='submit' value='Save'