Class: Bright::SisApi::BrightSis

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

Constant Summary collapse

DEMOGRAPHICS_CONVERSION =
{
  "I" => "American Indian Or Alaska Native",
  "A" => "Asian",
  "B" => "Black Or African American",
  "P" => "Native Hawaiian Or Other Pacific Islander",
  "W" => "White",
  "M" => "Other"
}
@@description =
"Connects to the Bright SIS Data Store"
@@doc_url =
""
@@api_version =
"v1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#connection_retry_wrapper, #filter_students_by_params

Constructor Details

#initialize(options = {}) ⇒ BrightSis

Returns a new instance of BrightSis.



19
20
21
22
23
24
25
# File 'lib/bright/sis_apis/bright_sis.rb', line 19

def initialize(options = {})
  self.connection_options = options[:connection] || {}
  # {
  #   :access_token => ""
  #   :uri => ""
  # }
end

Instance Attribute Details

#connection_optionsObject

Returns the value of attribute connection_options.



8
9
10
# File 'lib/bright/sis_apis/bright_sis.rb', line 8

def connection_options
  @connection_options
end

Instance Method Details

#create_student(student) ⇒ Object

Raises:

  • (NotImplementedError)


65
66
67
# File 'lib/bright/sis_apis/bright_sis.rb', line 65

def create_student(student)
  raise NotImplementedError
end

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



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/bright/sis_apis/bright_sis.rb', line 73

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

    schools = schools_hash.compact.collect { |st_hsh|
      School.new(convert_to_school_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_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



31
32
33
# File 'lib/bright/sis_apis/bright_sis.rb', line 31

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

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



27
28
29
# File 'lib/bright/sis_apis/bright_sis.rb', line 27

def get_student_by_api_id(api_id, options = {})
  get_students({"uuid" => api_id}, options.merge(limit: 1, wrap_in_collection: false)).first
end

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/bright/sis_apis/bright_sis.rb', line 35

def get_students(params = {}, options = {})
  params[:limit] = params[:limit] || options[:limit] || 100
  students_response_hash = request(:get, "students", map_student_search_params(params))
  total_results = students_response_hash[:response_headers]["total"].to_i
  if students_response_hash and students_response_hash["students"]
    students_hash = [students_response_hash["students"]].flatten

    students = students_hash.compact.collect { |st_hsh|
      Student.new(convert_to_student_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



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
# File 'lib/bright/sis_apis/bright_sis.rb', line 103

def request(method, path, params = {})
  uri = "#{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 = headers_for_auth
    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)


69
70
71
# File 'lib/bright/sis_apis/bright_sis.rb', line 69

def update_student(student)
  raise NotImplementedError
end