Class: LearnOpen::Adapters::LearnWebAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/learn_open/adapters/learn_web_adapter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ LearnWebAdapter

Returns a new instance of LearnWebAdapter.



6
7
8
9
# File 'lib/learn_open/adapters/learn_web_adapter.rb', line 6

def initialize(options = {})
  @client = options.fetch(:learn_web_client) {LearnOpen.learn_web_client}
  @io = options.fetch(:io) {LearnOpen.default_io}
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



4
5
6
# File 'lib/learn_open/adapters/learn_web_adapter.rb', line 4

def client
  @client
end

#ioObject (readonly)

Returns the value of attribute io.



4
5
6
# File 'lib/learn_open/adapters/learn_web_adapter.rb', line 4

def io
  @io
end

Instance Method Details

#current_lessonObject



44
45
46
# File 'lib/learn_open/adapters/learn_web_adapter.rb', line 44

def current_lesson
  @current_lesson ||= client.current_lesson
end

#fetch_lesson_data(target_lesson: false, fetch_next_lesson: false) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/learn_open/adapters/learn_web_adapter.rb', line 11

def fetch_lesson_data(target_lesson: false, fetch_next_lesson: false)
  if opening_current_lesson?(target_lesson, fetch_next_lesson)
    load_current_lesson
    {
        lesson: current_lesson,
        id: current_lesson.id,
        later_lesson: false
    }
  elsif opening_next_lesson?(target_lesson, fetch_next_lesson)
    load_next_lesson
    {
        lesson: next_lesson,
        id: next_lesson.id,
        later_lesson: false
    }
  else
    lesson = get_lesson(target_lesson)
    {
        lesson: lesson,
        id: lesson.lesson_id,
        later_lesson: lesson.later_lesson
    }
  end
end

#get_lesson(target_lesson, retries = 3) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/learn_open/adapters/learn_web_adapter.rb', line 86

def get_lesson(target_lesson, retries = 3)
  @correct_lesson ||= begin
    Timeout::timeout(15) do
      client.validate_repo_slug(repo_slug: target_lesson)
    end
  rescue Timeout::Error
    if retries > 0
      io.puts "There was a problem connecting to Learn. Retrying..."
      get_lesson(target_lesson, retries - 1)
    else
      io.puts "Cannot connect to Learn right now. Please try again."
      logger.log('ERROR: Error connecting to Learn')
      exit
    end
  end
end

#load_current_lesson(retries = 3) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/learn_open/adapters/learn_web_adapter.rb', line 52

def load_current_lesson(retries = 3)
  begin
    Timeout::timeout(15) do
      current_lesson
    end
  rescue Timeout::Error
    if retries > 0
      io.puts "There was a problem getting your lesson from Learn. Retrying..."
      load_current_lesson(retries - 1)
    else
      io.puts "There seems to be a problem connecting to Learn. Please try again."
      logger.log('ERROR: Error connecting to Learn')
      exit
    end
  end
end

#load_next_lesson(retries = 3) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/learn_open/adapters/learn_web_adapter.rb', line 69

def load_next_lesson(retries = 3)
  begin
    Timeout::timeout(15) do
      next_lesson
    end
  rescue Timeout::Error
    if retries > 0
      io.puts "There was a problem getting your next lesson from Learn. Retrying..."
      load_next_lesson(retries - 1)
    else
      io.puts "There seems to be a problem connecting to Learn. Please try again."
      logger.log('ERROR: Error connecting to Learn')
      exit
    end
  end
end

#next_lessonObject



48
49
50
# File 'lib/learn_open/adapters/learn_web_adapter.rb', line 48

def next_lesson
  @next_lesson ||= client.next_lesson
end

#opening_current_lesson?(target_lesson, fetch_next_lesson) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/learn_open/adapters/learn_web_adapter.rb', line 36

def opening_current_lesson?(target_lesson, fetch_next_lesson)
  !target_lesson && !fetch_next_lesson
end

#opening_next_lesson?(target_lesson, fetch_next_lesson) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/learn_open/adapters/learn_web_adapter.rb', line 40

def opening_next_lesson?(target_lesson, fetch_next_lesson)
  !target_lesson && fetch_next_lesson
end