Module: Crawler::Cinema::Duration

Defined in:
lib/crawler/cinema/duration.rb

Constant Summary collapse

SECONDS_PER_HOUR =
3600
SECONDS_PER_MINUTE =
60

Class Method Summary collapse

Class Method Details

.parse(str) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/crawler/cinema/duration.rb', line 9

def parse(str)
  duration = 0
  duration_matches = str.match(/^((?<hours>\d+)(h|hours?))?\s*((?<minutes>\d+)(m|minutes?|mins?))?\s*((?<seconds>\d+)(s|seconds?|secs?))?$/i)

  if duration_matches
    duration += duration_matches[:hours].to_i * SECONDS_PER_HOUR if duration_matches[:hours]
    duration += duration_matches[:minutes].to_i * SECONDS_PER_MINUTE if duration_matches[:minutes]
    duration += duration_matches[:seconds].to_i if duration_matches[:seconds]
  end

  duration
end