Class: Scraper::Codecademy
- Inherits:
-
Object
- Object
- Scraper::Codecademy
- Defined in:
- lib/devscrape.rb
Instance Method Summary collapse
- #access_site ⇒ Object
- #filter_links ⇒ Object
- #get_course_completion_percentage ⇒ Object
- #get_links ⇒ Object
-
#initialize(*courses, email:, password:) ⇒ Codecademy
constructor
A new instance of Codecademy.
- #match_link_to_courses ⇒ Object
- #sign_in ⇒ Object
Constructor Details
#initialize(*courses, email:, password:) ⇒ Codecademy
Returns a new instance of Codecademy.
8 9 10 11 12 13 |
# File 'lib/devscrape.rb', line 8 def initialize(*courses, email:, password:) @agent = Mechanize.new @email = email @password = password @courses = courses end |
Instance Method Details
#access_site ⇒ Object
15 16 17 |
# File 'lib/devscrape.rb', line 15 def access_site @agent.get('https://www.codecademy.com/login') end |
#filter_links ⇒ Object
32 33 34 |
# File 'lib/devscrape.rb', line 32 def filter_links @links = @agent.page.links.select { |link| link.href[/learn/]} end |
#get_course_completion_percentage ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/devscrape.rb', line 52 def get_course_completion_percentage access_site sign_in get_links filter_links match_link_to_courses end |
#get_links ⇒ Object
28 29 30 |
# File 'lib/devscrape.rb', line 28 def get_links @agent.page.links_with(href: %r{^/learn/\w+} ) end |
#match_link_to_courses ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/devscrape.rb', line 36 def match_link_to_courses course_hash = {} @links.each do |link_text| @courses.each do |course| if (link_text.to_s.include?(course)) && (link_text.to_s =~ /\d+/) course_hash[course] = link_text.to_s.scan(/\d+/).last end end end course_hash end |
#sign_in ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/devscrape.rb', line 19 def sign_in form = @agent.page.forms.first form["user[login]"] = @email form["user[password]"] = @password form.submit end |