Class: Procore::Client

Inherits:
Object
  • Object
show all
Includes:
Requestable
Defined in:
lib/procore/client.rb

Overview

Main class end users interact with. An instance of a client can call out the Procore API using methods matching standard HTTP verbs #get, #post, #put, #patch, #delete.

Examples:

Creating a new client:

store = Procore::Auth::Stores::Session.new(session: session)
client = Procore::Client.new(
  client_id: Rails.application.secrets.procore_client_id,
  client_secret: Rails.application.secrets.procore_secret_id,
  store: store
)

client.get("me").body #=> { id: 5, email: "[email protected]" }

Constant Summary

Constants included from Requestable

Requestable::HTTP_EXCEPTIONS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Requestable

#delete, #get, #patch, #post, #put, #sync

Constructor Details

#initialize(client_id:, client_secret:, store:, options: {}) ⇒ Client

Returns a new instance of Client.

Parameters:

  • client_id (String)

    Client ID issued from Procore

  • client_secret (String)

    Client Secret issued from Procore

  • store (Auth::Store)

    A store to use for saving, updating and refreshing tokens

  • options (Hash) (defaults to: {})

    options to configure the client with

Options Hash (options:):

  • :host (String)

    Endpoint to use for the API. Defaults to Configuration.host

  • :user_agent (String)

    User Agent string to send along with the request. Defaults to Configuration.user_agent



31
32
33
34
35
36
37
38
39
# File 'lib/procore/client.rb', line 31

def initialize(client_id:, client_secret:, store:, options: {})
  @options = Procore::Defaults::client_options.merge(options)
  @credentials = Procore::Auth::AccessTokenCredentials.new(
    client_id: client_id,
    client_secret: client_secret,
    host: @options[:host],
  )
  @store = store
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



20
21
22
# File 'lib/procore/client.rb', line 20

def options
  @options
end

#storeObject (readonly)

Returns the value of attribute store.



20
21
22
# File 'lib/procore/client.rb', line 20

def store
  @store
end

Instance Method Details

#refreshObject

Raises:

  • (OAuthError)

    if a token cannot be refreshed.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/procore/client.rb', line 42

def refresh
  token = fetch_token

  begin
    new_token = @credentials.refresh(
      token: token.access_token,
      refresh: token.refresh_token,
    )

    Util.log_info("Token Refresh Successful", store: store)
    store.save(new_token)
  rescue RuntimeError
    Util.log_error("Token Refresh Failed", store: store)
    raise Procore::OAuthError.new(
      "Unable to refresh the access token. Perhaps the Procore API is "  \
      "down or your access token store is misconfigured. Either "    \
      "way, you should clear the store and prompt the user to sign in "  \
      "again.",
    )
  end
end

#revokeObject

Raises:



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/procore/client.rb', line 65

def revoke
  token = fetch_token

  begin
    @credentials.revoke(token: token)
    Util.log_info("Token Revocation Successful", store: store)
  rescue RuntimeError
    Util.log_error("Token Revocation Failed", store: store)
    raise Procore::OAuthError.new(
      "Unable to revoke the access token. Perhaps the Procore API is "  \
      "down or your access token store is misconfigured. Either "    \
      "way, you should clear the store and prompt the user to sign in "  \
      "again.",
    )
  end
end