Class: NYCLI::Scraper

Inherits:
Object
  • Object
show all
Defined in:
lib/nycli/scraper.rb

Constant Summary collapse

@@page =
1

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url = "https://www.nyc.com/events/?int4=5") ⇒ Scraper

Returns a new instance of Scraper.



9
10
11
# File 'lib/nycli/scraper.rb', line 9

def initialize(url = "https://www.nyc.com/events/?int4=5")
  @url = url
end

Instance Attribute Details

#urlObject

Returns the value of attribute url.



5
6
7
# File 'lib/nycli/scraper.rb', line 5

def url
  @url
end

Class Method Details

.datesObject



41
42
43
44
# File 'lib/nycli/scraper.rb', line 41

def self.dates
  counter = 1
  dates = self.all.collect { |event| event.date }
end

.moreObject

Scraping next events list:



48
49
50
51
52
# File 'lib/nycli/scraper.rb', line 48

def self.more
  @@page += 1
  NYCLI::Scraper.new("https://www.nyc.com/events/?int4=5&p=" + "#{self.page}").show_events
  NYCLI::CLI.more_names
end

.pageObject



37
38
39
# File 'lib/nycli/scraper.rb', line 37

def self.page
  @@page
end

Instance Method Details

#get_eventsObject

Data structuring helper:



19
20
21
# File 'lib/nycli/scraper.rb', line 19

def get_events
  self.get_page.css(".eventrecords li[itemtype='http://schema.org/Event']")
end

#get_pageObject



13
14
15
# File 'lib/nycli/scraper.rb', line 13

def get_page
  Nokogiri::HTML(URI.open(self.url))
end

#show_eventsObject

Scraping elements:



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/nycli/scraper.rb', line 25

def show_events
  self.get_events.each do |item|
    event = NYCLI::CLI.new
    event.name = item.css("h3").text.strip
    event.date = item.css(".desktop-date").text.gsub("\n                    ", " ").strip
    event.time = item.css(".datevenue strong.nyc-mobile-hidden").text
    event.description = item.css("p[itemprop='description']").text.gsub("read more", "").strip
    event.venue = item.css("span[itemprop='name']").text
    event.link = "https://www.nyc.com" + item.css("a.venuelink").attr("href").text if item.css("a.venuelink").attr("href")
  end
end