Class: Desmos::Whiteboard

Inherits:
Object
  • Object
show all
Includes:
RequestSupport
Defined in:
lib/desmos/whiteboard.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RequestSupport

#build_http, #build_uri, #make_request, #parse_response, #request!

Methods included from DebugMode

#debug

Constructor Details

#initialize(options = {}) ⇒ Whiteboard

Returns a new instance of Whiteboard.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/desmos/whiteboard.rb', line 14

def initialize(options = {})
  @hash       = options[:hash]  # optional: The API will generate this if not provided. Must be unique if provided.
  @title      = options[:title] # optional

  # optional: Should be a Desmos::Tutor object
  if options[:tutor] && !(Tutor === options[:tutor])
    raise ArgumentError, ':tutor option must be either of type Desmos::Tutor or NilClass'
  end
  @tutor = options[:tutor]

  # optional: Should be an Array of Desmos::Student objects
  if options[:students] && (!options[:students].respond_to?(:all?) || !options[:students].all? { |option| Desmos::Student === option })
    raise ArgumentError, ':students option must be either an Array containing object of type Desmos::Student or NilClass'
  end
  @students = options[:students] || []
end

Instance Attribute Details

#hashObject

Returns the value of attribute hash.



4
5
6
# File 'lib/desmos/whiteboard.rb', line 4

def hash
  @hash
end

#studentsObject

Returns the value of attribute students.



4
5
6
# File 'lib/desmos/whiteboard.rb', line 4

def students
  @students
end

#titleObject

Returns the value of attribute title.



4
5
6
# File 'lib/desmos/whiteboard.rb', line 4

def title
  @title
end

#tutorObject

Returns the value of attribute tutor.



4
5
6
# File 'lib/desmos/whiteboard.rb', line 4

def tutor
  @tutor
end

Class Method Details

.create(options = {}) ⇒ Object



10
11
12
# File 'lib/desmos/whiteboard.rb', line 10

def self.create(options = {})
  new(options).save
end

.find(hash) ⇒ Object



6
7
8
# File 'lib/desmos/whiteboard.rb', line 6

def self.find(hash)
  new(:hash => hash).get
end

Instance Method Details

#add_student(student) ⇒ Object



57
58
59
# File 'lib/desmos/whiteboard.rb', line 57

def add_student(student)
  parsed_response = request!(:whiteboard, :add_student, student.request_options.reject { |k,v| k == :user_type }.merge(:whiteboard_hash => hash))
end

#add_studentsObject



49
50
51
52
53
54
55
# File 'lib/desmos/whiteboard.rb', line 49

def add_students
  unless students.empty?
    students.each do |student|
      add_student(student)
    end
  end
end

#build_from_hash(options) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/desmos/whiteboard.rb', line 87

def build_from_hash(options)
  self.hash     = options[:hash]
  self.title    = options[:title]
  self.tutor    = Tutor.build_from_hash(options[:tutor])
  self.students = options[:students].collect { |student_attributes| Student.new(student_attributes) }
  self
end

#getObject



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/desmos/whiteboard.rb', line 61

def get
  parsed_response = request!(:whiteboard, :read, :whiteboard_hash => hash)
  if parsed_response[:success] == 'false'
    case parsed_response[:error_message]
    when 'Whiteboard not found'
      raise WhiteboardNotFound, "Whiteboard with HASH=#{hash} could not be found"
    else
      raise RequestError, parsed_response[:error_message]
    end
  else
    build_from_hash(parsed_response)
  end
end

#request_optionsObject



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/desmos/whiteboard.rb', line 75

def request_options
  options = {}
  options.merge!(:hash  => hash)  if hash
  options.merge!(:title => title) if title
  if tutor
    options.merge!(:tutor_name => tutor.name) if tutor.name
    options.merge!(:tutor_id   => tutor.id)   if tutor.id
    options.merge!(:tutor_hash => tutor.hash) if tutor.hash
  end
  options
end

#saveObject



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/desmos/whiteboard.rb', line 31

def save
  parsed_response = request!(:whiteboard, :create, request_options)
  if parsed_response.fetch(:success) == 'false'
    raise RequestError, parsed_response[:error_message] unless parsed_response[:error_message] == 'whiteboard already exists'
  else
    self.hash = parsed_response.fetch(:hash)
  end
  set_tutor
  add_students
  self
end

#set_tutorObject



43
44
45
46
47
# File 'lib/desmos/whiteboard.rb', line 43

def set_tutor
  if tutor
    parsed_response = request!(:whiteboard, :set_tutor, tutor.request_options.reject { |k,v| k == :user_type }.merge(:whiteboard_hash => hash))
  end
end