lib/dramavladimir/repertoire.rb in dramavladimir-0.1.0 vs lib/dramavladimir/repertoire.rb in dramavladimir-0.1.1

- old
+ new

@@ -1,81 +1,61 @@ require 'pry' require 'active_support/all' module Dramavladimir class Repertoire - attr_accessor :doc, :url, :announce, :announce_content + attr_accessor :doc, :url, :spectacle def initialize(attributes = {}) - @url = "http://www.dramavladimir.ru/playbill" + @site = "http://www.dramavladimir.ru" + @url = "#{@site}/playbill" doc end def spectacles spectacles = [] get_afisha_links.each do |link| - announces = Dramavladimir.parse("http://www.dramavladimir.ru/#{link}") - announces.search('.mceItemTable tr').each do |announce| - @announce = announce - next unless prepare_date - spectacles.push({ - title: prepare_title, - scene: prepare_scene, - content: prepare_content, - video: prepare_video, - images: prepare_images, - schedules: prepare_date - }) + Dramavladimir.parse("#{@site}/#{link}").search('.mceItemTable tr').each do |spectacle| + @spectacle = spectacle + next unless schedules + s = { title: title, scene: scene, schedules: schedules } + s = s.merge({ content: announce.content, video: announce.video, images: announce.images, }) if announce + spectacles << s end end spectacles end - def get_afisha_links - doc.search('#main-mid table.blog a.contentpagetitle').map { |e| e.attribute('href').value } - end - - private - - def prepare_title - title = if announce.css('td a strong').last.nil? && announce.css('td strong').last.nil? - announce.css('td b').last - elsif announce.css('td a strong').last.nil? && announce.css('td b').last.nil? - announce.css('td strong').last + def title + title = if spectacle.css('td a strong').last.nil? && spectacle.css('td strong').last.nil? + spectacle.css('td b').last + elsif spectacle.css('td a strong').last.nil? && spectacle.css('td b').last.nil? + spectacle.css('td strong').last else - announce.css('td a strong').last + spectacle.css('td a strong').last end title.inner_text.mb_chars.capitalize.to_s end - def prepare_date - return false if announce.at_css('td').inner_text.empty? - t = DateTime.parse announce.at_css('td').inner_text.gsub!(/ПН|ВТ|СР|ЧТ|ПТ|СБ|ВС|\n/im, '') + def schedules + return false if spectacle.at_css('td').inner_text.empty? + t = DateTime.parse spectacle.at_css('td').inner_text.gsub!(/ПН|ВТ|СР|ЧТ|ПТ|СБ|ВС|\n/im, '') t.to_formatted_s(:db) end - def prepare_scene - announce.css('td').last.inner_text.mb_chars.downcase.to_s + def scene + spectacle.css('td').last.inner_text.mb_chars.downcase.to_s end - def prepare_content - return '' unless announce_content - announce_content.css('#main-mid p').map { |c| c.inner_text }.join(' ').gsub(/\n|\t/, '').sub(/title=.JoomlaWorks AllVideos Player.>/, '') - end + private - def prepare_video - return if announce_content.nil? || announce_content.css('.avPlayerWrapper.avVideo').nil? - announce_content.at_css('.avPlayerWrapper.avVideo iframe').attribute('src').value + def get_afisha_links + doc.search('#main-mid table.blog a.contentpagetitle').map { |e| e.attribute('href').value } end - def prepare_images - return if announce_content.nil? || announce_content.css('.rokbox-album-inner').nil? - announce_content.css('.rokbox-album-inner a').map { |image| image.attribute('href').value } - end - - def announce_content - return if announce.css('td a').first.nil? - @announce_content ||= Dramavladimir.parse(announce.css('td a').attribute('href').value) + def announce + return if spectacle.css('td a').first.nil? + Dramavladimir::Announce.new(spectacle.css('td a').attribute('href').value) end def doc @doc ||= Dramavladimir.parse(url) end