Sha256: 594032c0d5c1682452631edbdb2e9923842693e4dc5f0a4bb985b9a8ef68aba5

Contents?: true

Size: 1.72 KB

Versions: 6

Compression:

Stored size: 1.72 KB

Contents

# frozen_string_literal: true
module PlatformSdk
  module PowerSchool
    # Powerschool::Client
    class Client
      attr_reader :bearer_token, :conn

      def initialize(base_url:, bearer_token: nil, expansions: nil)
        @bearer_token = bearer_token
        @bearer_token ||= PlatformSdk::PowerSchool.bearer_token
        @expansions = expansions || self.class.expansions

        @conn = Faraday.new(base_url, headers:) do |conn|
          conn.response :raise_error
          conn.request :json
          conn.response :json
          conn.adapter :net_http
        end
      end

      def headers
        {
          "Authorization" => "Bearer #{@bearer_token}",
          "Content-Type" => "application/json",
          "Accept" => "application/json"
        }
      end

      def power_query(query_name, page_size: 0, params: nil)
        scrub_query(query_name)
        @conn.post("/ws/schema/query/com.strongmind.#{query_name}?pagesize=#{page_size}", params).body["record"]
      end

      def special_programs
        records_as_json = power_query("specialprograms")
        records_as_json.map { |record| SpecialProgram.new(record) }
      end

      def self.expansions
        {
          STUDENTS: %w[demographics addresses alerts phones school_enrollment ethnicity_race contact contact_info
                       initial_enrollment schedule_setup fees lunch],
          SECTIONS: %w[term],
          TEACHERS: %w[addresses emails phones school_affiliations]
        }
      end

      def valid_power_query_names
        %w[specialprograms edkey.attendance_get_daily_total_minutes]
      end

      def scrub_query(query_name)
        raise PowerQueryNotValid unless valid_power_query_names.include?(query_name)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
strongmind-platform-sdk-3.1.2 lib/platform_sdk/power_school/client.rb
strongmind-platform-sdk-3.1.1 lib/platform_sdk/power_school/client.rb
strongmind-platform-sdk-3.1.0 lib/platform_sdk/power_school/client.rb
strongmind-platform-sdk-3.0.0 lib/platform_sdk/power_school/client.rb
strongmind-platform-sdk-2.16.3 lib/platform_sdk/power_school/client.rb
strongmind-platform-sdk-2.16.2 lib/platform_sdk/power_school/client.rb