Class: Badgrcat::Client

Inherits:
Footrest::Client
  • Object
show all
Defined in:
lib/badgrcat/client.rb,
lib/badgrcat/client/methods.rb

Defined Under Namespace

Modules: Methods

Instance Method Summary collapse

Instance Method Details

#authenticate!Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/badgrcat/client.rb', line 26

def authenticate!
  connection.headers[:authorization] = nil

  tok_params = {
    scope: config[:scope],
    client_id: config[:client_id],
  }

  if @refresh_token
    tok_params.merge!({
      grant_type: "refresh_token",
      refresh_token: @refresh_token,
    })
  else
    tok_params.merge!({
      username: config[:username],
      password: config[:password],
    })
  end

  authresp = connection.send(:post, "o/token", tok_params)
  authdata = authresp.body

  @refresh_token = authdata["refresh_token"].presence || @refresh_token
  connection.headers[:authorization] = "#{authdata['token_type']} #{authdata['access_token']}"
end

#request(method, &block) ⇒ Object

Override Footrest request for ApiArray support



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/badgrcat/client.rb', line 14

def request(method, &block)
  begin
    response = connection.send(method, &block)
  rescue Footrest::HttpError::Unauthorized
    # Reauthenticate and retry
    authenticate!
    response = connection.send(method, &block)
  end

  Badgrcat::ApiArray.process_response(response, self)
end