diff --git a/lib/manabu/contact.rb b/lib/manabu/contact.rb index f617824..cabbc3d 100644 --- a/lib/manabu/contact.rb +++ b/lib/manabu/contact.rb @@ -1,14 +1,15 @@ require_relative './resource' +require_relative './contact_type' module Manabu class Contact < Resource - attr_accessor :id, :data, :contact_type_id + attr_accessor :id, :data, :contact_type def fill(**info) @id = info.fetch(:id, @id) @data = info.fetch(:data, @data) - @contact_type_id = info.fetch(:contact_type_id, @contact_type_id) - + @contact_type = + Manabu::ContactType.new(@client, info.fetch(:contact_type, {})) self end diff --git a/lib/manabu/guardian.rb b/lib/manabu/guardian.rb index a3112b4..9c73494 100644 --- a/lib/manabu/guardian.rb +++ b/lib/manabu/guardian.rb @@ -1,17 +1,16 @@ require_relative './resource' +require_relative './person' module Manabu - class Guardian < Resource + class Guardian < Person + PLURAL= 'guardians'.freeze + SINGULAR = 'guardian'.freeze + class ContactNotAdded < StandardError; end class AddressNotAdded < StandardError; end attr_accessor :id, :surname, :name, :name_reading, :surname_reading, :birth_date, :gender - def initialize(client, **info) - super - @contacts = [] - end - def fill(**info) @id = info.fetch(:id, @id) @surname = info.fetch(:surname, @surname) diff --git a/lib/manabu/person.rb b/lib/manabu/person.rb new file mode 100644 index 0000000..368252a --- /dev/null +++ b/lib/manabu/person.rb @@ -0,0 +1,75 @@ +module Manabu + class Person < Resource + + def initialize(client, **info) + super + @addresses = nil + @contacts = nil + @picture = nil + end + + def addresses + if @addresses.nil? + @addresses = _fetch_addresses + else + @addresses + end + end + + def contacts + if @contacts.nil? + @contacts = _fetch_contacts + else + @contacts + end + end + + def add_contact(contact) + contacts + + response = @client.post("students/#{id}/contacts", + contact_type_id: contact.contact_type.id, + data: contact.data + ) + Contact.new(@client, response).tap do |new_contact| + contacts.push(new_contact) + end + rescue StandardError + raise ContactNotAdded, "Contact is not added to #{self.class::SINGULAR}" + end + + def add_address(address) + response = + @client.post("students/#{id}/addresses", + address1: address.address1, + address2: address.address2, + zipcode: address.zipcode, + state: address.state, + country_id: address.country_id, + city: address.city + ) + Address.new(@client, response).tap do |new_address| + addresses.push(new_address) + end + rescue StandardError + raise AddressNotAdded, "Address is not added to #{self.class::SINGULAR}" + end + + private + + def _fetch_addresses + response = @client.get("#{self.class::PLURAL}/#{id}/addresses") + response[:addresses].map do |address| + Manabu::Address.new(@client, address) + end + end + + def _fetch_contacts + response = @client.get("#{self.class::PLURAL}/#{id}/contacts") + response[:contacts].map do |contact| + Manabu::Contact.new(@client, contact) + end + end + + end +end diff --git a/lib/manabu/student.rb b/lib/manabu/student.rb index c1f21a9..a47d7ca 100644 --- a/lib/manabu/student.rb +++ b/lib/manabu/student.rb @@ -1,24 +1,22 @@ require 'mimemagic' require_relative './resource' +require_relative './person' require_relative './guardian' require_relative './enrollment_status' require_relative './contact' module Manabu - class Student < Resource + class Student < Person + PLURAL= 'students'.freeze + SINGULAR = 'student'.freeze + class GuardianNotAdded < StandardError; end class ContactNotAdded < StandardError; end class AddressNotAdded < StandardError; end attr_accessor :id, :surname, :name, :name_reading, :surname_reading, :middle_name, :middle_name_reading,:birth_date, :gender, :enrollment_status, - :contacts, :enrollment_status_code - - def initialize(client, **info) - super - @contacts = [] - @picture = nil - end + :enrollment_status_code def fill(**info) @id = info.fetch(:id, @id) @@ -63,31 +61,6 @@ def add_guardian(guardian) raise GuardianNotAdded, 'Guardian is not added to student' end - def add_contact(contact_type, 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 - - def add_address(address) - response = - @client.post("students/#{id}/addresses", - address1: address.address1, - address2: address.address2, - zipcode: address.zipcode, - state: address.state, - country_id: address.country_id, - city: address.city - ) - self - rescue StandardError - raise AddressNotAdded, 'Address is not added to student' - end def guardians response = @client.get("students/#{id}/guardians")