Class: Procore::Auth::ClientCredentials

Inherits:
Object
  • Object
show all
Defined in:
lib/procore/auth/client_credentials.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id:, client_secret:, host:) ⇒ ClientCredentials

Returns a new instance of ClientCredentials.



7
8
9
10
11
12
13
14
15
# File 'lib/procore/auth/client_credentials.rb', line 7

def initialize(client_id:, client_secret:, host:)
  unless client_id && client_secret
    raise OAuthError.new("No client_id or client_secret provided.")
  end

  @client_id = client_id
  @client_secret = client_secret
  @host = host
end

Instance Attribute Details

#client_idObject (readonly)

Returns the value of attribute client_id.



6
7
8
# File 'lib/procore/auth/client_credentials.rb', line 6

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



6
7
8
# File 'lib/procore/auth/client_credentials.rb', line 6

def client_secret
  @client_secret
end

#hostObject (readonly)

Returns the value of attribute host.



6
7
8
# File 'lib/procore/auth/client_credentials.rb', line 6

def host
  @host
end

Instance Method Details

#refreshObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/procore/auth/client_credentials.rb', line 17

def refresh(*)
  begin
    new_token = client
      .client_credentials
      .get_token

    Procore::Auth::Token.new(
      access_token: new_token.token,
      refresh_token: new_token.refresh_token,
      expires_at: new_token.expires_at,
    )
  rescue OAuth2::Error => e
    raise OAuthError.new(e.description, response: e.response)
  rescue Faraday::ConnectionFailed => e
    raise APIConnectionError.new("Connection Error: #{e.message}")
  rescue URI::BadURIError, URI::InvalidURIError
    raise OAuthError.new(
      "Host is not a valid URI. Check your host option to make sure it "   \
      "is a properly formed url",
    )
  end
end