Class: CourseScraper::Spain

Inherits:
Object
  • Object
show all
Includes:
Capybara::DSL
Defined in:
lib/course_scraper/spain.rb

Overview

Public: A scraper for all the vocational training courses in Spain.

Examples

courses = Spain.scrape
# => [#<CourseScraper::Course ...>, #<CourseScraper::Course ...>]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.scrapeObject

Public: Instantiates a new scraper and fires it to grab all the vocational training courses in Spain.

Returns the Array collection of CourseScraper::Course instances.



21
22
23
# File 'lib/course_scraper/spain.rb', line 21

def self.scrape
  new.scrape
end

Instance Method Details

#each_category(&block) ⇒ Object

Internal: Call a block for every category.

Yields the String name of the category and its String URL.

Returns nothing.



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/course_scraper/spain.rb', line 68

def each_category(&block)
  visit_category_list

  links = []

  within ".columnas-fp" do
    links = all('a')
  end

  links.each do |link|
    block.call link.text, link[:href]
  end
end

#each_course(category_url, &block) ⇒ Object

Internal: Call a block for every course in a category URL.

category_url - the String category URL

Yields the String name of the course and its Symbol type.

Returns nothing.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/course_scraper/spain.rb', line 89

def each_course(category_url, &block)
  visit category_url

  courses = []

  all('.columnas-familiafp ul:nth-of-type(1) li').each do |course|
    courses << [course.text, :medium]
  end

  all('.columnas-familiafp ul:nth-of-type(2) li').each do |course|
    courses << [course.text, :high]
  end

  courses.each do |course|
    block.call *course
  end
end

#scrapeObject

Public: Scrapes the vocational training courses in Spain.

Returns the Array collection of CourseScraper::Category instances with nested Courses.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/course_scraper/spain.rb', line 39

def scrape
  setup_capybara

  categories = []
  each_category do |name, href|
    categories << { name: name, url: href }
  end

  categories.map do |category|
    cat = Category.new(category[:name], [])
    each_course category[:url] do |name, type|
      cat.courses << Course.new(name, type)
    end
    cat
  end
end

#setup_capybaraObject

Internal: Sets the configuration for capybara to work with the Todofp website.

Returns nothing.



29
30
31
32
33
# File 'lib/course_scraper/spain.rb', line 29

def setup_capybara
  Capybara.run_server = false
  Capybara.current_driver = :webkit
  Capybara.app_host       = 'http://todofp.es'
end

#visit_category_listObject

Internal: Visits the main page where the course categories are listed.

Returns nothing.



59
60
61
# File 'lib/course_scraper/spain.rb', line 59

def visit_category_list
  visit '/todofp/formacion/que-y-como-estudiar/oferta-formativa/todos-los-estudios.html'
end