Class: LearnOpen::LessonDownloader

Inherits:
Object
  • Object
show all
Defined in:
lib/learn_open/services/lesson_downloader.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lesson, location, environment, options = {}) ⇒ LessonDownloader

Returns a new instance of LessonDownloader.



9
10
11
12
13
14
15
16
17
18
# File 'lib/learn_open/services/lesson_downloader.rb', line 9

def initialize(lesson, location, environment, options = {})
  @lesson = lesson
  @location = location
  @environment = environment
  @client = options.fetch(:learn_web_client) { LearnOpen.learn_web_client }
  @logger = options.fetch(:logger) { LearnOpen.logger }
  @io = options.fetch(:io) { LearnOpen.default_io }
  @git_adapter = options.fetch(:git_adapter) { LearnOpen.git_adapter }
  @git_ssh_connector = options.fetch(:git_ssh_connector) { LearnOpen.git_ssh_connector }
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



3
4
5
# File 'lib/learn_open/services/lesson_downloader.rb', line 3

def client
  @client
end

#environmentObject (readonly)

Returns the value of attribute environment.



3
4
5
# File 'lib/learn_open/services/lesson_downloader.rb', line 3

def environment
  @environment
end

#git_adapterObject (readonly)

Returns the value of attribute git_adapter.



3
4
5
# File 'lib/learn_open/services/lesson_downloader.rb', line 3

def git_adapter
  @git_adapter
end

#git_ssh_connectorObject (readonly)

Returns the value of attribute git_ssh_connector.



3
4
5
# File 'lib/learn_open/services/lesson_downloader.rb', line 3

def git_ssh_connector
  @git_ssh_connector
end

#ioObject (readonly)

Returns the value of attribute io.



3
4
5
# File 'lib/learn_open/services/lesson_downloader.rb', line 3

def io
  @io
end

#lessonObject (readonly)

Returns the value of attribute lesson.



3
4
5
# File 'lib/learn_open/services/lesson_downloader.rb', line 3

def lesson
  @lesson
end

#locationObject (readonly)

Returns the value of attribute location.



3
4
5
# File 'lib/learn_open/services/lesson_downloader.rb', line 3

def location
  @location
end

#loggerObject (readonly)

Returns the value of attribute logger.



3
4
5
# File 'lib/learn_open/services/lesson_downloader.rb', line 3

def logger
  @logger
end

Class Method Details

.call(lesson, location, environment, options = {}) ⇒ Object



5
6
7
# File 'lib/learn_open/services/lesson_downloader.rb', line 5

def self.call(lesson, location, environment, options = {})
  self.new(lesson, location, environment, options).call
end

Instance Method Details

#callObject



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/learn_open/services/lesson_downloader.rb', line 20

def call
  if !repo_exists?
    if ensure_git_ssh!
      fork_repo if lesson.use_student_fork
      clone_repo
      :ok
    else
      :ssh_unauthenticated
    end
  else
    :noop
  end
end

#clone_repo(retries = 3) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/learn_open/services/lesson_downloader.rb', line 59

def clone_repo(retries = 3)
  logger.log('Cloning to your machine...')
  io.puts "Cloning lesson..."
  begin
    Timeout::timeout(15) do
      git_adapter.clone("git@#{lesson.git_server}:#{lesson.repo_path}.git", lesson.name, path: location)
    end
  rescue Git::GitExecuteError
    if retries > 0
      io.puts "There was a problem cloning this lesson. Retrying..." if retries > 1
      sleep(1)
      clone_repo(retries - 1)
    else
      io.puts "Cannot clone this lesson right now. Please try again."
      logger.log('ERROR: Error cloning. Try again.')
      exit
    end
  rescue Timeout::Error
    if retries > 0
      io.puts "There was a problem cloning this lesson. Retrying..."
      clone_repo(retries - 1)
    else
      io.puts "Cannot clone this lesson right now. Please try again."
      logger.log('ERROR: Error cloning. Try again.')
      exit
    end
  end

end

#ensure_git_ssh!Object



34
35
36
# File 'lib/learn_open/services/lesson_downloader.rb', line 34

def ensure_git_ssh!
  git_ssh_connector.call(git_server: lesson.git_server, environment: environment)
end

#fork_repo(retries = 3) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/learn_open/services/lesson_downloader.rb', line 38

def fork_repo(retries = 3)
  logger.log('Forking repository...')
  io.puts "Forking lesson..."

  begin
    Timeout::timeout(15) do
      client.fork_repo(repo_name: lesson.name)
    end
  rescue Timeout::Error
    if retries > 0
      io.puts "There was a problem forking this lesson. Retrying..."
      fork_repo(retries - 1)
    else
      io.puts "There is an issue connecting to Learn. Please try again."
      logger.log('ERROR: Error connecting to Learn')
      exit
    end
  end

end

#repo_exists?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/learn_open/services/lesson_downloader.rb', line 89

def repo_exists?
  File.exists?("#{lesson.to_path}/.git")
end