Class: Scraper::Codecademy

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

Instance Method Summary collapse

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_siteObject



15
16
17
# File 'lib/devscrape.rb', line 15

def access_site
  @agent.get('https://www.codecademy.com/login')
end


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_percentageObject



52
53
54
55
56
57
58
# File 'lib/devscrape.rb', line 52

def get_course_completion_percentage
  access_site
  
  get_links
  filter_links
  match_link_to_courses
end


28
29
30
# File 'lib/devscrape.rb', line 28

def get_links
  @agent.page.links_with(href: %r{^/learn/\w+} )
end


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_inObject



19
20
21
22
23
24
25
26
# File 'lib/devscrape.rb', line 19

def 
  form = @agent.page.forms.first

  form["user[login]"] = @email
  form["user[password]"] = @password

  form.submit
end