Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/manabu/contact.rb
Original file line number Diff line number Diff line change
@@ -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

Expand Down
11 changes: 5 additions & 6 deletions lib/manabu/guardian.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
75 changes: 75 additions & 0 deletions lib/manabu/person.rb
Original file line number Diff line number Diff line change
@@ -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
39 changes: 6 additions & 33 deletions lib/manabu/student.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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")
Expand Down