Class: Manabu::Student

Inherits:
Resource show all
Defined in:
lib/manabu/student.rb

Defined Under Namespace

Classes: ContactNotAdded, GuardianNotAdded

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#_fill, #to_hash

Constructor Details

#initialize(client, **info) ⇒ Student

Returns a new instance of Student.



15
16
17
18
19
# File 'lib/manabu/student.rb', line 15

def initialize(client, **info)
  super
  @contacts = []
  @picture = nil
end

Instance Attribute Details

#birth_dateObject

Returns the value of attribute birth_date.



11
12
13
# File 'lib/manabu/student.rb', line 11

def birth_date
  @birth_date
end

#enrollment_statusObject

Returns the value of attribute enrollment_status.



11
12
13
# File 'lib/manabu/student.rb', line 11

def enrollment_status
  @enrollment_status
end

#genderObject

Returns the value of attribute gender.



11
12
13
# File 'lib/manabu/student.rb', line 11

def gender
  @gender
end

#idObject

Returns the value of attribute id.



11
12
13
# File 'lib/manabu/student.rb', line 11

def id
  @id
end

#middle_nameObject

Returns the value of attribute middle_name.



11
12
13
# File 'lib/manabu/student.rb', line 11

def middle_name
  @middle_name
end

#middle_name_readingObject

Returns the value of attribute middle_name_reading.



11
12
13
# File 'lib/manabu/student.rb', line 11

def middle_name_reading
  @middle_name_reading
end

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/manabu/student.rb', line 11

def name
  @name
end

#name_readingObject

Returns the value of attribute name_reading.



11
12
13
# File 'lib/manabu/student.rb', line 11

def name_reading
  @name_reading
end

#surnameObject

Returns the value of attribute surname.



11
12
13
# File 'lib/manabu/student.rb', line 11

def surname
  @surname
end

#surname_readingObject

Returns the value of attribute surname_reading.



11
12
13
# File 'lib/manabu/student.rb', line 11

def surname_reading
  @surname_reading
end

Instance Method Details

#add_contact(contact_type_id, data) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/manabu/student.rb', line 62

def add_contact(contact_type_id, data)
  response = @client.post("students/#{id}/contacts",
    contact_type_id: contact_type_id,
    data: data
  )
  @contacts.push Contact.new(@client, response)
  self
# rescue StandardError
  # raise ContactNotAdded, 'Contact is not added to student'
end

#add_guardian(guardian) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/manabu/student.rb', line 54

def add_guardian(guardian)
  # NOTE: detect when guardian is already added to student
  response = @client.post("students/#{id}/student_guardians", guardian_id: guardian.id)
  self
rescue StandardError
  raise GuardianNotAdded, 'Guardian is not added to student'
end

#add_picture(path) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/manabu/student.rb', line 46

def add_picture(path)
  file = Faraday::UploadIO.new(path, FileMagic.new(FileMagic::MAGIC_MIME).file(path))

  response = @client.patch("students/#{@id}", picture: file)
  @picture = nil
  fill(response)
end

#coursesObject



80
81
82
83
84
85
# File 'lib/manabu/student.rb', line 80

def courses
  response = @client.get("students/#{id}/courses")
  response[:courses].map do |course|
    Manabu::Course.new(@client, course)
  end
end

#fill(**info) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/manabu/student.rb', line 21

def fill(**info)
  @id = info.fetch(:id, @id)
  @surname = info.fetch(:surname, @surname)
  @name = info.fetch(:name, @name)
  @name_reading = info.fetch(:name_reading, @name_reading)
  @surname_reading = info.fetch(:surname_reading, @surname_reading)
  @birth_date = info.fetch(:birth_date, @birth_date)
  @gender = info.fetch(:gender, @gender)
  @enrollment_status = Manabu::EnrollmentStatus.new(@client, info[:enrollment_status] || {})
  self
end

#guardiansObject



73
74
75
76
77
78
# File 'lib/manabu/student.rb', line 73

def guardians
  response = @client.get("students/#{id}/guardians")
  response[:guardians].map do |guardian|
    Manabu::Guardian.new(@client, guardian)
  end
end

#pictureObject



33
34
35
36
37
38
39
# File 'lib/manabu/student.rb', line 33

def picture
  return unless @id
  return @picture if @picture

  response = @client.simple_get("students/#{id}/picture")
  @picture = response.body
end

#set(**info) ⇒ Object



41
42
43
44
# File 'lib/manabu/student.rb', line 41

def set(**info)
  response = @client.patch("students/#{@id}", info)
  fill(response)
end