Merge pull request #36 from CactusPuppy/176895124-happening-now-json-api-endpoint

Happening now json api endpoint
This commit is contained in:
kingdish 2021-03-31 12:21:30 -07:00 committed by GitHub
commit 1977b5be49
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 71 additions and 3 deletions

View file

@ -72,6 +72,11 @@ class SchedulesController < ApplicationController
).select(&:happening_now?)
@events_schedules = [] unless @events_schedules
@current_time = Time.now.in_time_zone(@conference.timezone)
respond_to do |format|
format.html
format.json { render json: @events_schedules.as_json(root: false, include: :event) }
end
end
def app

View file

@ -33,6 +33,8 @@
class Event < ApplicationRecord
include ActiveRecord::Transitions
include RevisionCount
include FormatHelper
has_paper_trail on: [:create, :update], ignore: [:updated_at, :guid, :week], meta: { conference_id: :conference_id }
acts_as_commentable
@ -337,6 +339,10 @@ class Event < ApplicationRecord
time <=> other.time
end
def serializable_hash(options = {})
super(options).merge('rendered_abstract' => markdown(abstract))
end
private
##

View file

@ -55,16 +55,19 @@ class EventSchedule < ApplicationRecord
# True within `threshold` before and after the event.
#
def happening_now?(threshold = 30.minutes)
# TODO: Save start_time with local timezone info when making an event schedule
in_tz_start = start_time.in_time_zone(timezone)
in_tz_end = end_time.in_time_zone(timezone)
in_tz_start -= in_tz_start.utc_offset
in_tz_end -= in_tz_end.utc_offset
return false if in_tz_end < Time.now
begin_range = Time.now - threshold
end_range = Time.now + threshold
event_time_range = in_tz_start..in_tz_end
now_range = begin_range..end_range
# TODO: There's probably better logic.
event_time_range.overlaps?(now_range) && (in_tz_end > Time.now)
event_time_range.overlaps?(now_range)
end
def self.withdrawn_or_canceled_event_schedules(schedule_ids)

View file

@ -48,5 +48,4 @@ describe ConferencesController do
expect(response.response_code).to eq(200)
end
end
end

View file

@ -27,4 +27,49 @@ describe SchedulesController do
end
end
end
describe 'GET #happening_now' do
let!(:conference2) { create(:full_conference, start_date: 1.day.ago, end_date: 7.days.from_now, start_hour: 0, end_hour: 24) }
let!(:program) { conference2.program }
let!(:selected_schedule) { create(:schedule, program: program) }
let!(:scheduled_event1) do
program.update_attributes!(selected_schedule: selected_schedule)
create(:event, program: program, state: 'confirmed', abstract: '`markdown`')
end
let!(:event_schedule1) { create(:event_schedule, event: scheduled_event1, schedule: selected_schedule, start_time: Time.now.in_time_zone(conference2.timezone).strftime('%a, %d %b %Y %H:%M:%S')) }
let!(:scheduled_event2) do
program.update_attributes!(selected_schedule: selected_schedule)
create(:event, program: program, state: 'confirmed')
end
let!(:event_schedule2) { create(:event_schedule, event: scheduled_event2, schedule: selected_schedule, start_time: (Time.now.in_time_zone(conference2.timezone) + 1.hour).strftime('%a, %d %b %Y %H:%M:%S')) }
context 'html' do
before :each do
get :happening_now, params: { conference_id: conference2.short_title }
end
it 'has 200 status code' do
expect(response).to be_success
end
end
context 'json' do
before :each do
get :happening_now, format: :json, params: { conference_id: conference2.short_title }
end
it 'has 200 status code' do
expect(response).to be_success
end
it 'returns the events that are happening now' do
expect(response.body).to include(event_schedule1.to_json(include: :event))
expect(response.body).not_to include(event_schedule2.to_json(include: :event))
end
it 'contains the rendered markdown in HTML of events that are happening now' do
expect(response.body).to include('code')
end
end
end
end

View file

@ -448,4 +448,14 @@ describe Event do
end
end
end
describe '#serializable_hash' do
let(:event2) { create(:event, program: conference.program, abstract: '`markdown`') }
context 'serializes event correctly' do
it 'contains rendered markdown in HTML' do
expect(event2.serializable_hash['rendered_abstract']).to include('<code>markdown</code>')
end
end
end
end