Class: Scraper

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

Class Method Summary collapse

Class Method Details

.scrape_page(clinic_url = "http://callen-lorde.org/meet-our-providers/") ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/Scraper.rb', line 10

def self.scrape_page(clinic_url = "http://callen-lorde.org/meet-our-providers/")
  html = open(clinic_url)
  doc = Nokogiri::HTML(html)

  #name_qualifications_title = doc.css(".middleColumn_three").css('h3').first.text.split(",")
  #name = name_qualifications_title[0]
  #qualification = name_qualifications_title[1]
  #title = name_qualifications_title[2]

  #team_specialties_languages = doc.css(".middleColumn_three").css('ul').first.text
  #t_s_l_array = team_specialties_languages.split("\n").reject { |n| n == "" }

  #cleaned_t_s_l_array = t_s_l_array.map do |ele|
  #  ele.gsub(/\w+[:]/, "").strip
  #end

  #team = cleaned_t_s_l_array[0]
  #specialties = cleaned_t_s_l_array[1]
  #languages = cleaned_t_s_l_array[2]


  attr_hash = Hash.new
  array_of_providers = Array.new


  #getting name, qualification and title attributes for a secific provider
  doc.css(".middleColumn_three").css('h3').each do |provider|
    new_hash = Hash.new
    name_qualification_title = provider.text.split(",")
    new_hash[:name] = name_qualification_title[0].gsub(/\w+[MD]/, "").strip
    new_hash[:qualification] = name_qualification_title[1].strip
    if name_qualification_title[2] != nil
      new_hash[:title] = name_qualification_title[2].strip
    end
    array_of_providers << new_hash
  end

  #getting the team, specialties and languages attributes for a specific provider
  i = 0
  doc.css(".middleColumn_three").css('ul').each do |provider|
    team_specialties_languanges = provider.text.split("\n").reject { |n| n == "" }

    #remove words that end with ":"
    team_specialties_languanges = team_specialties_languanges.map do |ele|
      ele.gsub(/\w+[:]/, "").strip
    end

    #accointing for use case where attribute team is missing
    if team_specialties_languanges.size == 2
      array_of_providers[i][:specialties] = team_specialties_languanges[0].strip
      array_of_providers[i][:languages] = team_specialties_languanges[1]

    else
      array_of_providers[i][:team] = team_specialties_languanges[0]
      array_of_providers[i][:specialties] = team_specialties_languanges[1]
      array_of_providers[i][:languages] = team_specialties_languanges[2]

    end

    if i < 46
      i += 1
    end

  end

  #creatign instances from our array of hashes
  array_of_providers.each do |provider|
    Providers.new(provider)

  end

end