Skip to content

Commit

Permalink
implemented db storage
Browse files Browse the repository at this point in the history
  • Loading branch information
mlpinit committed Nov 16, 2012
1 parent 8025d4a commit 77e47ee
Show file tree
Hide file tree
Showing 19 changed files with 165 additions and 10 deletions.
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ source 'https://rubygems.org'
gem 'nokogiri'
gem 'mechanize'
gem 'rspec'

gem 'activerecord'
gem 'activerecord-nulldb-adapter'
# Specify your gem's dependencies in bootleg.gemspec

gemspec
1 change: 1 addition & 0 deletions bootleg.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ Gem::Specification.new do |gem|

gem.add_dependency "nokogiri"
gem.add_dependency "mechanize"
gem.add_dependency "activerecord"
gem.add_development_dependency "rspec"
end
2 changes: 1 addition & 1 deletion lib/bootleg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require "presenter"

module Bootleg
def self.presenter(zipcode)
def self.load(zipcode)
Presenter.new(zipcode)
end
end
2 changes: 1 addition & 1 deletion lib/bootleg/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Bootleg
VERSION = "0.0.3"
VERSION = "0.0.4"
end
1 change: 1 addition & 0 deletions lib/extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def initialize(page)
def extract_movies
theaters.each do |theater|
theater.extend Theater
BootlegTheater.create!(name: theater.name, href: theater.link)
theater_info = { name: theater.name, href: theater.link, movies: theater.movies}
@page_theaters << theater_info
end
Expand Down
Empty file added lib/generators/bootleg/USAGE
Empty file.
15 changes: 15 additions & 0 deletions lib/generators/bootleg/install_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'rails/generators/migration'

module Bootleg
module Generators
class InstallGenerator < Rails::Generators::Base
source_root File.expand_path('../templates', __FILE__)

def run_generators
generate "bootleg:movie"
generate "bootleg:theater"
generate "bootleg:showtime"
end
end
end
end
24 changes: 24 additions & 0 deletions lib/generators/bootleg/movie_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'rails/generators/migration'

module Bootleg
module Generators
class MovieGenerator < Rails::Generators::Base
include Rails::Generators::Migration

source_root File.expand_path('../templates', __FILE__)

def generate_movie_migration
migration_template "movie_migration.rb", "db/migrate/create_bootleg_movies.rb"
end

def generate_movie_model
copy_file "movie_model.rb", "app/models/bootleg_movie.rb"
end

def self.next_migration_number(path)
@migration_number = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i.to_s
end
end
end
end

24 changes: 24 additions & 0 deletions lib/generators/bootleg/showtime_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'rails/generators/migration'

module Bootleg
module Generators
class ShowtimeGenerator < Rails::Generators::Base
include Rails::Generators::Migration

source_root File.expand_path('../templates', __FILE__)

def generate_theater_movie_migration
migration_template "showtime_migration.rb", "db/migrate/create_bootleg_showtimes.rb"
end

def generate_theater_movie_model
copy_file "showtime_model.rb", "app/models/bootleg_showtime.rb"
end

def self.next_migration_number(path)
@migration_number = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i.to_s
end
end
end
end

11 changes: 11 additions & 0 deletions lib/generators/bootleg/templates/movie_migration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateBootlegMovies < ActiveRecord::Migration
def change
create_table :bootleg_movies do |t|
t.string :name
t.string :href
t.string :showtimes

t.timestamps
end
end
end
6 changes: 6 additions & 0 deletions lib/generators/bootleg/templates/movie_model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class BootlegMovie < ActiveRecord::Base
attr_accessible :name, :href, :showtimes

has_many :bootleg_showtimes
has_many :theaters, through: :bootleg_showtimes, source: :bootleg_theater
end
11 changes: 11 additions & 0 deletions lib/generators/bootleg/templates/showtime_migration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateBootlegShowtimes < ActiveRecord::Migration
def change
create_table :bootleg_showtimes do |t|
t.integer :bootleg_movie_id
t.integer :bootleg_theater_id
t.string :showtimes

t.timestamps
end
end
end
6 changes: 6 additions & 0 deletions lib/generators/bootleg/templates/showtime_model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class BootlegShowtime < ActiveRecord::Base
attr_accessible :bootleg_movie_id, :bootleg_theater_id, :showtimes

belongs_to :bootleg_movie
belongs_to :bootleg_theater
end
10 changes: 10 additions & 0 deletions lib/generators/bootleg/templates/theater_migration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateBootlegTheaters < ActiveRecord::Migration
def change
create_table :bootleg_theaters do |t|
t.string :name
t.string :href

t.timestamps
end
end
end
6 changes: 6 additions & 0 deletions lib/generators/bootleg/templates/theater_model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class BootlegTheater < ActiveRecord::Base
attr_accessible :name, :href

has_many :bootleg_showtimes
has_many :movies, through: :bootleg_showtimes, source: :bootleg_movie
end
24 changes: 24 additions & 0 deletions lib/generators/bootleg/theater_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'rails/generators/migration'

module Bootleg
module Generators
class TheaterGenerator < Rails::Generators::Base
include Rails::Generators::Migration

source_root File.expand_path('../templates', __FILE__)

def generate_theater_migration
migration_template "theater_migration.rb", "db/migrate/create_bootleg_theaters.rb"
end

def generate_theater_model
copy_file "theater_model.rb", "app/models/bootleg_theater.rb"
end

def self.next_migration_number(path)
@migration_number = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i.to_s
end
end
end
end

10 changes: 6 additions & 4 deletions lib/modules/movie.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
module Movie
def details
self.css('div.movietitle')
end

def name
details.css('a').text.strip
end
Expand All @@ -19,4 +15,10 @@ def showtimes
end
values
end

private

def details
self.css('div.movietitle')
end
end
16 changes: 16 additions & 0 deletions lib/modules/theater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ def link
def movies
movies = self.css('div.movie-listing.first')
values = []
theater = BootlegTheater.last
movies.each do |movie|
movie.extend Movie
movie_info = { name: movie.name, href: movie.link, showtimes: movie.showtimes }
values << movie_info
insert_movies(theater,movie)
end
values
end
Expand All @@ -24,4 +26,18 @@ def movies
def details
self.css('h3.title').css('a')
end

def insert_movies(theater, movie)
existing_movie = BootlegMovie.where(name: movie.name).first
if existing_movie
showtime = theater.bootleg_showtimes.new
showtime.bootleg_movie_id = existing_movie.id
showtime.save
else
theater.movies.create!(name: movie.name, href: movie.link)
end
showtime = BootlegShowtime.last
showtime.showtimes = movie.showtimes.to_s.gsub(/-/, '').gsub(/\n/,'').strip
showtime.save
end
end
3 changes: 0 additions & 3 deletions spec/presenter_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
require 'spec_helper'

describe Presenter do
it "should not work" do
pending 'to be continued...'
end
end

0 comments on commit 77e47ee

Please sign in to comment.