Skip to content

Instantly share code, notes, and snippets.

@jagmitg
Created March 25, 2012 22:47
Show Gist options
  • Save jagmitg/2200536 to your computer and use it in GitHub Desktop.
Save jagmitg/2200536 to your computer and use it in GitHub Desktop.
def self.import_location(cinema_data)
location = Location.find_or_create_by_title(cinema_data[:name])
cinema_url = cinema_data[:url] + cinema_data[:postcode]
location.update_attribute(:addressing, cinema_url)
end
require 'nokogiri'
require 'open-uri'
class Importer
def self.get_film_links
doc = Nokogiri::XML(open('http://jagmit.co.uk/ruby/all-performances.xml'))
doc.xpath('//cinema').each do |cinema_data|
import_cinema(cinema_data)
end
end
private
def self.import_cinema(cinema_data)
# find cinema
cinema = Cinema.find_or_create_by_name(cinema_data[:name])
cinema.update_attributes(:external_id => cinema_data[:id],
:url => cinema_data[:address],
:postcode => cinema_data[:postcode],
:telephone => cinema_data[:phone])
# add films
cinema_data.children.first.children.each do |film_data|
import_film(cinema, film_data)
end
end
def self.import_location(cinema_data)
location = Location.find_or_create_by_title(cinema_data[:name])
cinema_url = cinema_data[:url] + cinema_data[:postcode]
location.update_attribute(:addressing, cinema_url)
end
def self.import_film(cinema, film_data)
puts "#{film_data.inspect}"
film = Film.find_or_create_by_title(film_data[:title])
film.update_attributes(:rating => film_data[:rating],
:release => film_data[:release],
:length => film_data[:length],
:poster => film_data[:poster],
:director => film_data[:director],
:synopsis => film_data[:synopsis],
:cast => film_data[:cast])
# add shows
film_data.children.first.children.each do |show_data|
import_screening(cinema, film, show_data)
end
end
def self.import_screening(cinema, film, show_data)
screening = Screening.find_by_cinema_id_and_film_id(cinema.id, film.id)
screening ||= Screening.create(:cinema => cinema, :film => film)
screening.shows.create(:day => show_data[:date],
:showed_at => show_data[:time],
:video_type => show_data[:videoType])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment