Class: PandaPal::Platform

Inherits:
Object
  • Object
show all
Defined in:
app/models/panda_pal/platform.rb

Direct Known Subclasses

Canvas, Generic

Defined Under Namespace

Classes: Canvas, Generic

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.deserialize(params) ⇒ Object



31
32
33
# File 'app/models/panda_pal/platform.rb', line 31

def self.deserialize(params)
  new(params)
end

.from_serialized(ser) ⇒ Object



26
27
28
29
# File 'app/models/panda_pal/platform.rb', line 26

def self.from_serialized(ser)
  cls = ser[:platform_class].safe_constantize
  cls.deserialize(ser)
end

.organization_apiObject



75
76
77
78
79
80
81
82
83
84
# File 'app/models/panda_pal/platform.rb', line 75

def self.organization_api
  return PandaPal::Organization unless defined?(self::OrgExtension)
  return PandaPal::Organization if PandaPal::Organization < self::OrgExtension

  oext = self::OrgExtension

  @organization_api ||= self::OrganizationApi ||= Class.new(PandaPal::Organization) do
    include oext
  end
end

.resolve_platform(params) ⇒ Object



41
42
43
44
# File 'app/models/panda_pal/platform.rb', line 41

def self.resolve_platform(params)
  resolved = resolve_raw_platform(params)
  resolved.is_a?(Class) ? resolved.new({ iss: params[:iss] }) : resolved
end

.resolve_platform_class(params) ⇒ Object



46
47
48
49
# File 'app/models/panda_pal/platform.rb', line 46

def self.resolve_platform_class(params)
  resolved = resolve_raw_platform(params)
  resolved.is_a?(Class) ? resolved : resolved.class
end

.resolve_raw_platform(params) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/models/panda_pal/platform.rb', line 51

def self.resolve_raw_platform(params)
  platform = PandaPal.lti_options[:platform] || "canvas.instructure.com"

  if platform.is_a? Symbol
    platform_const = Object.send(platform, params)
    return platform_const if platform_const

  elsif platform.is_a? Proc
    platform_const = platform.call(params)
    return platform_const if platform_const

  else
    return Platform::Canvas if platform == "canvas.instructure.com"

    platform_const = platform.safe_constantize
    return platform_const if platform_const.is_a? Class
  end

  # TODO Default logic?
  return Platform::Canvas if Platform::Canvas::TRUSTED_ISSUERS.include?(params[:iss])

  raise "Unknown platform '#{platform}'"
end

Instance Method Details

#public_jwksObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/models/panda_pal/platform.rb', line 8

def public_jwks
  require "json/jwt"

  jwk_json = Rails.cache.fetch("panda_pal/jwks/#{jwks_url}")
  jwk_json ||= begin
    response = HTTParty.get(jwks_url)
    response.success? ? response.body : nil
  end

  return nil unless jwk_json.present?

  Rails.cache.write("panda_pal/jwks/#{jwks_url}", jwk_json, expires_in: 24.hours)

  JSON::JWK::Set.new(JSON.parse(jwk_json))
rescue
  nil
end

#serializeObject



35
36
37
38
39
# File 'app/models/panda_pal/platform.rb', line 35

def serialize
  {
    platform_class: self.class.to_s,
  }
end