Class: Bright::SisApi::Focus

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

Constant Summary collapse

DEFAULT_NO_THREADS =
2
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 Focus OneRoster API for accessing student information"
@@doc_url =
""
@@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 = {}) ⇒ Focus

Returns a new instance of Focus.



24
25
26
27
28
29
30
31
32
33
# File 'lib/bright/sis_apis/focus.rb', line 24

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/focus.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/focus.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/focus.rb', line 11

def schools_cache
  @schools_cache
end

Instance Method Details

#api_versionObject



35
36
37
# File 'lib/bright/sis_apis/focus.rb', line 35

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

#create_student(student) ⇒ Object

Raises:

  • (NotImplementedError)


78
79
80
# File 'lib/bright/sis_apis/focus.rb', line 78

def create_student(student)
  raise NotImplementedError
end

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



125
126
127
128
# File 'lib/bright/sis_apis/focus.rb', line 125

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



91
92
93
# File 'lib/bright/sis_apis/focus.rb', line 91

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



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

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



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/bright/sis_apis/focus.rb', line 95

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



44
45
46
# File 'lib/bright/sis_apis/focus.rb', line 44

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



39
40
41
42
# File 'lib/bright/sis_apis/focus.rb', line 39

def get_student_by_api_id(api_id, params = {})
  st_hsh = self.request(:get, "students/#{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



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
74
75
76
# File 'lib/bright/sis_apis/focus.rb', line 48

def get_students(params = {}, options = {})
  params[:limit] = params[:limit] || options[:limit] || 100
  students_response_hash = self.request(:get, 'students', 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] || DEFAULT_NO_THREADS
    })
  else
    students
  end
end

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



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/bright/sis_apis/focus.rb', line 130

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)


82
83
84
# File 'lib/bright/sis_apis/focus.rb', line 82

def update_student(student)
  raise NotImplementedError
end