Class: PsUtilities::Connection

Inherits:
Object
  • Object
show all
Includes:
PreBuiltGet, PreBuiltPost, PreBuiltPut
Defined in:
lib/ps_utilities/connection.rb

Overview

The PsUtilities, makes it east to work with the Powerschool API

Since:

  • 0.1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PreBuiltPost

#create_students, #update_students

Methods included from PreBuiltGet

#get_all_active_students, #get_all_matching_students, #get_one_student

Constructor Details

#initialize(api_info: {}, client_info: {}) ⇒ Connection

Note:

preference is to use environment variables to initialize your server.

def initialize( header_info: {}, api_info: {}, client_info: {})

Parameters:

  • api_info: (Hash) (defaults to: {})
    • allows override of api settings :base_uri (overrides ENV) and :auth_path (overrides ENV)

  • client_info: (Hash) (defaults to: {})
    • allows override of client login values :client_id (overrides ENV) & :client_secret (overrides ENV)

Raises:

  • (ArgumentError)

Since:

  • 0.1.0



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ps_utilities/connection.rb', line 40

def initialize( api_info: {}, client_info: {})
  @version       = "v#{PsUtilities::Version::VERSION}"
  @client        = client_defaults.merge(client_info)
  @client_id     = client[:client_id]
  @client_secret = client[:client_secret]
  @api_data      = api_defaults.merge(api_info)
  @base_uri      = api_data[:base_uri]
  @auth_path     = api_data[:auth_endpoint]
  @headers       = header_defaults  # Auth key is added when authenticate is executed
  # @headers['Authorization'] = header_info['Authorization'] if header_info['Authorization']

  raise ArgumentError, "missing client_secret" if client_secret.nil? or client_secret.empty?
  raise ArgumentError, "missing client_id"     if client_id.nil? or client_id.empty?
  raise ArgumentError, "missing auth endpoint" if auth_path.nil? or auth_path.empty?
  raise ArgumentError, "missing base_uri"      if base_uri.nil? or base_uri.empty?
end

Instance Attribute Details

#api_dataObject (readonly)

Since:

  • 0.1.0



28
29
30
# File 'lib/ps_utilities/connection.rb', line 28

def api_data
  @api_data
end

#auth_infoObject (readonly)

Since:

  • 0.1.0



27
28
29
# File 'lib/ps_utilities/connection.rb', line 27

def auth_info
  @auth_info
end

#auth_pathObject (readonly)

Since:

  • 0.1.0



27
28
29
# File 'lib/ps_utilities/connection.rb', line 27

def auth_path
  @auth_path
end

#auth_tokenObject (readonly)

Since:

  • 0.1.0



27
28
29
# File 'lib/ps_utilities/connection.rb', line 27

def auth_token
  @auth_token
end

#base_uriObject (readonly)

Since:

  • 0.1.0



28
29
30
# File 'lib/ps_utilities/connection.rb', line 28

def base_uri
  @base_uri
end

#clientObject (readonly)

Since:

  • 0.1.0



29
30
31
# File 'lib/ps_utilities/connection.rb', line 29

def client
  @client
end

#client_idObject (readonly)

Since:

  • 0.1.0



29
30
31
# File 'lib/ps_utilities/connection.rb', line 29

def client_id
  @client_id
end

#client_secretObject (readonly)

Since:

  • 0.1.0



29
30
31
# File 'lib/ps_utilities/connection.rb', line 29

def client_secret
  @client_secret
end

#headersObject (readonly)

Since:

  • 0.1.0



27
28
29
# File 'lib/ps_utilities/connection.rb', line 27

def headers
  @headers
end

#versionObject (readonly)

Since:

  • 0.1.0



30
31
32
# File 'lib/ps_utilities/connection.rb', line 30

def version
  @version
end

Instance Method Details

#run(command: nil, api_path: "", options: {}, params: {}) ⇒ Object

Note:

with no command an authenticatation check is done

this runs the various options:

Parameters:

  • command: (Symbol) (defaults to: nil)
    • commands include direct api calls: :authenticate, :delete, :get, :patch, :post, :put (these require api_path: and options: params) & also pre-built commands - see included methods (they require params:)

  • api_path: (String) (defaults to: "")
    • this is the api_endpoint (only needed for direct api calls)

  • options: (Hash) (defaults to: {})
    • this is the data body or the query options (needed for direct api calls)

  • params: (Hash) (defaults to: {})
    • this is the data needed for using pre-built commands - see the individual command for details

Since:

  • 0.1.0



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ps_utilities/connection.rb', line 63

def run(command: nil, api_path: "", options: {}, params: {})
  authenticate                            unless auth_valid?

  case command
  when nil, :authenticate
    authenticate
  when :delete, :get, :patch, :post, :put
    api(command, api_path, options)       unless api_path.empty?
    # TODO: panick if api_path empty
  else
    send(command, params)
  end
  rescue ArgumentError => error
    return {"error_message" => error.message}
end