class Bright::SisApi::Aeries

Constants

DATE_FORMAT

Attributes

connection_options[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/bright/sis_apis/aeries.rb, line 12
def initialize(options = {})
  self.connection_options = options[:connection] || {}
  # {
  #   :certficate => "",
  #   :uri => ""
  # }
end

Public Instance Methods

create_student(student) click to toggle source
# File lib/bright/sis_apis/aeries.rb, line 57
def create_student(student)
  raise NotImplementedError
end
get_schools(params = {}) click to toggle source
# File lib/bright/sis_apis/aeries.rb, line 65
def get_schools(params = {})
  schools_response_hash = self.request(:get, 'api/v2/schools', params)
  
  schools_response_hash.collect{|h| School.new(convert_to_school_data(h))}
end
get_student(params) click to toggle source
# File lib/bright/sis_apis/aeries.rb, line 24
def get_student(params)
  get_students(params.merge(:limit => 1)).first
end
get_student_by_api_id(api_id) click to toggle source
# File lib/bright/sis_apis/aeries.rb, line 20
def get_student_by_api_id(api_id)
  get_students({:api_id => api_id, :limit => 1}).first
end
get_students(params) click to toggle source
# File lib/bright/sis_apis/aeries.rb, line 28
def get_students(params)
  if params.has_key?(:school) or params.has_key?(:school_api_id)
    school_api_id = params.delete(:school) || params.delete(:school_api_id)
    students = get_students_by_school(school_api_id, params)
  else
    threads = []
    get_schools.each do |school|
      threads << Thread.new do 
        get_students_by_school(school, params)
      end
    end
    students = threads.collect(&:value).flatten.compact
  end
  filter_students_by_params(students, params)
end
get_students_by_school(school, params = {}) click to toggle source
# File lib/bright/sis_apis/aeries.rb, line 44
def get_students_by_school(school, params = {})
  school_api_id = school.is_a?(School) ? school.api_id : school
  if params.has_key?(:api_id)
    path = "api/schools/#{school_api_id}/students/#{params[:api_id]}"
  elsif params.has_key?(:sis_student_id)
    path = "api/schools/#{school_api_id}/students/sn/#{params[:sis_student_id]}"
  else
    path = "api/schools/#{school_api_id}/students"
  end
  students_response_hash = self.request(:get, path, self.map_student_search_params(params))
  students_response_hash.collect{|shsh| Student.new(convert_to_student_data(shsh))}
end
request(method, path, params = {}) click to toggle source
# File lib/bright/sis_apis/aeries.rb, line 71
def request(method, path, params = {})
  uri  = "#{self.connection_options[:uri]}/#{path}"
  body = nil
  if method == :get
    query = URI.encode_www_form(params)
    uri += "?#{query}"
  else
    body = JSON.dump(params)
  end
  
  headers = self.headers_for_auth

  connection = Bright::Connection.new(uri)
  response = connection.request(method, body, headers)
  
  if !response.error?
    response_hash = JSON.parse(response.body)
  else
    puts "#{response.inspect}"
    puts "#{response.body}"
  end
  response_hash
end
update_student(student) click to toggle source
# File lib/bright/sis_apis/aeries.rb, line 61
def update_student(student)
  raise NotImplementedError
end

Protected Instance Methods

convert_to_school_data(attrs) click to toggle source
# File lib/bright/sis_apis/aeries.rb, line 126
def convert_to_school_data(attrs)
  cattrs = {}
  
  cattrs[:api_id] = attrs["SchoolCode"]
  cattrs[:name] = attrs["Name"]
  cattrs[:number] = attrs["SchoolCode"]
  
  cattrs.reject{|k,v| v.respond_to?(:empty?) ? v.empty? : v.nil?}
end
convert_to_student_data(attrs) click to toggle source
# File lib/bright/sis_apis/aeries.rb, line 101
def convert_to_student_data(attrs)
  cattrs = {}
  
  cattrs[:first_name]        = attrs["FirstName"]
  cattrs[:middle_name]       = attrs["MiddleName"]
  cattrs[:last_name]         = attrs["LastName"]
  
  cattrs[:api_id]           = attrs["PermanentID"]
  cattrs[:sis_student_id]   = attrs["StudentNumber"]
  cattrs[:state_student_id] = attrs["StateStudentID"]
  
  cattrs[:gender]           = attrs["Sex"]
  if attrs["Birthdate"]
    begin 
      cattrs[:birth_date] = Date.strptime(attrs["Birthdate"], DATE_FORMAT)
    rescue => e
      puts "#{e.inspect} #{bd}"
    end
  end
  
  #SchoolCode
  
  cattrs.reject{|k,v| v.respond_to?(:empty?) ? v.empty? : v.nil?}
end
headers_for_auth() click to toggle source
# File lib/bright/sis_apis/aeries.rb, line 136
def headers_for_auth
  {
      'AERIES-CERT' => self.connection_options[:certificate],
      'Content-Type' => "application/json"
  }
end
map_student_search_params(attrs) click to toggle source
# File lib/bright/sis_apis/aeries.rb, line 97
def map_student_search_params(attrs)
  attrs
end