Skip to content

Commit

Permalink
'add hms times to clip message on share a segment' (#2509)
Browse files Browse the repository at this point in the history
* 'add hms times to clip message on share a segment'

* 'guard clause in clip_message partial'

* 'please the lovely rubo'

* Add tests for time conversion

* Convert seconds to_s: allows nil and number input

* Fix rubocop errors from helpful failing test 👮

Co-authored-by: Harpo <[email protected]>
  • Loading branch information
foglabs and mrharpo authored Dec 21, 2022
1 parent bbe2ce2 commit 20a3a6b
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
3 changes: 3 additions & 0 deletions app/controllers/catalog_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,9 @@ def show

if redirect_to_proxy_start_time?(@pbcore, params)
redirect_to catalog_path(params["id"], proxy_start_time: @pbcore.proxy_start_time) and return
elsif params["start"] && params["end"]
# proxy start time takes precendence, this is for 'share a segment'
@clip_message = %(This is a segment of a longer program (#{ seconds_to_hms(params['start']) }-#{ seconds_to_hms(params['end']) }). <a href="/catalog/#{ @pbcore.id }">Click here to view full-length item.</a>)
end
end

Expand Down
7 changes: 7 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,10 @@ def handle_date_string(date_val, type)
proper_val.to_time.strftime('%Y-%m-%dT%H:%M:%SZ')
end
end

def seconds_to_hms(seconds)
# if its just digits and period
unless seconds.to_s.delete(".") =~ /\D/
Time.at(seconds.to_f).utc.strftime("%H:%M:%S")
end
end
3 changes: 1 addition & 2 deletions app/views/catalog/_time_range.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
</div>

<div id="clip-message-container">
<p>This is a segment of a longer program. <a href="/catalog/<%= @pbcore.id %>">Click here to view full-length item.</a></p>
<p><%= @clip_message.html_safe if @clip_message %></p>
</div>

<div id="times">
Expand All @@ -30,4 +30,3 @@
</div>

<div id="video-duration"><%= @pbcore.seconds %></div>

58 changes: 58 additions & 0 deletions spec/helpers/time_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
require 'rails_helper'

RSpec.describe ApplicationHelper do
describe '#convert_seconds_to_hms' do
let(:time) { seconds_to_hms(seconds) }

context 'when input is nil' do
let(:seconds) { nil }
it 'returns 00:00:00' do
expect(time).to eq '00:00:00'
end
end

context 'when input is empty' do
let(:seconds) { '' }
it 'returns 00:00:00' do
expect(time).to eq '00:00:00'
end
end

context 'when input is not empty' do
context 'and when input contains non-digits' do
let(:seconds) { '3 hours 2 minutes and 1 second' }
it 'returns nil' do
expect(time).to eq nil
end
end

context 'when input is 0' do
let(:seconds) { 0 }
it 'returns 00:00:00' do
expect(time).to eq '00:00:00'
end
end

context 'when input is an integer' do
let(:seconds) { 3 }
it 'parses the number as the number of seconds: 00:00:03' do
expect(time).to eq '00:00:03'
end
end

context 'when input is a string' do
let(:seconds) { '3661.1' }
it 'returns 01:01:01' do
expect(time).to eq '01:01:01'
end
end

context 'when input is a float' do
let(:seconds) { 45_296.38 }
it 'returns 12:34:56' do
expect(time).to eq '12:34:56'
end
end
end
end
end

0 comments on commit 20a3a6b

Please sign in to comment.