Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions app/models/alma.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ class Alma
attr_reader :date_to

def initialize(args)
@date_from = args[:date_from]
@date_to = args[:date_to]
dates_input = add_extra_day(args[:date_from], args[:date_to])
@date_from = dates_input.first
@date_to = dates_input.last
@limited = args[:limited]
@raw_hours = fetch_dates
end
Expand All @@ -21,6 +22,14 @@ def hours_json

private

def add_extra_day(day_from, day_to)
# get next day date and add it to the end of the list (this is used in the
# parser to determine labels that change depending on future times
dates_new = [day_from, day_to].sort
dates_new << DateTime.parse(dates_new.last).next_day.strftime('%Y-%m-%d')
[dates_new.first, dates_new.last]
end

##
# Fetch the Alma Open Hours in json format for the givend dates
# @return [Hash] raw hours from Alma
Expand Down
34 changes: 25 additions & 9 deletions app/services/api/library_open_hours_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ def self.call(raw_hours, limited = false)
def call
hours_data = {}
@raw_hours.each do |day, hour|
hours_data[day] = lib_open_hours(hour['day'])
next_day = @raw_hours[DateTime.parse(day).next_day.to_s]
hours_data[day] = lib_open_hours(hour['day'], next_day)
end
hours_data.to_json
hours_data.without(hours_data.to_a.last.first).to_json
# hours_data.to_json
end

private
Expand All @@ -29,7 +31,7 @@ def initialize(raw_hours, limited = false)
@limited = limited
end

def lib_open_hours(raw_json)
def lib_open_hours(raw_json, next_day_json)
return {} if raw_json.blank?

alma_day = raw_json.first
Expand All @@ -42,8 +44,8 @@ def lib_open_hours(raw_json)
close: to_hour.present? ? format_hour(to_hour) : '',
string_date: alma_date.strftime('%a, %b %-d, %Y'),
sortable_date: alma_date.strftime('%Y-%m-%d'),
formatted_hours: all_formatted_hours(alma_day, false),
formatted_hours_plain_text: all_formatted_hours(alma_day, true),
formatted_hours: all_formatted_hours(day: alma_day, next_day: next_day_json, plain_text: false),
formatted_hours_plain_text: all_formatted_hours(day: alma_day, next_day: next_day_json, plain_text: true),
open_all_day: open_all_day?(from_hour, to_hour),
closes_at_night: closes_at_night?(last_to_hour),
event_desc: '',
Expand Down Expand Up @@ -89,13 +91,14 @@ def event_status(day)
all_open_hours(day).count.zero? ? 'CLOSE' : ''
end

def all_formatted_hours(day, plain_text = false)
def all_formatted_hours(day:, next_day:, plain_text: false)
return 'Closed' if all_open_hours(day).count.zero?

return limited_formatted_hours(day, plain_text) if limited_hours?(day)

# byebug if day['date'] == '2020-03-13Z'
all_open_hours(day).map do |h|
formatted_hours(h[:open], h[:close])
formatted_hours(open_time: h[:open], close_time: h[:close], day: day, next_day: next_day)
end.join(hours_delimiter(plain_text))
end

Expand Down Expand Up @@ -141,11 +144,18 @@ def open_all_day?(open_time, close_time)
open_time == '00:00' && close_time == '23:59' ? true : false
end

def formatted_hours(open_time, close_time)
if close_time == '00:14' || partially_open?(open_time, close_time)
def formatted_hours(open_time:, close_time:, day:, next_day:)
# byebug if day['date'] == '2020-03-13Z'
if close_time == '00:14'
"#{format_hour(open_time)} - No closing"
elsif partially_open?(open_time, close_time) && close_midnight?(next_day)
"#{format_hour(open_time)} - midnight"
elsif partially_open?(open_time, close_time)
"#{format_hour(open_time)} - No closing"
elsif open_time == '00:14'
"Closes at #{format_hour(close_time)}"
elsif open_time == '00:00' && close_time == '23:59' && close_midnight?(next_day)
"#{format_hour(open_time)} - midnight"
elsif open_time == '00:00' && close_time == '23:59'
'Open 24 Hours'
elsif open_time.eql? close_time
Expand All @@ -155,6 +165,12 @@ def formatted_hours(open_time, close_time)
end
end

# check if a day is closing at midnight (next day doesn't open at 00:00)
def close_midnight?(next_day)
next_day_open_time = next_day.try(:[], 'day').try(:first).try(:[], 'hour').try(:first).try(:[], 'from')
next_day_open_time.present? ? next_day_open_time != '00:00' : false
end

def format_hour(time)
Time.parse(time).strftime('%-l:%M%P')
end
Expand Down
13 changes: 13 additions & 0 deletions spec/controllers/api_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@
end
let(:alma_with_special_hours) { AlmaSpecialHours.new }
let(:url) { "#{ENV['ALMA_OPEN_HOURS_URL']}?apikey=#{ENV['ALMA_API_KEY']}&from=#{date_from}&to=#{date_to}" }
let(:url_next) { "#{ENV['ALMA_OPEN_HOURS_URL']}?apikey=#{ENV['ALMA_API_KEY']}&from=#{next_day}&to=#{next_day}" }
let(:open_and_special_hours_url) { "#{ENV['ALMA_SPECIAL_HOURS_URL']}?apikey=#{ENV['ALMA_API_KEY']}&scope=#{ENV['ALMA_SPECIAL_HOURS_SCOPE']}" }
let(:xml) { File.read("spec/fixtures/alma_open_hours.xml") }
let(:date_from) { '2019-06-24' }
let(:date_to) { '2019-06-24' }
let(:next_day) { DateTime.parse(date_to).next_day.strftime('%Y-%m-%d') }
let(:dates) { [date_from, date_to] }

before do
Expand All @@ -53,12 +55,23 @@
'User-Agent'=>'Ruby'
}).
to_return(status: 200, body: raw_json, headers: {})

stub_request(:get, url_next).
with(
headers: {
'Accept'=>'application/json',
'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'User-Agent'=>'Ruby'
}).
to_return(status: 200, body: raw_json, headers: {})
end

context "When no day is provided" do
let(:day) { '' }
let(:date_from) { day }
let(:date_to) { day }
let(:next_day) { '' }


before do
allow(API::OpenAndSpecialHoursXmlToJsonParser).to receive(:call).with(anything(), []).and_return(valid_json)
Expand Down
2 changes: 2 additions & 0 deletions spec/fixtures/alma_january_2020_raw.json
Original file line number Diff line number Diff line change
Expand Up @@ -584,5 +584,7 @@
]
}
]
},
"2020-02-01T00:00:00+00:00": {
}
}
3 changes: 3 additions & 0 deletions spec/fixtures/alma_june_2019_raw.json
Original file line number Diff line number Diff line change
Expand Up @@ -516,5 +516,8 @@
]
}
]
},
"2019-07-01T00:00:00+00:00": {
}

}
2 changes: 2 additions & 0 deletions spec/fixtures/alma_june_2020_raw.json
Original file line number Diff line number Diff line change
Expand Up @@ -516,5 +516,7 @@
]
}
]
},
"2020-07-01T00:00:00+00:00": {
}
}
Loading