Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Completed refactoring.

  • Loading branch information...
commit e5607910f7a2c8eda73a91542686eaf703c88450 1 parent 3c4d905
@ccallebs authored
View
BIN  app/.DS_Store
Binary file not shown
View
BIN  app/models/.DS_Store
Binary file not shown
View
15 app/models/attraction.rb
@@ -2,4 +2,19 @@ class Attraction < ActiveRecord::Base
belongs_to :event
validates_uniqueness_of :artist_id
+
+ def populate(data)
+ @sequence = data['sequence']
+ @artist_name = data['artist']
+ @billing = data['billing']
+
+ @links = ""
+
+ unless data['links'].class != "string"
+ data['links'].each do |l, index|
+ @links += " | " unless index == 0
+ @links += l['url']
+ end
+ end
+ end
end
View
29 app/models/event.rb
@@ -3,4 +3,33 @@ class Event < ActiveRecord::Base
has_many :attractions
validates_uniqueness_of :ticketweb_event_id
+
+ def populate(data)
+ @ticketweb_event_id = data['eventid']
+ @ticketweb_venue_id = data['venue']['venueid']
+ @ticketweb_event_id = data['eventid']
+ @facebook_event_id = data['facebookeventid']
+ @name = data['eventname']
+ @description = data['description']
+ @url = data['eventurl']
+ @tags = data['tags']
+ @status = data['status']
+ @additional_listing_text = data['additionallistingtext']
+
+ @start_date = data['dates']['startdate']
+ @end_date = data['dates']['enddate']
+ @on_sale_date = data['dates']['onsaledate']
+ @timezone = data['dates']['timezone']
+
+ unless data['images'].nil?
+ @event_image_url_large = data['images']['large']
+ @event_image_url_small = data['images']['small']
+ end
+
+ unless data['prices'].nil?
+ @price_low = data['prices']['pricelow']
+ @price_high = data['prices']['pricehigh']
+ @price_display = data['prices']['pricedisplay']
+ end
+ end
end
View
68 app/models/ticketweb_api.rb
@@ -0,0 +1,68 @@
+module TicketWebAPI
+ require 'httparty'
+ require 'json'
+
+ def get_page(number)
+ current_url = base_url + number
+
+ response = HTTParty.get(current_url)
+
+ begin
+ json = JSON.parse(response.body)
+
+ json['events'].each do |e|
+ create_objects(e)
+ end
+ rescue
+ return false
+ end
+ end
+
+ private
+ def create_objects (event)
+ # adding or updating venue
+ new_venue = Venue.where(:ticketweb_venue_id => event['venue']['venueid']).first
+
+ if new_venue.nil?
+ new_venue = Venue.new(event)
+ end
+
+ # saving venue
+ new_venue.save
+
+ # adding or updating event
+ new_event = Event.where(:ticketweb_event_id => event['eventid'],
+ :venue_id => new_venue.id).first
+
+ if new_event.nil?
+ new_event = Event.new(event)
+ new_event.save
+ new_venue.events << new_event
+ new_venue.save
+ end
+
+ # adding attractions to event
+ event['attractionList'].each do |a|
+ new_attraction = Attraction.where(:artist_id => a['artistid']).first
+
+ if new_attraction.nil?
+ new_attraction = Attraction.new(a)
+ end
+
+ # saving attraction
+ new_attraction.save
+
+ # performing final event save
+ new_event.attractions << new_attraction
+ new_event.save
+ end
+
+ puts "Event count #{Event.all.count} || Venue count #{Venue.all.count} || Attraction count #{Attraction.all.count}"
+
+ puts "Event #{event['eventid']} saved..."
+ end
+
+ def base_url
+ "http://api.ticketweb.com/snl/EventAPI.action?key=OnTLfy5CJ7XX1mLwynRp&version=1&method=json&page="
+ end
+end
View
14 app/models/venue.rb
@@ -2,4 +2,18 @@ class Venue < ActiveRecord::Base
has_many :events
validates_uniqueness_of :ticketweb_venue_id
+
+ def populate(data)
+ @ticketweb_venue_id = data['venue']['venueid']
+ @name = data['venue']['name']
+ @url = data['venue']['venueurl']
+ @city = data['venue']['city']
+ @state = data['venue']['state']
+ @postal_code = data['venue']['postalcode']
+ @country = data['venue']['country']
+ @address = data['venue']['address']
+ @twitter_id = data['venue']['twitterid']
+ @venue_image_large = data['venue']['venueimages']['large']
+ @venue_image_small = data['venue']['venueimages']['small']
+ end
end
View
136 lib/tasks/ticketweb.rake
@@ -1,138 +1,36 @@
-require 'httparty'
-require 'json'
+require 'ticketweb_api.rb'
namespace :ticketweb do
+ include TicketWebAPI
+
desc "Either creates or updates values in the database from the ticketweb api"
+
task :load => :environment do
- @page_num = 1
- @failure_count = 0
-
- @ticketweb_url = "http://api.ticketweb.com/snl/EventAPI.action?key=OnTLfy5CJ7XX1mLwynRp&version=1&method=json&page="
+ failure_count = 0
+ page_num = 1
repeat_every(10) do
- puts "Loading ticketweb data page #{@page_num}"
- unless load_ticketweb_page(@page_num)
- if @failure_count < 10
+ puts "Loading ticketweb data page #{page_num}"
+
+ if not TicketWebAPI.get_page(page_num.to_s)
+ if failure_count < 10
puts "No more data, or something went wrong! Skipping page..."
- @failure_count += 1
+ failure_count += 1
else
puts "Too many failures... Parsing stopped."
end
end
- @page_num += 1
+
+ page_num += 1
end
end
def repeat_every(seconds)
loop do
- start_time = Time.now
- yield
- elapsed = Time.now - start_time
- sleep([seconds - elapsed, 0].max)
- end
- end
-
- def load_ticketweb_page(number)
- current_url = @ticketweb_url + number.to_s
-
- response = HTTParty.get(current_url)
-
- begin
- json = JSON.parse(response.body)
-
- json['events'].each do |e|
- create_objects(e)
- end
- rescue
- return false
- end
-end
-
-def create_objects (event)
- # adding or updating venue
- new_venue = Venue.where(:ticketweb_venue_id => event['venue']['venueid']).first
-
- if new_venue.nil?
- new_venue = Venue.new
- end
-
- new_venue.ticketweb_venue_id = event['venue']['venueid']
- new_venue.name = event['venue']['name']
- new_venue.url = event['venue']['venueurl']
- new_venue.city = event['venue']['city']
- new_venue.state = event['venue']['state']
- new_venue.postal_code = event['venue']['postalcode']
- new_venue.country = event['venue']['country']
- new_venue.address = event['venue']['address']
- new_venue.twitter_id = event['venue']['twitterid']
- new_venue.venue_image_large = event['venue']['venueimages']['large']
- new_venue.venue_image_small = event['venue']['venueimages']['small']
-
- # saving venue
- new_venue.save
-
- # adding or updating event
- new_event = Event.where(:ticketweb_event_id => event['eventid'],
- :venue_id => new_venue.id).first
-
- if new_event.nil?
- new_event = new_venue.events.create(:ticketweb_event_id => event['eventid'])
- end
-
- new_event.ticketweb_venue_id = event['venue']['venueid']
- new_event.ticketweb_event_id = event['eventid']
- new_event.facebook_event_id = event['facebookeventid']
- new_event.name = event['eventname']
- new_event.description = event['description']
- new_event.url = event['eventurl']
- new_event.tags = event['tags']
- new_event.status = event['status']
- new_event.additional_listing_text = event['additionallistingtext']
-
- new_event.start_date = event['dates']['startdate']
- new_event.end_date = event['dates']['enddate']
- new_event.on_sale_date = event['dates']['onsaledate']
- new_event.timezone = event['dates']['timezone']
-
- unless event['images'].nil?
- new_event.event_image_url_large = event['images']['large']
- new_event.event_image_url_small = event['images']['small']
- end
-
- unless event['prices'].nil?
- new_event.price_low = event['prices']['pricelow']
- new_event.price_high = event['prices']['pricehigh']
- new_event.price_display = event['prices']['pricedisplay']
- end
-
- new_event.save
-
- # adding attractions to event
- event['attractionList'].each do |a|
- new_attraction = Attraction.where(:artist_id => a['artistid']).first_or_create
- new_attraction.sequence = a['sequence']
- new_attraction.artist_name = a['artist']
- new_attraction.billing = a['billing']
-
- new_attraction.links = ""
-
- unless a['links'].class != "string"
- a['links'].each do |l, index|
- new_attraction.links += " | " unless index == 0
- new_attraction.links += l['url']
- end
- end
-
- # saving attraction
- new_attraction.save
-
- # performing final event save
- new_event.attractions << new_attraction
- new_event.save
+ start_time = Time.now
+ yield
+ elapsed = Time.now - start_time
+ sleep([seconds - elapsed, 0].max)
end
-
- puts "Event count #{Event.all.count} || Venue count #{Venue.all.count} || Attraction count #{Attraction.all.count}"
-
- puts "Event #{event['eventid']} saved..."
end
end
View
71 lib/ticketweb_api.rb
@@ -0,0 +1,71 @@
+module TicketWebAPI
+ require 'httparty'
+ require 'json'
+
+ def get_page(number)
+ current_url = base_url + number
+
+ response = HTTParty.get(current_url)
+
+ begin
+ json = JSON.parse(response.body)
+
+ json['events'].each do |e|
+ create_objects(e)
+ end
+ #rescue
+ # return false
+ end
+ end
+
+ private
+ def create_objects (event)
+ # adding or updating venue
+ new_venue = Venue.where(:ticketweb_venue_id => event['venue']['venueid']).first
+
+ if new_venue.nil?
+ new_venue = Venue.new
+ new_venue.populate(event)
+ end
+
+ # saving venue
+ new_venue.save
+
+ # adding or updating event
+ new_event = Event.where(:ticketweb_event_id => event['eventid'],
+ :venue_id => new_venue.id).first
+
+ if new_event.nil?
+ new_event = Event.new
+ new_event.populate(event)
+ new_event.save
+ new_venue.events << new_event
+ new_venue.save
+ end
+
+ # adding attractions to event
+ event['attractionList'].each do |a|
+ new_attraction = Attraction.where(:artist_id => a['artistid']).first
+
+ if new_attraction.nil?
+ new_attraction = Attraction.new
+ new_attraction.populate(a)
+ end
+
+ # saving attraction
+ new_attraction.save
+
+ # performing final event save
+ new_event.attractions << new_attraction
+ new_event.save
+ end
+
+ puts "Event count #{Event.all.count} || Venue count #{Venue.all.count} || Attraction count #{Attraction.all.count}"
+
+ puts "Event #{event['eventid']} saved..."
+ end
+
+ def base_url
+ "http://api.ticketweb.com/snl/EventAPI.action?key=OnTLfy5CJ7XX1mLwynRp&version=1&method=json&page="
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.