Class: Coursera::CourseraCourses

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

Overview

This class is for Coursera courses

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(total_course_num, courses) ⇒ CourseraCourses

Returns a new instance of CourseraCourses.



8
9
10
11
# File 'lib/share_learning/coursera_courses.rb', line 8

def initialize(total_course_num, courses)
  @total_course_num = total_course_num
  @courses = courses
end

Instance Attribute Details

#coursesObject (readonly)

Returns the value of attribute courses.



6
7
8
# File 'lib/share_learning/coursera_courses.rb', line 6

def courses
  @courses
end

#total_course_numObject (readonly)

Returns the value of attribute total_course_num.



6
7
8
# File 'lib/share_learning/coursera_courses.rb', line 6

def total_course_num
  @total_course_num
end

Class Method Details

.findObject



13
14
15
16
17
# File 'lib/share_learning/coursera_courses.rb', line 13

def self.find
  total_course_num = CourseraApi.total_course_num
  courses = CourseraApi.courses
  new(total_course_num, courses)
end

Instance Method Details

#course_matched?(course, method, keyword) ⇒ Boolean

Returns:

  • (Boolean)


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

def course_matched?(course, method, keyword)
  available_methods = [:course_name, :description]
  result = false
  available_methods.each do |m|
    next unless method == :all || m == method
    result = true if course[m].downcase.include?(keyword)
  end
  result
end


38
39
40
41
42
43
# File 'lib/share_learning/coursera_courses.rb', line 38

def print_all_courses
  puts "#{@total_course_num} courses:"
  @courses.size.times do |i|
    print_course(@courses[i], i)
  end
end

#search_courses(method, keyword) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/share_learning/coursera_courses.rb', line 29

def search_courses(method, keyword)
  matched_courses = []
  @courses.size.times do |i|
    course = courses[i]
    matched_courses.push(course) if course_matched?(course, method, keyword)
  end
  matched_courses
end