Class: Udacity::UdacityCourse

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

Overview

Service for all Udacity API calls

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(udacity_api, data, total_course_num) ⇒ UdacityCourse

Returns a new instance of UdacityCourse.



10
11
12
13
14
# File 'lib/share_learning/udacity_course.rb', line 10

def initialize(udacity_api, data, total_course_num)
  @udacity_api = udacity_api
  @json_response = data
  @total_course_num = total_course_num
end

Instance Attribute Details

#json_responseObject (readonly)

Returns the value of attribute json_response.



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

def json_response
  @json_response
end

#total_course_numObject (readonly)

Returns the value of attribute total_course_num.



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

def total_course_num
  @total_course_num
end

Class Method Details

.findObject

# get courses by tracks (return a list of course id) def acquire_courses_by_tracks(track_name)

@json_response['tracks'].each do |track|
  next unless track['name'] == track_name
  return track['courses'].inspect
end

end



97
98
99
100
101
# File 'lib/share_learning/udacity_course.rb', line 97

def self.find
  course_data = UdacityAPI.acquire_json_response
  total_course_num = UdacityAPI.total_course_num
  new(UdacityAPI, course_data, total_course_num)
end

Instance Method Details

#acquire_all_coursesObject

get all courses information



21
22
23
24
25
26
27
28
29
30
# File 'lib/share_learning/udacity_course.rb', line 21

def acquire_all_courses
  course_array = []
  @json_response['courses'].each do |course|
    h = create_hash(course['title'], course['key'], \
                    course['summary'], course['homepage'], \
                    course['image'])
    course_array.push(h)
  end
  course_array
end

#acquire_course_by_id(id) ⇒ Object

get course information by course id



33
34
35
# File 'lib/share_learning/udacity_course.rb', line 33

def acquire_course_by_id(id)
  acquire_course_info('key', id)
end

#acquire_course_by_title(title) ⇒ Object

get course information by course title



38
39
40
# File 'lib/share_learning/udacity_course.rb', line 38

def acquire_course_by_title(title)
  acquire_course_info('title', title)
end

#acquire_course_info(key, value) ⇒ Object

get course information



43
44
45
46
47
48
49
50
51
# File 'lib/share_learning/udacity_course.rb', line 43

def acquire_course_info(key, value)
  @json_response['courses'].each do |course|
    next unless course[key] == value
    h = create_hash(course['title'], course['key'], \
                    course['summary'], course['homepage'], \
                    course['image'])
    return h
  end
end

#acquire_courses_by_keywords(keyword) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/share_learning/udacity_course.rb', line 65

def acquire_courses_by_keywords(keyword)
  course_array = []
  @json_response['courses'].each do |course|
    next unless substring?(course['title'].downcase, \
                           course['summary'].downcase, keyword.downcase)

    course_array = append_course(course_array, course)
  end
  
  course_array
end

#append_course(course_array, course) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/share_learning/udacity_course.rb', line 57

def append_course(course_array, course)
  h = create_hash(course['title'], course['key'], \
                  course['summary'], course['homepage'], \
                  course['image'])
  course_array.push(h)
  course_array
end

#create_hash(title, id, intro, link, image) ⇒ Object



16
17
18
# File 'lib/share_learning/udacity_course.rb', line 16

def create_hash(title, id, intro, link, image)
  { title: title, id: id, intro: intro, link: link, image: image }
end

#substring?(title, summary, keyword) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/share_learning/udacity_course.rb', line 53

def substring?(title, summary, keyword)
  ((title.include? keyword) || (summary.include? keyword))
end