Class: Bright::SisApi::Skyward

Inherits:
Base show all
Defined in:
lib/bright/sis_apis/skyward.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"
}
PHONE_TYPE_CONVERSION =
{
  "Cellular" => "Cell",
  "Work" => "Work",
  "Home" => "Home",
  "Other" => "Other"
}
@@description =
"Connects to the Skyward API for accessing student information"
@@doc_url =
"https://esdemo1.skyward.com/api/swagger/ui/index"
@@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 = {}) ⇒ Skyward

Returns a new instance of Skyward.



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

def initialize(options = {})
  self.connection_options = options[:connection] || {}
  # {
  #   :client_id => "",
  #   :client_secret => "",
  #   :uri => "https://skywardinstall.com/API"
  # }
end

Instance Attribute Details

#connection_optionsObject

Returns the value of attribute connection_options.



9
10
11
# File 'lib/bright/sis_apis/skyward.rb', line 9

def connection_options
  @connection_options
end

#schools_cacheObject

Returns the value of attribute schools_cache.



9
10
11
# File 'lib/bright/sis_apis/skyward.rb', line 9

def schools_cache
  @schools_cache
end

Instance Method Details

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



95
96
97
98
# File 'lib/bright/sis_apis/skyward.rb', line 95

def get_contact_by_api_id(api_id, params = {})
  contact_hsh = self.request(:get, "v1/names/#{api_id}", params)[:parsed_body]
  Contact.new(convert_to_user_data(contact_hsh, {:type => "Contact"})) unless contact_hsh.blank?
end

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



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/bright/sis_apis/skyward.rb', line 100

def get_guardians_by_api_id(api_id, params = {})
  guardians = []
  guardians_array = self.request(:get, "v1/guardians", params.merge({"studentNameId" => api_id}))[:parsed_body]
  if !guardians_array.blank?
    guardians_array.each do |guardian_hsh|
      relationship_type = guardian_hsh.delete("Students").detect{|s_hsh| s_hsh["StudentNameId"].to_s == api_id.to_s}["RelationshipDesc"]
      guardian_hsh["RelationshipType"] = relationship_type
      guardian_hsh["NameId"] = guardian_hsh.delete("GuardianNameId")
      guardians << Contact.new(convert_to_user_data(guardian_hsh, {:type => "Contact"}))
    end
  end
  return guardians
end

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



79
80
81
82
# File 'lib/bright/sis_apis/skyward.rb', line 79

def get_school_by_api_id(api_id, params = {})
  sc_hsh = self.request(:get, "v1/schools/#{api_id}", params)[:parsed_body]
  School.new(convert_to_school_data(sc_hsh)) unless sc_hsh.blank?
end

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



84
85
86
87
88
89
90
91
92
93
# File 'lib/bright/sis_apis/skyward.rb', line 84

def get_schools(params = {}, options = {})
  params["paging.limit"] = params[:limit] || options[:limit] || 10000
  schools_hshs = self.request(:get, "v1/schools", params)[:parsed_body]
  if !schools_hshs.blank?
    schools = schools_hshs.compact.collect {|sc_hsh|
      School.new(convert_to_school_data(sc_hsh))
    }
  end
  return schools
end

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



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

def get_student_by_api_id(api_id, params = {})
  st_hsh = self.request(:get, "v1/students/#{api_id}", params)[:parsed_body]
  Student.new(convert_to_user_data(st_hsh, {:type => "Student"})) unless st_hsh.blank?
end

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



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

def get_students(params = {}, options = {})
  params["paging.limit"] = params[:limit] || options[:limit] || 1000
  students_response = self.request(:get, 'v1/students', params)
  if !students_response[:parsed_body].blank?
    students = students_response[:parsed_body].compact.collect {|st_hsh|
      Student.new(convert_to_user_data(st_hsh, {:type => "Student"}))
    }
  end

  next_cursor = nil
  if students_response[:headers]["Link"]
    students_response[:headers]["Link"].split(",").each do |part, index|
      section = part.split(';')
      url = section[0][/<(.*)>/,1]
      name = section[1][/rel="(.*)"/,1].to_s
      if name == "next"
        next_cursor = CGI.parse(URI.parse(url).query)["cursor"].first
      end
    end
  end
  if options[:wrap_in_collection] != false
    api = self
    load_more_call = proc {|cursor|
      params["paging.cursor"] = cursor
      options = {:wrap_in_collection => false, :include_cursor => true}
      api.get_students(params, options)
    }
    CursorResponseCollection.new({
      :seed_page => students,
      :load_more_call => load_more_call,
      :next_cursor => next_cursor
    })
  elsif options[:include_cursor] == true
    return {:objects => students, :next_cursor => next_cursor}
  else
    return students
  end
end

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



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

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

  response = connection_retry_wrapper {
    connection = Bright::Connection.new(uri)
    headers = self.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
  return {:parsed_body => response_hash, :headers => response.headers}
end

#retrieve_access_tokenObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/bright/sis_apis/skyward.rb', line 114

def retrieve_access_token
  connection = Bright::Connection.new("#{self.connection_options[:uri]}/token")
  response = connection.request(:post,
    {"grant_type" => "password",
      "username" => self.connection_options[:client_id],
      "password" => self.connection_options[:client_secret]
    },
    self.headers_for_access_token)
  if !response.error?
    response_hash = JSON.parse(response.body)
  end
  if response_hash["access_token"]
    self.connection_options[:access_token] = response_hash["access_token"]
    self.connection_options[:access_token_expires] = (Time.now - 10) + response_hash["expires_in"]
  end
  response_hash
end