Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge Eventbrite support #514

Merged
merged 5 commits into from Feb 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -27,6 +27,7 @@ gem 'webpacker'
gem 'httparty'
gem 'icalendar'
gem 'icalendar-recurrence'
gem 'eventbrite_sdk'

# Uploads
gem 'carrierwave'
Expand Down
17 changes: 17 additions & 0 deletions Gemfile.lock
Expand Up @@ -145,6 +145,8 @@ GEM
actionmailer (>= 5.0)
devise (>= 4.6)
docile (1.3.2)
domain_name (0.5.20190701)
unf (>= 0.0.5, < 1.0.0)
dotenv (2.7.5)
dotenv-rails (2.7.5)
dotenv (= 2.7.5)
Expand Down Expand Up @@ -177,6 +179,8 @@ GEM
activesupport (>= 3.2)
equalizer (0.0.11)
erubi (1.9.0)
eventbrite_sdk (3.4.0)
rest-client (~> 2.0)
eventmachine (1.2.7)
execjs (2.7.0)
factory_bot (5.1.1)
Expand Down Expand Up @@ -233,6 +237,9 @@ GEM
minitest (>= 3.0)
hashdiff (1.0.0)
hashie (3.6.0)
http-accept (1.7.0)
http-cookie (1.0.3)
domain_name (~> 0.5)
http_parser.rb (0.6.0)
httparty (0.17.3)
mime-types (~> 3.0)
Expand Down Expand Up @@ -313,6 +320,7 @@ GEM
mustermann-grape (1.0.1)
mustermann (>= 1.0.0)
nenv (0.3.0)
netrc (0.11.0)
nio4r (2.5.2)
nokogiri (1.10.7)
mini_portile2 (~> 2.4.0)
Expand Down Expand Up @@ -408,6 +416,11 @@ GEM
responders (3.0.0)
actionpack (>= 5.0)
railties (>= 5.0)
rest-client (2.1.0)
http-accept (>= 1.7.0, < 2.0)
http-cookie (>= 1.0.2, < 2.0)
mime-types (>= 1.16, < 4.0)
netrc (~> 0.8)
rollbar (2.23.2)
rubocop (0.79.0)
jaro_winkler (~> 1.5.1)
Expand Down Expand Up @@ -478,6 +491,9 @@ GEM
thread_safe (~> 0.1)
uglifier (4.2.0)
execjs (>= 0.3.0, < 3)
unf (0.1.4)
unf_ext
unf_ext (0.0.7.6)
unicode-display_width (1.6.1)
vcr (5.0.0)
virtus (1.0.5)
Expand Down Expand Up @@ -530,6 +546,7 @@ DEPENDENCIES
devise_invitable
dotenv-rails
enumerize
eventbrite_sdk
factory_bot_rails
font-awesome-rails
friendly_id (~> 5.3.0)
Expand Down
2 changes: 1 addition & 1 deletion app/models/calendar_parser.rb
Expand Up @@ -2,7 +2,7 @@ class CalendarParser
class UnsupportedFeed < StandardError; end
class InaccessibleFeed < StandardError; end

PARSERS = [Parsers::ManchesterUni, Parsers::Zarts, Parsers::Facebook, Parsers::Ics].freeze
PARSERS = [Parsers::ManchesterUni, Parsers::Zarts, Parsers::Facebook, Parsers::Ics, Parsers::Eventbrite].freeze

def initialize(calendar, options={})
@calendar = calendar
Expand Down
3 changes: 2 additions & 1 deletion app/models/events/base.rb
Expand Up @@ -15,6 +15,7 @@ def rrule
end

def sanitize_invalid_char(input)
input = I18n.transliterate(input)
input.encode('utf-8', :invalid => :replace, :undef => :replace, :replace => '')
end

Expand Down Expand Up @@ -85,7 +86,7 @@ def ip_class
end

def private?
ip_class&.casecmp('private')&.zero? || (@event.description&.include?('#placecal-ignore'))
ip_class&.casecmp('private')&.zero? || (description&.include?('#placecal-ignore'))
end
end
end
62 changes: 62 additions & 0 deletions app/models/events/eventbrite_event.rb
@@ -0,0 +1,62 @@
# frozen_string_literal: true

module Events
class EventbriteEvent < Base
def initialize(event)
@event = event
end

def uid
@event['id']
end

def summary
@event['name']['text']
end

def description
@event['description']['text']
end

def place
@place ||= @event['venue']
end

def location
return if place.blank?
address = place['address']

if address.present?
[ place['name'],
address['address_1'],
address['address_2'],
address['city'],
address['region'],
address['postal_code']
].reject(&:blank?).join(', ')
else
place['name']
end
end

def dtstart
DateTime.parse(@event['start']['local'])
rescue StandardError
nil
end

def dtend
DateTime.parse(@event['end']['local'])
rescue StandardError
nil
end

def occurrences_between(*)
#TODO: Expand when multi-day events supported
@occurrences = []
@occurrences << Dates.new(dtstart, dtend)
@occurrences
end

end
end
37 changes: 37 additions & 0 deletions app/models/parsers/eventbrite.rb
@@ -0,0 +1,37 @@
# frozen_string_literal: true

# In order for a parser to be recognized, it must be added
# to the PARSERS constant list in app/models/calendar_parser.rb.
# Parent parser classes should not be added.

module Parsers
class Eventbrite < Base
def self.whitelist_pattern
/https:\/\/www.eventbrite\.(com|co.uk)\/o\/[A-Za-z0-9-]+/
end

def organizer_id
path = URI.parse(@url).path
path.split('/').last.split('-').last
end

def download_calendar
EventbriteSDK.token = ENV['EVENTBRITE_TOKEN']

@events = []
results = EventbriteSDK::Organizer.retrieve(id: organizer_id).events.with_expansion(:venue).page(1)

loop do
@events += results
results = results.next_page
break if results.blank?
end

@events
end

def import_events_from(data)
data.map { |d| Events::EventbriteEvent.new(d) }
end
end
end
4 changes: 4 additions & 0 deletions app/models/partner.rb
Expand Up @@ -119,6 +119,10 @@ def to_s
# errors.add(:_, "Select at least one Tag") if tag_ids.blank?
# end

def should_generate_new_friendly_id?
slug.blank?
end

def permalink
"https://placecal.org/partners/#{id}"
end
Expand Down
1 change: 1 addition & 0 deletions app/views/admin/sites/_form.html.erb
@@ -1,4 +1,5 @@
<%= simple_form_for [:admin, @site] do |f| %>
<%= render_component "error", object: @site %>
<%= f.input :name %>
<%= f.input :place_name %>
<%= f.input :tagline %>
Expand Down
1,938 changes: 1,938 additions & 0 deletions test/fixtures/vcr_cassettes/Eventbrite_calendar.yml

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions test/models/calendar_parser_test.rb
Expand Up @@ -118,6 +118,22 @@ class CalendarParserTest < ActiveSupport::TestCase
end
end

test 'imports eventbrite calendars' do
calendar = create(:calendar, name: 'Eventbrite - BAVS',
source: 'https://www.eventbrite.co.uk/o/berwickshire-association-for-voluntary-service-15751503063')

VCR.use_cassette('Eventbrite calendar') do
output = CalendarParser.new(calendar).parse
events = output.events
first_event = events.first
last_event = events.last

assert_equal 41, events.count
assert_equal 'BAVS Forum: Supporting Positive Pathways – Action Research Event', first_event.summary
assert_equal 'Vision 4 Eyemouth', last_event.summary
end
end

test 'does not import if checksum is the same' do
calendar = create(:calendar, name: 'Z-Arts',
last_checksum: 'd1a94a9869af91d0548a1faf0ded91d7',
Expand Down