Class: Bright::SisApi::InfiniteCampus

Inherits:
Base show all
Defined in:
lib/bright/sis_apis/infinite_campus.rb

Constant Summary collapse

DEMOGRAPHICS_CONVERSION =
{
  "americanIndianOrAlaskaNative"=>"American Indian Or Alaska Native",
  "asian"=>"Asian",
  "blackOrAfricanAmerican"=>"Black Or African American",
  "nativeHawaiianOrOtherPacificIslander"=>"Native Hawaiian Or Other Pacific Islander",
  "white"=>"White",
  "hispanicOrLatinoEthnicity"=>"Hispanic Or Latino"
}
@@description =
"Connects to the Infinite Campus OneRoster API for accessing student information"
@@doc_url =
"https://content.infinitecampus.com/sis/latest/documentation/oneroster-api"
@@api_version =
"1.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#connection_retry_wrapper, #filter_students_by_params

Constructor Details

#initialize(options = {}) ⇒ InfiniteCampus

Returns a new instance of InfiniteCampus.



22
23
24
25
26
27
28
29
30
31
# File 'lib/bright/sis_apis/infinite_campus.rb', line 22

def initialize(options = {})
  self.connection_options = options[:connection] || {}
  # {
  #   :client_id => "",
  #   :client_secret => "",
  #   :api_version => "", (defaults to @@api_version)
  #   :uri => "",
  #   :token_uri => ""  (api_version 1.2 required)
  # }
end

Instance Attribute Details

#connection_optionsObject

Returns the value of attribute connection_options.



11
12
13
# File 'lib/bright/sis_apis/infinite_campus.rb', line 11

def connection_options
  @connection_options
end

#school_years_cacheObject

Returns the value of attribute school_years_cache.



11
12
13
# File 'lib/bright/sis_apis/infinite_campus.rb', line 11

def school_years_cache
  @school_years_cache
end

#schools_cacheObject

Returns the value of attribute schools_cache.



11
12
13
# File 'lib/bright/sis_apis/infinite_campus.rb', line 11

def schools_cache
  @schools_cache
end

Instance Method Details

#api_versionObject



33
34
35
# File 'lib/bright/sis_apis/infinite_campus.rb', line 33

def api_version
  Gem::Version.new(self.connection_options.dig(:api_version) || @@api_version)
end

#create_student(student) ⇒ Object

Raises:

  • (NotImplementedError)


86
87
88
# File 'lib/bright/sis_apis/infinite_campus.rb', line 86

def create_student(student)
  raise NotImplementedError
end

#get_contact_by_api_id(api_id, params = {}) ⇒ Object



133
134
135
136
# File 'lib/bright/sis_apis/infinite_campus.rb', line 133

def get_contact_by_api_id(api_id, params ={})
  contact_hsh = self.request(:get, "users/#{api_id}", params)
  Contact.new(convert_to_user_data(contact_hsh["user"], bright_type: "Contact")) if contact_hsh and contact_hsh["user"]
end

#get_school(params = {}, options = {}) ⇒ Object



99
100
101
# File 'lib/bright/sis_apis/infinite_campus.rb', line 99

def get_school(params = {}, options = {})
  self.get_schools(params, options.merge(:limit => 1, :wrap_in_collection => false)).first
end

#get_school_by_api_id(api_id, params = {}) ⇒ Object



94
95
96
97
# File 'lib/bright/sis_apis/infinite_campus.rb', line 94

def get_school_by_api_id(api_id, params = {})
  sc_hsh = self.request(:get, "schools/#{api_id}", params)
  School.new(convert_to_school_data(sc_hsh["org"])) if sc_hsh and sc_hsh["org"]
end

#get_schools(params = {}, options = {}) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/bright/sis_apis/infinite_campus.rb', line 103

def get_schools(params = {}, options = {})
  params[:limit] = params[:limit] || options[:limit] || 100
  schools_response_hash = self.request(:get, 'schools', self.map_school_search_params(params))
  total_results = schools_response_hash[:response_headers]["x-total-count"].to_i
  if schools_response_hash and schools_response_hash["orgs"]
    schools_hash = [schools_response_hash["orgs"]].flatten

    schools = schools_hash.compact.collect {|sc_hsh|
      School.new(convert_to_school_data(sc_hsh))
    }
  end
  if options[:wrap_in_collection] != false
    api = self
    load_more_call = proc { |page|
      # pages start at one, so add a page here
      params[:offset] = (params[:limit].to_i * page)
      api.get_schools(params, {:wrap_in_collection => false})
    }
    ResponseCollection.new({
      :seed_page => schools,
      :total => total_results,
      :per_page => params[:limit],
      :load_more_call => load_more_call,
      :no_threads => options[:no_threads]
    })
  else
    schools
  end
end

#get_student(params = {}, options = {}) ⇒ Object



47
48
49
# File 'lib/bright/sis_apis/infinite_campus.rb', line 47

def get_student(params = {}, options = {})
  self.get_students(params, options.merge(:limit => 1, :wrap_in_collection => false)).first
end

#get_student_by_api_id(api_id, params = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/bright/sis_apis/infinite_campus.rb', line 37

def get_student_by_api_id(api_id, params = {})
  if api_version <= Gem::Version.new("1.1")
    params = {:role => "student"}.merge(params)
  else
    params = {:roles => "student"}.merge(params)
  end
  st_hsh = self.request(:get, "users/#{api_id}", params)
  Student.new(convert_to_user_data(st_hsh["user"])) if st_hsh and st_hsh["user"]
end

#get_students(params = {}, options = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/bright/sis_apis/infinite_campus.rb', line 51

def get_students(params = {}, options = {})
  if api_version <= Gem::Version.new("1.1")
    params = {:role => "student"}.merge(params)
  else
    params = {:roles => "student"}.merge(params)
  end
  params[:limit] = params[:limit] || options[:limit] || 100
  students_response_hash = self.request(:get, 'users', self.map_search_params(params))
  total_results = students_response_hash[:response_headers]["x-total-count"].to_i
  if students_response_hash and students_response_hash["users"]
    students_hash = [students_response_hash["users"]].flatten

    students = students_hash.compact.collect {|st_hsh|
      Student.new(convert_to_user_data(st_hsh))
    }
  end
  if options[:wrap_in_collection] != false
    api = self
    load_more_call = proc { |page|
      # pages start at one, so add a page here
      params[:offset] = (params[:limit].to_i * page)
      api.get_students(params, {:wrap_in_collection => false})
    }
    ResponseCollection.new({
      :seed_page => students,
      :total => total_results,
      :per_page => params[:limit],
      :load_more_call => load_more_call,
      :no_threads => options[:no_threads]
    })
  else
    students
  end
end

#request(method, path, params = {}) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/bright/sis_apis/infinite_campus.rb', line 138

def request(method, path, params = {})
  uri  = "#{self.connection_options[:uri]}/#{path}"
  body = nil
  if method == :get
    query = URI.encode_www_form(params)
    uri += "?#{query}" unless query.strip == ""
  else
    body = JSON.dump(params)
  end

  response = connection_retry_wrapper {
    connection = Bright::Connection.new(uri)
    headers = self.headers_for_auth(uri)
    connection.request(method, body, headers)
  }

  if !response.error?
    response_hash = JSON.parse(response.body)
    response_hash[:response_headers] = response.headers
  else
    puts "#{response.inspect}"
    puts "#{response.body}"
  end
  response_hash
end

#update_student(student) ⇒ Object

Raises:

  • (NotImplementedError)


90
91
92
# File 'lib/bright/sis_apis/infinite_campus.rb', line 90

def update_student(student)
  raise NotImplementedError
end