Class: Bright::SisApi::PowerSchool

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

Constant Summary collapse

DATE_FORMAT =
"%Y-%m-%d"
INVALID_SEARCH_CHAR_RE =
/[,;]/
@@description =
"Connects to the PowerSchool API for accessing student information"
@@doc_url =
"http://psimages.sunnysideschools.org/api-developer-guide-1.6.0/"
@@api_version =
"1.6.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#connection_retry_wrapper, #filter_students_by_params

Constructor Details

#initialize(options = {}) ⇒ PowerSchool

Returns a new instance of PowerSchool.



13
14
15
16
17
18
19
20
21
22
# File 'lib/bright/sis_apis/power_school.rb', line 13

def initialize(options = {})
  self.connection_options = options[:connection] || {}
  self.expansion_options = options[:expansion] || {}
  # {
  #   :client_id => "",
  #   :client_secret => "",
  #   :uri => ""
  #   :access_token => "", #optional
  # }
end

Instance Attribute Details

#connection_optionsObject

Returns the value of attribute connection_options.



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

def connection_options
  @connection_options
end

#expansion_optionsObject

Returns the value of attribute expansion_options.



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

def expansion_options
  @expansion_options
end

Instance Method Details

#create_student(student, additional_params = {}) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/bright/sis_apis/power_school.rb', line 75

def create_student(student, additional_params = {})
  response = request(:post, "ws/v1/student", convert_from_student_data(student, "INSERT", additional_params))
  if response["results"] and response["results"]["insert_count"] == 1
    student.api_id = response["results"]["result"]["success_message"]["id"]

    # update our local student object with any data the server might have updated
    nstudent = get_student_by_api_id(student.api_id)
    student.assign_attributes(Bright::Student.attribute_names.collect { |n| [n, nstudent.send(n)] }.to_h.reject { |k, v| v.nil? })

    # enrollment is no longer needed as creation is over
    student.enrollment = nil
    nstudent = nil
  else
    puts response.inspect
  end
  student
end

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



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/bright/sis_apis/power_school.rb', line 108

def get_schools(params = {}, options = {})
  params = apply_options(params, options)

  if options[:wrap_in_collection] != false
    schools_count_response_hash = request(:get, "ws/v1/district/school/count", params)
    # {"resource"=>{"count"=>293}}
    total_results = schools_count_response_hash["resource"]["count"].to_i if schools_count_response_hash["resource"]
  end

  schools_response_hash = request(:get, "ws/v1/district/school", params)
  schools_hsh = [schools_response_hash["schools"]["school"]].flatten

  schools = schools_hsh.compact.collect { |st_hsh|
    School.new(convert_to_school_data(st_hsh))
  }

  if options[:wrap_in_collection] != false
    api = self
    load_more_call = proc { |page|
      # pages start at one, so add a page here
      params[:page] = (page + 1)
      api.get_schools(params, {wrap_in_collection: false})
    }

    ResponseCollection.new({
      seed_page: schools,
      total: total_results,
      per_page: params[:pagesize],
      load_more_call: load_more_call,
      no_threads: options[:no_threads]
    })
  else
    schools
  end
end

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



30
31
32
# File 'lib/bright/sis_apis/power_school.rb', line 30

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

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



24
25
26
27
28
# File 'lib/bright/sis_apis/power_school.rb', line 24

def get_student_by_api_id(api_id, params = {})
  params = apply_expansions(params)
  st_hsh = request(:get, "ws/v1/student/#{api_id}", params)
  Student.new(convert_to_student_data(st_hsh["student"])) if st_hsh and st_hsh["student"]
end

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



34
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
64
65
66
67
68
69
70
71
72
73
# File 'lib/bright/sis_apis/power_school.rb', line 34

def get_students(params = {}, options = {})
  params = apply_expansions(params)
  params = apply_options(params, options)

  if options[:wrap_in_collection] != false
    students_count_response_hash = request(:get, "ws/v1/district/student/count", map_student_search_params(params))
    # {"resource"=>{"count"=>293}}
    total_results = students_count_response_hash["resource"]["count"].to_i if students_count_response_hash["resource"]
  end

  students_response_hash = request(:get, "ws/v1/district/student", map_student_search_params(params))
  if students_response_hash and students_response_hash["students"] && students_response_hash["students"]["student"]
    students_hash = [students_response_hash["students"]["student"]].flatten

    students = students_hash.compact.collect { |st_hsh|
      Student.new(convert_to_student_data(st_hsh))
    }

    if options[:wrap_in_collection] != false
      api = self
      load_more_call = proc { |page|
        # pages start at one, so add a page here
        params[:page] = (page + 1)
        api.get_students(params, {wrap_in_collection: false})
      }

      ResponseCollection.new({
        seed_page: students,
        total: total_results,
        per_page: params[:pagesize],
        load_more_call: load_more_call,
        no_threads: options[:no_threads]
      })
    else
      students
    end
  else
    []
  end
end

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



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/bright/sis_apis/power_school.rb', line 156

def request(method, path, params = {})
  uri = "#{connection_options[:uri]}/#{path}"
  body = nil
  if method == :get
    query = URI.encode_www_form(params)
    uri += "?#{query}"
  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)
  else
    puts "#{response.inspect}"
    puts "#{response.body}"
  end
  response_hash
end

#retrieve_access_tokenObject



144
145
146
147
148
149
150
151
152
153
154
# File 'lib/bright/sis_apis/power_school.rb', line 144

def retrieve_access_token
  connection = Bright::Connection.new("#{connection_options[:uri]}/oauth/access_token/")
  response = connection.request(:post, "grant_type=client_credentials", headers_for_access_token)
  if !response.error?
    response_hash = JSON.parse(response.body)
  end
  if response_hash["access_token"]
    connection_options[:access_token] = response_hash["access_token"]
  end
  response_hash
end

#subscribe_student(student) ⇒ Object

Raises:

  • (NotImplementedError)


104
105
106
# File 'lib/bright/sis_apis/power_school.rb', line 104

def subscribe_student(student)
  raise NotImplementedError
end

#update_student(student, additional_params = {}) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/bright/sis_apis/power_school.rb', line 93

def update_student(student, additional_params = {})
  response = request(:post, "ws/v1/student", convert_from_student_data(student, "UPDATE", additional_params))
  if response["results"] and response["results"]["update_count"] == 1
    student.api_id = response["results"]["result"]["success_message"]["id"]
    get_student_by_api_id(student.api_id)
  else
    puts response.inspect
    student
  end
end