Class: ImpactHubEvents::Scraper

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Scraper

Returns a new instance of Scraper.



7
8
9
# File 'lib/impact_hub_events/scraper.rb', line 7

def initialize(url)
  @doc = Nokogiri::HTML(open(url))
end

Instance Attribute Details

#docObject

Returns the value of attribute doc.



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

def doc
  @doc
end

Instance Method Details

#create_eventsObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/impact_hub_events/scraper.rb', line 11

def create_events
  @doc.css("div.pbr-event-schedule div.row").each do |row|
    row.css("div.col-sm-10 div.session-item-content-wrapper").each do |event|
      # binding.pry
      ImpactHubEvents::Event.new(event.css("article div.right a").attribute("href").value)
    end
  end
  #---- From inidividual page

  #date doc.css("div.col-sm-3 ul li span").first.text
  #time doc.css("div.col-sm-3 ul li span")[1].text
  #title doc.css("div.col-sm-9 h1").text
  #location doc.css("div.col-sm-3 ul li span")[3].text
  #description doc.css("div.col-sm-9 p").first.text
end

#populate_eventsObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/impact_hub_events/scraper.rb', line 27

def populate_events
  ImpactHubEvents::Event.all.each do |event|
    event_detail = Nokogiri::HTML(open(event.url))
    event.date = event_detail.css("div.col-sm-3 ul li span").first.text unless (event_detail.css("div.col-sm-3 ul li span").first).nil?
    event.time = event_detail.css("div.col-sm-3 ul li span")[1].text unless (event_detail.css("div.col-sm-3 ul li span")[1]).nil?
    event.title = event_detail.css("div.col-sm-9 h1").text unless (event_detail.css("div.col-sm-9 h1")).nil?
    event.location = event_detail.css("div.col-sm-3 ul li span")[3].text unless (event_detail.css("div.col-sm-3 ul li span")[3]).nil?
    event.description = event_detail.css("div.col-sm-9 p").first.text unless (event_detail.css("div.col-sm-9 p").first).nil?
  end
end