Class: CanvasDotfile

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

Class Method Summary collapse

Class Method Details

.create_assignment_data(response, course, type) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/github-to-canvas/canvas_dotfile.rb', line 52

def self.create_assignment_data(response, course, type)
  {
    lessons: [
      {
        id: response['id'],
        course_id: course.to_i,
        canvas_url: response['html_url'],
        type: type
      }
    ]
  }
end

.create_canvas_dotfile(filepath, canvas_data) ⇒ Object



25
26
27
# File 'lib/github-to-canvas/canvas_dotfile.rb', line 25

def self.create_canvas_dotfile(filepath, canvas_data)
  File.write("#{filepath}/.canvas", canvas_data.to_yaml)
end

.create_page_data(response, course, type) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/github-to-canvas/canvas_dotfile.rb', line 79

def self.create_page_data(response, course, type)
  {
    lessons: [
      {
        id: response['page_id'],
        course_id: course.to_i,
        canvas_url: response['html_url'],
        type: type
      }
    ]
  }
end

.exists?Boolean

Returns:

  • (Boolean)


4
5
6
# File 'lib/github-to-canvas/canvas_dotfile.rb', line 4

def self.exists?
  File.file?(".canvas")
end

.read_canvas_dataObject



29
30
31
32
33
34
35
36
# File 'lib/github-to-canvas/canvas_dotfile.rb', line 29

def self.read_canvas_data
  if File.file?(".canvas")
     YAML.load(File.read(".canvas"))
  else
    puts 'ERROR: Align functionalty requires .canvas file to be present'
    abort
  end
end

.update_assignment_data(response, course, type) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/github-to-canvas/canvas_dotfile.rb', line 38

def self.update_assignment_data(response, course, type)
  canvas_data = YAML.load(File.read(".canvas"))
  if canvas_data[:lessons].none? { |lesson| lesson[:id] == response['id'] && lesson[:course_id] == course.to_i && lesson[:canvas_url] == response['html_url']}
    lesson_data = {
      id: response['id'],
      course_id: course.to_i,
      canvas_url: response['html_url'],
      type: type
    }  
    canvas_data[:lessons] << lesson_data
  end
  canvas_data
end

.update_or_create(options, response) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/github-to-canvas/canvas_dotfile.rb', line 8

def self.update_or_create(options, response)
  if self.exists?
    if options[:type] == "assignment" || options[:type] == "discussion"
      canvas_data = self.update_assignment_data(response, options[:course_id], options[:type])
    else
      canvas_data = self.update_page_data(response, options[:course_id], options[:type])
    end
  else
    if options[:type] == "assignment" || options[:type] == "discussion"
      canvas_data = self.create_assignment_data(response, options[:course_id], options[:type])
    else
      canvas_data = self.create_page_data(response, options[:course_id], options[:type])
    end
  end
  self.create_canvas_dotfile(options[:filepath], canvas_data)
end

.update_page_data(response, course, type) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/github-to-canvas/canvas_dotfile.rb', line 65

def self.update_page_data(response, course, type)
  canvas_data = YAML.load(File.read(".canvas"))
  if canvas_data[:lessons].none? { |lesson| lesson[:id] == response['page_id'] && lesson[:course_id] == course.to_i && lesson[:canvas_url] == response['html_url']}
    lesson_data = {
      id: response['page_id'],
      course_id: course.to_i,
      canvas_url: response['html_url'],
      type: type
    }  
    canvas_data[:lessons] << lesson_data
  end
  canvas_data
end