Class: GithubToCanvas

Inherits:
Object
  • Object
show all
Defined in:
lib/github-to-canvas.rb,
lib/github-to-canvas/version.rb

Constant Summary collapse

VERSION =
"0.1.4"

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ GithubToCanvas

Returns a new instance of GithubToCanvas.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/github-to-canvas.rb', line 17

def initialize(options)
  case options[:mode]
  when 'version'
    puts VERSION
  when 'query'
    CanvasInterface.get_course_info(options[:course_id], options[:id])
  when 'map'
    CanvasInterface.map_course_info(options)
  when 'csv'
    CanvasInterface.csv(options[:file_to_convert])
  when 'canvas_read'
    puts CanvasInterface.read_lesson(options[:filepath])
  when 'github_read'
    html = RepositoryConverter.remote_file_conversion(options)
    puts RepositoryConverter.adjust_converted_html(options, html)
  when 'create' # used with a local repo
    html = RepositoryConverter.local_file_conversion(options)
    name = RepositoryInterface.get_name(options[:filepath], html)
    html = RepositoryConverter.adjust_converted_html(options, html)
    response = CanvasInterface.create_lesson(options, name, html)
    RepositoryInterface.local_repo_post_submission(options, response)
    puts "Canvas lesson created. Lesson available at #{response['html_url']}"
  when 'align' # used with a local repo
    html = RepositoryConverter.local_file_conversion(options)
    name = options[:name] ? options[:name] : RepositoryInterface.get_name(options[:filepath], html)
    html = RepositoryConverter.adjust_converted_html(options, html)
    CanvasInterface.update_all_related_lessons(options, name, html)
    
  when 'github_create'
    html = RepositoryConverter.remote_file_conversion(options)
    name = options[:name] ? options[:name] : RepositoryInterface.get_name(options[:filepath], html)
    html = RepositoryConverter.adjust_converted_html(options, html)
    
    response = CanvasInterface.create_lesson(options, name, html)
    
    puts "Canvas lesson created. Lesson available at #{response['html_url']}"
  when 'github_align'
    html = RepositoryConverter.remote_file_conversion(options)
    name = options[:name] ? options[:name] : RepositoryInterface.get_name(options[:filepath], html)
    html = RepositoryConverter.adjust_converted_html(options, html)
    response = CanvasInterface.update_existing_lesson(options, name, html)
    puts "Canvas lesson updated. Lesson available at #{response['html_url']}"
  when 'build_course'
    course_yaml = YAML.load(File.read(options[:file_to_convert]))
    # Create Course
    created_course_info = CanvasInterface.create_course(course_yaml)
    puts "Course created - #{created_course_info["id"]}"

    course_yaml[:modules].each { |module_info|
      # Create each module
      created_module_info = CanvasInterface.create_module(created_course_info["id"], module_info)
      puts "Module created - #{created_module_info['name']}"
      module_info[:lessons].each { |lesson|
        # Create each lesson
        options[:type] = lesson["type"].downcase
        options[:course_id] = created_course_info["id"]
        options[:filepath] = lesson["repository"]
        
        html = RepositoryConverter.remote_file_conversion(options)
        # Add each lesson to it's module
        html = RepositoryConverter.adjust_converted_html(options, html)
        created_lesson_info = CanvasInterface.create_lesson(options, lesson["title"], html)
        lesson = lesson.merge(created_lesson_info)
        
        lesson["page_url"] = lesson["url"] if !lesson["page_url"]

        response = CanvasInterface.add_to_module(created_course_info["id"], created_module_info, lesson)
        
        puts "Lesson added to #{created_module_info['name']} - #{lesson['title']}"
        sleep(1)
      }
    }
  when 'add_to_course'
    course_yaml = YAML.load(File.read(options[:file_to_convert]))

    course_yaml[:modules].each { |module_info|
      # Create each module
      created_module_info = CanvasInterface.create_module(options[:course_id], module_info)
      puts "Module created - #{created_module_info['name']}"
      module_info[:lessons].each { |lesson|
        # Create each lesson

        options[:type] = lesson["type"].downcase
        options[:filepath] = lesson["repository"]
        html = RepositoryConverter.remote_file_conversion(options)
        # Add each lesson to it's module
        html = RepositoryConverter.adjust_converted_html(options, html)
        created_lesson_info = CanvasInterface.create_lesson(options, lesson["title"], html)
        lesson = lesson.merge(created_lesson_info)
        response = CanvasInterface.add_to_module(options[:course_id], created_module_info, lesson)
        
        puts "Lesson added to #{created_module_info['name']} - #{lesson['title']}"
        sleep(1)
      }
    }
  when 'update_course_lessons'
    course_yaml = YAML.load(File.read(options[:file_to_convert]))
    options[:course_id] = course_yaml[:id]
    course_yaml[:modules].each { |module_info|
      puts "Updating #{module_info[:name]}"
      module_info[:lessons].each { |lesson|
        if lesson["repository"] == ""
          puts "No repository found for #{lesson['title']}"
          next
        end
        options[:id] = lesson['id']
        options[:type] = lesson["type"].downcase
        options[:filepath] = lesson["repository"]
        options[:branch] = 'master'
        html = RepositoryConverter.remote_file_conversion(options)
        
        html = RepositoryConverter.adjust_converted_html(options, html)
        created_lesson_info = CanvasInterface.update_existing_lesson(options, lesson["title"], html)
        lesson = lesson.merge(created_lesson_info)
        
        
        puts "Lesson updated - #{lesson['title']}"
        sleep(1)
      }
    }
  when 'clone_course'
    course_yaml = YAML.load(File.read(options[:file_to_convert]))
    new_dir = "#{course_yaml[:name].downcase.gsub(' ','-')}"
    cmd = "mkdir #{new_dir}"
    `#{cmd}`
    course_yaml[:modules].each { |module_info|
    puts "Cloning #{module_info[:name]}"
      module_info[:lessons].each { |lesson|
      if lesson["repository"] == ""
        puts "No repository found for #{lesson['title']}"
        next
      else
        cmd = "git clone #{lesson['repository']}"
        puts cmd
        GithubInterface.cd_into_and(new_dir, cmd)
      end
      }
    }
  else
    puts VERSION
  end
end