Module: OpenStax::Accounts::Api
- Defined in:
- lib/openstax/accounts/api.rb,
app/representers/openstax/accounts/api/v1/account_representer.rb,
app/representers/openstax/accounts/api/v1/account_search_representer.rb,
app/representers/openstax/accounts/api/v1/unclaimed_account_representer.rb,
app/representers/openstax/accounts/api/v1/application_account_representer.rb,
app/representers/openstax/accounts/api/v1/application_accounts_representer.rb,
app/representers/openstax/accounts/api/v1/application_account_search_representer.rb
Defined Under Namespace
Modules: V1
Constant Summary collapse
- DEFAULT_API_VERSION =
:v1
Class Method Summary collapse
-
.find_or_create_account(attributes, options = {}) ⇒ Object
Finds an account matching the provided attributes or creates a new account.
-
.get_application_account_updates(options = {}) ⇒ Object
Retrieves information about accounts that have been recently updated.
-
.mark_account_updates_as_read(application_users, options = {}) ⇒ Object
Marks account updates as “read”.
-
.request(http_method, path, options = {}) ⇒ Object
Executes an OpenStax Accounts API request, using the given HTTP method, API path and request options.
-
.request_for_account(account, http_method, path, options = {}) ⇒ Object
Performs an API request using the given account’s access token.
-
.search_accounts(query, options = {}) ⇒ Object
Performs an account search in the Accounts server.
-
.search_application_accounts(query, options = {}) ⇒ Object
Performs an account search in the Accounts server.
-
.update_account(account, options = {}) ⇒ Object
Updates a user account in the Accounts server.
Class Method Details
.find_or_create_account(attributes, options = {}) ⇒ Object
Finds an account matching the provided attributes or creates a new account. Also takes an options hash. On failure, throws an Exception, just like the request method. On success, returns an OAuth2::Response object.
105 106 107 |
# File 'lib/openstax/accounts/api.rb', line 105 def self.find_or_create_account(attributes, = {}) request(:post, "user/find-or-create", .merge(body: attributes.to_json)) end |
.get_application_account_updates(options = {}) ⇒ Object
Retrieves information about accounts that have been recently updated. Results are limited to accounts that have used the current app, and limited in quantity per call by a configuration parameter. Takes an options hash. On failure, throws an Exception, just like the request method. On success, returns an OAuth2::Response object.
81 82 83 84 85 86 |
# File 'lib/openstax/accounts/api.rb', line 81 def self.get_application_account_updates( = {}) limit = OpenStax::Accounts.configuration.max_user_updates_per_request request( :get, "application_users/updates#{ '?limit=' + limit.to_s if !limit.blank? }", ) end |
.mark_account_updates_as_read(application_users, options = {}) ⇒ Object
Marks account updates as “read”. The application_users parameter is an array of hashes. Each hash has 2 required fields: ‘id’, which should contain the application_user’s id, and ‘read_updates’, which should contain the last received value of unread_updates for that application_user. Can only be called for application_users that belong to the current app. Also takes an options hash. On failure, throws an Exception, just like the request method. On success, returns an OAuth2::Response object.
97 98 99 |
# File 'lib/openstax/accounts/api.rb', line 97 def self.mark_account_updates_as_read(application_users, = {}) request(:put, 'application_users/updated', .merge(body: application_users.to_json)) end |
.request(http_method, path, options = {}) ⇒ Object
Executes an OpenStax Accounts API request, using the given HTTP method, API path and request options. Any options accepted by OAuth2 requests can be used, such as :params, :body, :headers, etc, plus the :access_token option, which can be used to manually specify an OAuth access token. On failure, it can throw Faraday::ConnectionFailed for connection errors or OAuth2::Error if Accounts returns an HTTP 400 error, such as 422 Unprocessable Entity. On success, returns an OAuth2::Response object.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/openstax/accounts/api.rb', line 15 def self.request(http_method, path, = {}) version = .delete(:api_version) || DEFAULT_API_VERSION unless version.blank? [:headers] ||= {} [:headers].merge!({ 'Accept' => "application/vnd.accounts.openstax.#{version.to_s}", 'Content-Type' => 'application/json' }) end token_string = .delete(:access_token) token = token_string.blank? ? client.client_credentials.get_token : OAuth2::AccessToken.new(client, token_string) accounts_url = OpenStax::Accounts.configuration.openstax_accounts_url url = URI.join(accounts_url, 'api/', path) token.request(http_method, url, ) end |
.request_for_account(account, http_method, path, options = {}) ⇒ Object
Performs an API request using the given account’s access token
36 37 38 39 |
# File 'lib/openstax/accounts/api.rb', line 36 def self.request_for_account(account, http_method, path, = {}) request(http_method, path, .merge(access_token: account.access_token)) end |
.search_accounts(query, options = {}) ⇒ Object
Performs an account search in the Accounts server. Results are limited to 10 accounts maximum. Takes a query parameter and an options hash. On failure, throws an Exception, just like the request method. On success, returns an OAuth2::Response object.
46 47 48 |
# File 'lib/openstax/accounts/api.rb', line 46 def self.search_accounts(query, = {}) request(:get, 'users', .merge(params: {q: query})) end |
.search_application_accounts(query, options = {}) ⇒ Object
Performs an account search in the Accounts server. Results are limited to accounts that have used the current app. Takes a query parameter and an options hash. On failure, throws an Exception, just like the request method. On success, returns an OAuth2::Response object.
70 71 72 |
# File 'lib/openstax/accounts/api.rb', line 70 def self.search_application_accounts(query, = {}) request(:get, 'application_users', .merge(params: {q: query})) end |
.update_account(account, options = {}) ⇒ Object
Updates a user account in the Accounts server. The account is determined by the OAuth access token. Also takes an options hash. On failure, throws an Exception, just like the request method. On success, returns an OAuth2::Response object.
55 56 57 58 59 60 61 62 63 |
# File 'lib/openstax/accounts/api.rb', line 55 def self.update_account(account, = {}) request_for_account( account, :put, 'user', .merge( body: account.attributes.slice('username', 'first_name', 'last_name', 'full_name', 'title').to_json ) ) end |