Skip to content

Commit

Permalink
Merge pull request #182 from aspc/feature/import-engage-events
Browse files Browse the repository at this point in the history
Implement engage_events_service.rb to pull events from Engage API
  • Loading branch information
thebradbain authored Dec 1, 2019
2 parents f44b5d5 + b788546 commit a2d2756
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 6 deletions.
5 changes: 4 additions & 1 deletion app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ class EventsController < ApplicationController
# GET /events.json
def index
# Only display approved events
@events = Event.where(:status => :approved)
@events = Event.where(:status => :approved)

# Pull Engage events from API and save to database
EngageEventsService.new().get_events()
end

# GET /events/1
Expand Down
3 changes: 1 addition & 2 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ class Event < ApplicationRecord
enum status: [ :pending, :approved, :rejected]
enum college_affiliation: [:all_colleges, :pomona, :claremont_mckenna, :harvey_mudd, :scripps, :pitzer]

belongs_to :submitted_by, class_name: "User", foreign_key: "submitted_by_user_fk"
belongs_to :submitted_by, class_name: "User", foreign_key: "submitted_by_user_fk", optional: true

validates :name, presence: true
validates :start, presence: true
validates :end, presence: true
validates :location, presence: true
validates :description, presence: true
validates :submitted_by_user_fk, presence: true
validates :college_affiliation, presence: true

def approve!
Expand Down
61 changes: 61 additions & 0 deletions app/services/engage_events_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# app/services/engage_events_service.rb

class EngageEventsService
def initialize()
end

def get_events()
headers = {
"Accept" => "application/json",
"X-Engage-Api-Key" => Rails.application.credentials.engage_api[:production]
}

start_time = Time.now.to_i * 1000 # in milliseconds
end_time = start_time + 30*24*3600*1000 # +30 days

query = {
"startDate" => start_time,
"endDate" => end_time
}

response = HTTParty.get(Rails.application.credentials.engage_api[:url], :headers => headers, :query => query)

engage_events = response["items"] # @events is a list of hashes, each of which is an event

# save to db on the fly, then let events_controller pull from db
engage_events.each do |event|
# if event is marked for organisation only, then skip
if event["typeName"] == "Organization Only"
next
end

# if event already exists in database (checking with eventUrl), then skip
if Event.exists?(details_url: event["eventUrl"])
next
end

# if an event starts 12pm PST, the controller expects an input of 12pm UTC (rather than 8pm UTC)
# need to manually offset time
offset = Time.now.in_time_zone("America/Los_Angeles").utc_offset
start_time = Time.at(event["startDateTime"].to_i / 1000) + offset
end_time = Time.at(event["endDateTime"].to_i / 1000) + offset

# remove HTML tags from description
description = ActionView::Base.full_sanitizer.sanitize(event["description"])

@event = Event.new(
:name => event["eventName"],
:location => event["locationName"].presence || "N/A", # return "N/A" if locationName is blank
:description => description,
:host => event["organizationName"],
:details_url => event["eventUrl"],
:start => start_time,
:end => end_time
)

@event.approve!

res = @event.save!
end
end
end
2 changes: 1 addition & 1 deletion config/credentials.yml.enc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Be2ogp7UV42tavtmO5pmn/cVJ/5TbiSbbyzsrH9SZBE6eRxsAvTpGtKsT7mVi4IPBxkcdeiOcGM55z3wIiUE0ymNTG7MgFf0duIaiTS6yJNXNdpjIRpKNipLW2CGFLWFOZzJrRRziVfCHGeX51Mvm2mkxHOdn1sINiHg3dgNDvvQedUKxomPIRmqHNJFx9wvYnb0W7CoxzlVjpOJW8qKqIeR/n+KsXVWwE5ImLmCRQelrs5AG1Bg7+1EmQENlacaIuwdnDbnq4HlxFiXhtUzT4bbnNfKmWaTogcL7wfIx3Ubdf+5xREj1pLBv1xeWfNPziHq2Zfj7BnAx8ltbLUe/oa4XRZm6z6XD20A/22l0BdfNpFIGjx8Qi9fHuxgVgzsC/8FbhUad2m77CxlsxvaWaxWe3hbdIKOnxF8x4Xpssn5GeAzKO4jfjY59mwgc55wzwEfQ1RJpyhnc5QJyYbXTuqSood06jCeCocg9qfYYWtw7qgnpk73Zi7PYKaAn+rwwkO19cru79aKDPcfDaPc6Vhbx5WmuYQbDb10qA5ysexQvqH5vRNIM0ObfdJUfLV3/0uzvivklCEP64jxastttpY35a0fg/qwYe7Whwq5HrUpLr5zq/eqz3m/Yz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mRClQTsJRdj5pBxFLO3gDX3lv8V4efK1y781z9hT9ksRed+u31TkE0vcRVJA41PCjNdkhHkYcYRlfezqs9Eeld5ln8nqZeIebxIQyYOalP6Tn3ih3fU9/X2RTHUGTtqWlzbEfl82NpBervn413Vtwe3pNtfpFQFqp1PH9e+FW6NwQvR/crsOYzUtbOXiauiaecHAxcifDS/5ceQ0yc+JVNss0lmeILct4+CVHV3agu/EC0/z7RE6BN4x8sPu+NjZSW37MaAlZxVaQ38uJA8L4weN4IYLGtIxGRbQK13BiSLDV5yUkxn2rhdijAgkHMuvDpjcXt+OVVYZiWcjkxyrBksM7q39oS7Av0TTZGbAhLTETi6IEoRT8p5+LIARGiY7Vc7h/hAxRAAQnWsVvGmWdwsr2GOui7O0pPdR44bwdk5MaHPrpKaJE1jCuE6qsVJSx8nD8dzPUx5yft7jZ2/Wjbpxq8RCsmdK7aFvXVsnSk7y/KNG8E6U5hiRM4/jRWUVLyQAswQagXIgYokCK1IprmKFu7fHXq/ElSDl8+rLEtJWeigFEoIZZ8bA8i9SB2rHqEFQNTNOk4zpoHYzixbTUMbBe6XoEwOetej+aG73Ng40hFGZISbnSf7Tr8Kk3l3n+IucpL4fDKBTStfv2L0UymNHZXiBdEfhkXY7GrQHcJtRi23CMtMNvDlGpJyE/hicZE1zLPjOG4V5gnYFf6jsryAR7XLrXsXNPdHdBJkRg9AxT8BYoJrnshPatFOuat8P6BddP3/1OzalOWGd1Sx0=--FSUcmECmmRsCh4rg--61ONmcZVU8+5FGvHOouL8w==
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ChangeSubmittedByUserFkColumnOnUsers < ActiveRecord::Migration[5.2]
def change
change_column_null :events, :submitted_by_user_fk, true
end
end
4 changes: 2 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2019_09_12_005931) do
ActiveRecord::Schema.define(version: 2019_12_01_230018) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -172,7 +172,7 @@
t.text "host"
t.text "details_url"
t.integer "status", default: 0, null: false
t.integer "submitted_by_user_fk", null: false
t.integer "submitted_by_user_fk"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "college_affiliation", default: 0, null: false
Expand Down

0 comments on commit a2d2756

Please sign in to comment.