Class: AtomicLti::Services::Base

Inherits:
Object
  • Object
show all
Defined in:
app/lib/atomic_lti/services/base.rb

Instance Method Summary collapse

Constructor Details

#initialize(id_token_decoded: nil, iss: nil, deployment_id: nil) ⇒ Base

Returns a new instance of Base.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/lib/atomic_lti/services/base.rb', line 5

def initialize(id_token_decoded: nil, iss: nil, deployment_id: nil)
  token_iss = nil
  token_deployment_id = nil

  if id_token_decoded.present?
    token_iss = id_token_decoded["iss"]
    token_deployment_id = id_token_decoded[AtomicLti::Definitions::DEPLOYMENT_ID]
  end

  @id_token_decoded = id_token_decoded
  @iss = iss || token_iss
  @deployment_id = deployment_id || token_deployment_id
end

Instance Method Details

#get_next_url(response) ⇒ Object



28
29
30
31
# File 'app/lib/atomic_lti/services/base.rb', line 28

def get_next_url(response)
  next_url, = AtomicLti::PagingHelper.response_link_urls(response, "next")
  next_url
end

#headers(options = {}) ⇒ Object



21
22
23
24
25
26
# File 'app/lib/atomic_lti/services/base.rb', line 21

def headers(options = {})
  @token ||= AtomicLti::Authorization.request_token(iss: @iss, deployment_id: @deployment_id, scopes: scopes)
  {
    "Authorization" => "Bearer #{@token['access_token']}",
  }.merge(options)
end

#logged_service_call(method, *args) ⇒ Object



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
78
79
# File 'app/lib/atomic_lti/services/base.rb', line 49

def logged_service_call(method, *args)
  Rails.logger.debug("Making service call #{method} #{args}")
  response = HTTParty.send(method, *args)
  Rails.logger.debug("Got status #{response.code} for service call #{method} #{args}")

  if response.body.present? && response.success?
    parsed_body = JSON.parse(response.body)
  end

  if !response.success? && response.code != 404
    Rails.logger.error("Encountered an error while making service request #{method} #{args}")
    Rails.logger.error("Got code #{response.code}")
    Rails.logger.error(response.body)
  end

  [response, parsed_body]
rescue JSON::ParserError => e
  # We do not reraise this error as previously we did not check at all for valid json. This is purely for
  # logging purposes.
  Rails.logger.error("Encountered an error while parsing response for service request #{method} #{args}")
  Rails.logger.error(response.body)
  Rails.logger.error(e)

  [response, nil]
rescue StandardError => e
  Rails.logger.error("Encountered an error while making service request #{method} #{args}")
  Rails.logger.error(response&.body)
  Rails.logger.error(e)

  raise e
end

#scopesObject



19
# File 'app/lib/atomic_lti/services/base.rb', line 19

def scopes; end

#service_delete(*args) ⇒ Object



45
46
47
# File 'app/lib/atomic_lti/services/base.rb', line 45

def service_delete(*args)
  logged_service_call(:delete, *args)
end

#service_get(*args) ⇒ Object



33
34
35
# File 'app/lib/atomic_lti/services/base.rb', line 33

def service_get(*args)
  logged_service_call(:get, *args)
end

#service_post(*args) ⇒ Object



41
42
43
# File 'app/lib/atomic_lti/services/base.rb', line 41

def service_post(*args)
  logged_service_call(:post, *args)
end

#service_put(*args) ⇒ Object



37
38
39
# File 'app/lib/atomic_lti/services/base.rb', line 37

def service_put(*args)
  logged_service_call(:put, *args)
end