Module: PandaPal::Platform::Canvas::OrgExtension

Defined in:
app/models/panda_pal/platform.rb

Instance Method Summary collapse

Instance Method Details

#bearcat_clientObject



163
164
165
166
167
168
169
170
171
# File 'app/models/panda_pal/platform.rb', line 163

def bearcat_client
  return canvas_sync_client if defined?(canvas_sync_client)

  Bearcat::Client.new(
    prefix: canvas_url,
    token: canvas_token,
    master_rate_limit: (Rails.env.production? && !!defined?(Redis) && ENV['REDIS_URL'].present?),
  )
end

#canvas_api_tokenObject



151
152
153
154
155
156
157
158
159
160
# File 'app/models/panda_pal/platform.rb', line 151

def canvas_api_token
  PandaPal::Platform.find_org_setting([
    "canvas.api_token",
    "canvas.api_key",
    "canvas.token",
    "canvas_api_token",
    "canvas_token",
    "api_token",
  ], self)
end

#canvas_urlObject



141
142
143
144
145
146
147
148
149
# File 'app/models/panda_pal/platform.rb', line 141

def canvas_url
  PandaPal::Platform.find_org_setting([
    "canvas.base_url",
    "canvas_url",
    "canvas_base_url",
    "canvas.url",
    "base_url",
  ], self) || (Rails.env.development? && 'http://localhost:3000') || 'https://canvas.instructure.com'
end

#install_lti(host: nil, context: :root_account, version: 'v1p1', exists: :error) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/models/panda_pal/platform.rb', line 55

def install_lti(host: nil, context: :root_account, version: 'v1p1', exists: :error)
  raise "Automatically installing this LTI requires Bearcat." unless defined?(Bearcat)

  context = context.to_s
  context = 'account/self' if context == 'root_account'
  cid, ctype = context.split(/[\.\/]/).reverse
  ctype ||= 'account'

  existing_installs = bearcat_client.send(:"#{ctype}_external_tools", cid).all_pages_each.filter do |cet|
    cet[:consumer_key] == self.key
  end

  if existing_installs.present?
    case exists
    when :error
      raise "Tool with key #{self.key} already installed"
    when :duplicate
    when :replace
      existing_installs.each do |install|
        bearcat_client.send(:"delete_#{ctype}_external_tool", cid, install[:id])
      end
    else
      raise "exists: #{exists} is not supported"
    end
  end

  # TODO LTI 1.3 Support

  conf = {
    name: PandaPal.lti_options[:title],
    description: PandaPal.lti_options[:description],
    consumer_key: self.key,
    shared_secret: self.secret,
    privacy_level: "public",
    config_type: 'by_url',
    config_url: PandaPal::LaunchUrlHelpers.resolve_route(:v1p0_config_url, host: host),
  }

  bearcat_client.send(:"create_#{ctype}_external_tool", cid, conf)
end

#lti_api_configuration(host: nil) ⇒ Object



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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'app/models/panda_pal/platform.rb', line 96

def lti_api_configuration(host: nil)
  PandaPal::LaunchUrlHelpers.with_uri_host(host) do
    domain = PandaPal.lti_properties[:domain] || host.host
    launch_url = PandaPal.lti_options[:secure_launch_url] ||
      "#{domain}#{PandaPal.lti_options[:secure_launch_path]}" ||
      PandaPal.lti_options[:launch_url] ||
      "#{domain}#{PandaPal.lti_options[:launch_path]}" ||
      domain

    lti_json = {
      name: PandaPal.lti_options[:title],
      description: PandaPal.lti_options[:description],
      domain: host.host,
      url: launch_url,
      consumer_key: self.key,
      shared_secret: self.secret,
      privacy_level: "public",

      custom_fields: {},

      environments: PandaPal.lti_environments,
    }

    lti_json = lti_json.with_indifferent_access
    lti_json.merge!(PandaPal.lti_properties)

    (PandaPal.lti_options[:custom_fields] || []).each do |k, v|
      lti_json[:custom_fields][k] = v
    end

    PandaPal.lti_paths.each do |k, options|
      options = PandaPal::LaunchUrlHelpers.normalize_lti_launch_desc(options)
      options[:url] = PandaPal::LaunchUrlHelpers.absolute_launch_url(
        k.to_sym,
        host: host,
        launch_handler: :v1p0_launch_path,
        default_auto_launch: false
      ).to_s
      lti_json[k] = options
    end

    lti_json
  end
end