Module: OpenStax::Accounts::Api
- Defined in:
- lib/openstax/accounts/api.rb,
app/representers/openstax/accounts/api/v1/group_representer.rb,
app/representers/openstax/accounts/api/v1/account_representer.rb,
app/representers/openstax/accounts/api/v1/group_user_representer.rb,
app/representers/openstax/accounts/api/v1/group_nesting_representer.rb,
app/representers/openstax/accounts/api/v1/account_search_representer.rb,
app/representers/openstax/accounts/api/v1/application_group_representer.rb,
app/representers/openstax/accounts/api/v1/unclaimed_account_representer.rb,
app/representers/openstax/accounts/api/v1/application_groups_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
-
.create_group(account, group, options = {}) ⇒ Object
Creates a group in the Accounts server.
-
.create_group_member(account, group_member, options = {}) ⇒ Object
Creates a group_member in the Accounts server.
-
.create_group_nesting(account, group_nesting, options = {}) ⇒ Object
Creates a group_nesting in the Accounts server.
-
.create_group_owner(account, group_owner, options = {}) ⇒ Object
Creates a group_owner in the Accounts server.
-
.destroy_group(account, group, options = {}) ⇒ Object
Deletes a group from the Accounts server.
-
.destroy_group_member(account, group_member, options = {}) ⇒ Object
Deletes a group_member from the Accounts server.
-
.destroy_group_nesting(account, group_nesting, options = {}) ⇒ Object
Deletes a group_nesting from the Accounts server.
-
.destroy_group_owner(account, group_owner, options = {}) ⇒ Object
Deletes a group_owner from the Accounts server.
-
.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.
-
.get_application_group_updates(options = {}) ⇒ Object
Retrieves information about groups that have been recently updated.
-
.mark_account_updates_as_read(application_users, options = {}) ⇒ Object
Marks account updates as “read”.
-
.mark_group_updates_as_read(application_groups, options = {}) ⇒ Object
Marks group 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.
-
.update_group(account, group, options = {}) ⇒ Object
Updates a group in the Accounts server.
Class Method Details
.create_group(account, group, options = {}) ⇒ Object
Creates a group in the Accounts server. The given account will be the owner of the group. Also takes an options hash. On failure, throws an Exception, just like the request method. On success, returns a hash containing the group attributes
129 130 131 132 133 134 135 136 137 |
# File 'lib/openstax/accounts/api.rb', line 129 def self.create_group(account, group, = {}) response = ActiveSupport::JSON.decode( request_for_account(account, :post, 'groups', .merge( body: group.attributes.slice('name', 'is_public').to_json )).body ) group.openstax_uid = response['id'] response end |
.create_group_member(account, group_member, options = {}) ⇒ Object
Creates a group_member in the Accounts server. The given account must own the group. Also takes an options hash. On failure, throws an Exception, just like the request method. On success, returns an OAuth2::Response object.
166 167 168 169 170 171 172 173 |
# File 'lib/openstax/accounts/api.rb', line 166 def self.create_group_member(account, group_member, = {}) request_for_account( account, :post, "groups/#{group_member.group_id}/members/#{group_member.user_id}", ) end |
.create_group_nesting(account, group_nesting, options = {}) ⇒ Object
Creates a group_nesting in the Accounts server. The given account must own both groups. Also takes an an options hash. On failure, throws an Exception, just like the request method. On success, returns an OAuth2::Response object.
222 223 224 225 226 227 228 229 230 |
# File 'lib/openstax/accounts/api.rb', line 222 def self.create_group_nesting(account, group_nesting, = {}) request_for_account( account, :post, "groups/#{group_nesting.container_group_id}/nestings/#{ group_nesting.member_group_id}", ) end |
.create_group_owner(account, group_owner, options = {}) ⇒ Object
Creates a group_owner in the Accounts server. The given account must own the group. Also takes an options hash. On failure, throws an Exception, just like the request method. On success, returns an OAuth2::Response object.
194 195 196 197 198 199 200 201 |
# File 'lib/openstax/accounts/api.rb', line 194 def self.create_group_owner(account, group_owner, = {}) request_for_account( account, :post, "groups/#{group_owner.group_id}/owners/#{group_owner.user_id}", ) end |
.destroy_group(account, group, options = {}) ⇒ Object
Deletes a group from the Accounts server. The given account must own the group. Also takes an options hash. On failure, throws an Exception, just like the request method. On success, returns an OAuth2::Response object.
157 158 159 |
# File 'lib/openstax/accounts/api.rb', line 157 def self.destroy_group(account, group, = {}) request_for_account(account, :delete, "groups/#{group.openstax_uid}", ) end |
.destroy_group_member(account, group_member, options = {}) ⇒ Object
Deletes a group_member from the Accounts server. The given account must own the group. Also takes an options hash. On failure, throws an Exception, just like the request method. On success, returns an OAuth2::Response object.
180 181 182 183 184 185 186 187 |
# File 'lib/openstax/accounts/api.rb', line 180 def self.destroy_group_member(account, group_member, = {}) request_for_account( account, :delete, "groups/#{group_member.group_id}/members/#{group_member.user_id}", ) end |
.destroy_group_nesting(account, group_nesting, options = {}) ⇒ Object
Deletes a group_nesting from the Accounts server. The given account must own either group. Also takes an options hash. On failure, throws an Exception, just like the request method. On success, returns an OAuth2::Response object.
237 238 239 240 241 242 243 244 245 |
# File 'lib/openstax/accounts/api.rb', line 237 def self.destroy_group_nesting(account, group_nesting, = {}) request_for_account( account, :delete, "groups/#{group_nesting.container_group_id}/nestings/#{ group_nesting.member_group_id}", ) end |
.destroy_group_owner(account, group_owner, options = {}) ⇒ Object
Deletes a group_owner from the Accounts server. The given account must own the group. Also takes an options hash. On failure, throws an Exception, just like the request method. On success, returns an OAuth2::Response object.
208 209 210 211 212 213 214 215 |
# File 'lib/openstax/accounts/api.rb', line 208 def self.destroy_group_owner(account, group_owner, = {}) request_for_account( account, :delete, "groups/#{group_owner.group_id}/owners/#{group_owner.user_id}", ) end |
.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.
251 252 253 |
# File 'lib/openstax/accounts/api.rb', line 251 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. Takes an options hash. On failure, throws an Exception, just like the request method. On success, returns an OAuth2::Response object.
81 82 83 |
# File 'lib/openstax/accounts/api.rb', line 81 def self.get_application_account_updates( = {}) request(:get, 'application_users/updates', ) end |
.get_application_group_updates(options = {}) ⇒ Object
Retrieves information about groups that have been recently updated. Results are limited to groups that users of the current app have access to. 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.get_application_group_updates( = {}) request(:get, 'application_groups/updates', ) 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.
94 95 96 |
# File 'lib/openstax/accounts/api.rb', line 94 def self.mark_account_updates_as_read(application_users, = {}) request(:put, 'application_users/updated', .merge(body: application_users.to_json)) end |
.mark_group_updates_as_read(application_groups, options = {}) ⇒ Object
Marks group updates as “read”. The application_groups parameter is an array of hashes. Each hash has 2 required fields: ‘id’, which should contain the application_group’s id, and ‘read_updates’, which should contain the last received value of unread_updates for that application_group. Can only be called for application_groups 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.
118 119 120 121 122 |
# File 'lib/openstax/accounts/api.rb', line 118 def self.mark_group_updates_as_read(application_groups, = {}) request(:put, 'application_groups/updated', .merge( body: application_groups.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.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/openstax/accounts/api.rb', line 16 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
37 38 39 40 |
# File 'lib/openstax/accounts/api.rb', line 37 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.
47 48 49 |
# File 'lib/openstax/accounts/api.rb', line 47 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.
71 72 73 |
# File 'lib/openstax/accounts/api.rb', line 71 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.
56 57 58 59 60 61 62 63 64 |
# File 'lib/openstax/accounts/api.rb', line 56 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 |
.update_group(account, group, options = {}) ⇒ Object
Updates a group in the Accounts server. The given account must own the group. Also takes an options hash. On failure, throws an Exception, just like the request method. On success, returns an OAuth2::Response object.
144 145 146 147 148 149 150 |
# File 'lib/openstax/accounts/api.rb', line 144 def self.update_group(account, group, = {}) request_for_account(account, :put, "groups/#{group.openstax_uid}", .merge( body: group.attributes.slice('name', 'is_public').to_json ) ) end |