Class: XodClient::Connection

Inherits:
Object
  • Object
show all
Includes:
DataEndpoints, InfoEndpoints
Defined in:
lib/xod_client/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DataEndpoints

#groups, #health, #school_info, #staff, #students, #timetable, #timetable_model, #timetable_structure

Methods included from InfoEndpoints

#gdpr_ids, #logs, #queries, #scopes, #token_details, #usage

Constructor Details

#initialize(relying_party, estab, secret, token: nil, token_expires_at: nil, response_logger: nil, token_refreshed_proc: nil) ⇒ Connection

rubocop:disable Metrics/ParameterLists



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/xod_client/connection.rb', line 19

def initialize(relying_party, estab, secret,
               token: nil, token_expires_at: nil, response_logger: nil, token_refreshed_proc: nil)
  @relying_party = relying_party
  @estab = estab
  @secret = secret
  @token = token
  @token_expires_at = token_expires_at
  @response_logger = response_logger
  @token_refreshed_proc = token_refreshed_proc
  @config = Config.new
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



15
16
17
# File 'lib/xod_client/connection.rb', line 15

def config
  @config
end

#estabObject (readonly)

Returns the value of attribute estab.



15
16
17
# File 'lib/xod_client/connection.rb', line 15

def estab
  @estab
end

#relying_partyObject (readonly)

Returns the value of attribute relying_party.



15
16
17
# File 'lib/xod_client/connection.rb', line 15

def relying_party
  @relying_party
end

#response_loggerObject (readonly)

Returns the value of attribute response_logger.



15
16
17
# File 'lib/xod_client/connection.rb', line 15

def response_logger
  @response_logger
end

#secretObject (readonly)

Returns the value of attribute secret.



15
16
17
# File 'lib/xod_client/connection.rb', line 15

def secret
  @secret
end

#tokenObject (readonly)

Returns the value of attribute token.



15
16
17
# File 'lib/xod_client/connection.rb', line 15

def token
  @token
end

#token_expires_atObject (readonly)

Returns the value of attribute token_expires_at.



15
16
17
# File 'lib/xod_client/connection.rb', line 15

def token_expires_at
  @token_expires_at
end

#token_refreshed_procObject (readonly)

Returns the value of attribute token_refreshed_proc.



15
16
17
# File 'lib/xod_client/connection.rb', line 15

def token_refreshed_proc
  @token_refreshed_proc
end

Instance Method Details

#endpoint(endpoint_name = nil, **params) ⇒ Object



31
32
33
34
35
36
# File 'lib/xod_client/connection.rb', line 31

def endpoint(endpoint_name = nil, **params)
  endpoint_name ||= params.delete(:endpoint_name) || raise(ArgumentError, 'Endpoint name should be provided')
  ensure_token

  EndpointCall.new(self, endpoint_name, params)
end

#ensure_tokenObject



38
39
40
# File 'lib/xod_client/connection.rb', line 38

def ensure_token
  refresh_token unless valid_token?
end

#faraday(**options) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/xod_client/connection.rb', line 51

def faraday(**options)
  Faraday.new(options) do |conn|
    conn.request :retry, config.retry_options
    conn.response :logger, response_logger, bodies: true if response_logger
    conn.adapter :net_http
    conn.headers['Content-Type'] = 'application/json'
    conn.headers['User-Agent'] = config.user_agent
  end
end

#refresh_tokenObject



42
43
44
45
# File 'lib/xod_client/connection.rb', line 42

def refresh_token
  @token, @token_expires_at = TokenRefresher.new(self).perform
  token_refreshed_proc&.call(token: @token, token_expires_at: @token_expires_at)
end

#valid_token?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/xod_client/connection.rb', line 47

def valid_token?
  token && token_expires_at&.future?
end