Module: OpenStax::Accounts

Defined in:
lib/openstax_accounts.rb,
lib/openstax/accounts/engine.rb,
lib/openstax/accounts/version.rb,
app/models/openstax/accounts/group.rb,
app/models/openstax/accounts/account.rb,
app/models/openstax/accounts/group_owner.rb,
app/models/openstax/accounts/group_member.rb,
app/models/openstax/accounts/group_nesting.rb,
app/routines/openstax/accounts/sync_groups.rb,
lib/openstax/accounts/current_user_manager.rb,
app/routines/openstax/accounts/sync_accounts.rb,
lib/openstax/accounts/has_many_through_groups.rb,
app/handlers/openstax/accounts/accounts_search.rb,
app/models/openstax/accounts/anonymous_account.rb,
app/models/openstax/accounts/application_group.rb,
app/routines/openstax/accounts/search_accounts.rb,
app/handlers/openstax/accounts/sessions_callback.rb,
app/helpers/openstax/accounts/application_helper.rb,
app/models/openstax/accounts/application_account.rb,
app/routines/openstax/accounts/dev/create_account.rb,
lib/openstax/accounts/default_account_user_mapper.rb,
app/handlers/openstax/accounts/dev/accounts_create.rb,
app/handlers/openstax/accounts/dev/accounts_search.rb,
app/routines/openstax/accounts/update_group_caches.rb,
app/routines/openstax/accounts/search_local_accounts.rb,
app/controllers/openstax/accounts/dev/base_controller.rb,
app/controllers/openstax/accounts/sessions_controller.rb,
app/controllers/openstax/accounts/application_controller.rb,
app/controllers/openstax/accounts/dev/accounts_controller.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/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: Api, ApplicationHelper, Dev, HasManyThroughGroups Classes: Account, AccountsSearch, AnonymousAccount, ApplicationAccount, ApplicationController, ApplicationGroup, Configuration, CurrentUserManager, DefaultAccountUserMapper, Engine, Group, GroupMember, GroupNesting, GroupOwner, SearchAccounts, SearchLocalAccounts, SessionsCallback, SessionsController, SyncAccounts, SyncGroups, UpdateGroupCaches

Constant Summary collapse

DEFAULT_API_VERSION =
:v1
VERSION =
"4.1.1"

Class Method Summary collapse

Class Method Details

.api_call(http_method, url, options = {}) ⇒ Object

Executes an OpenStax Accounts API call, using the given HTTP method, API url 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.



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/openstax_accounts.rb', line 125

def api_call(http_method, url, options = {})
  version = options.delete(:api_version)
  unless version.blank?
    options[:headers] ||= {}
    options[:headers].merge!({
      'Accept' => "application/vnd.accounts.openstax.#{version.to_s}"
    })
  end

  token_string = options.delete(:access_token)
  token = token_string.blank? ? client.client_credentials.get_token :
            OAuth2::AccessToken.new(client, token_string)

  api_url = URI.join(configuration.openstax_accounts_url, 'api/', url)

  token.request(http_method, api_url, options)
end

.configurationObject



38
39
40
# File 'lib/openstax_accounts.rb', line 38

def configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Configuration machinery.

To configure OpenStax Accounts, put the following code in your application’s initialization logic (eg. in the config/initializers in a Rails app)

OpenStax::Accounts.configure do |config|
  config.<parameter name> = <parameter value>
  ...
end

Yields:



34
35
36
# File 'lib/openstax_accounts.rb', line 34

def configure
  yield configuration
end

.create_group(account, group, version = DEFAULT_API_VERSION) ⇒ Object

Creates a group in the Accounts server. The given account will be the owner of the group. Also takes an optional API version parameter. API version currently defaults to :v1 (may change in the future). On failure, throws an Exception, just like api_call. On success, returns an OAuth2::Response object.



235
236
237
238
239
240
241
242
243
# File 'lib/openstax_accounts.rb', line 235

def create_group(, group, version = DEFAULT_API_VERSION)
  options = {:access_token => .access_token,
             :api_version => version,
             :body => group.attributes.slice('name', 'is_public').to_json}
  response = ActiveSupport::JSON.decode(api_call(
               :post, 'groups', options).body)
  group.openstax_uid = response['id']
  response
end

.create_group_member(account, group_member, version = DEFAULT_API_VERSION) ⇒ Object

Creates a group_member in the Accounts server. The given account must own the group. Also takes an optional API version parameter. API version currently defaults to :v1 (may change in the future). On failure, throws an Exception, just like api_call. On success, returns an OAuth2::Response object.



276
277
278
279
280
281
282
# File 'lib/openstax_accounts.rb', line 276

def create_group_member(, group_member, version = DEFAULT_API_VERSION)
  options = {:access_token => .access_token,
             :api_version => version}
  api_call(:post,
           "groups/#{group_member.group_id}/members/#{group_member.user_id}",
           options)
end

.create_group_nesting(account, group_nesting, version = DEFAULT_API_VERSION) ⇒ Object

Creates a group_nesting in the Accounts server. The given account must own both groups. Also takes an optional API version parameter. API version currently defaults to :v1 (may change in the future). On failure, throws an Exception, just like api_call. On success, returns an OAuth2::Response object.



332
333
334
335
336
337
338
# File 'lib/openstax_accounts.rb', line 332

def create_group_nesting(, group_nesting, version = DEFAULT_API_VERSION)
  options = {:access_token => .access_token,
             :api_version => version}
  api_call(:post,
           "groups/#{group_nesting.container_group_id}/nestings/#{group_nesting.member_group_id}",
           options)
end

.create_group_owner(account, group_owner, version = DEFAULT_API_VERSION) ⇒ Object

Creates a group_owner in the Accounts server. The given account must own the group. Also takes an optional API version parameter. API version currently defaults to :v1 (may change in the future). On failure, throws an Exception, just like api_call. On success, returns an OAuth2::Response object.



304
305
306
307
308
309
310
# File 'lib/openstax_accounts.rb', line 304

def create_group_owner(, group_owner, version = DEFAULT_API_VERSION)
  options = {:access_token => .access_token,
             :api_version => version}
  api_call(:post,
           "groups/#{group_owner.group_id}/owners/#{group_owner.user_id}",
           options)
end

.destroy_group(account, group, version = DEFAULT_API_VERSION) ⇒ Object

Deletes a group from the Accounts server. The given account must own the group. Also takes an optional API version parameter. API version currently defaults to :v1 (may change in the future). On failure, throws an Exception, just like api_call. On success, returns an OAuth2::Response object.



264
265
266
267
268
# File 'lib/openstax_accounts.rb', line 264

def destroy_group(, group, version = DEFAULT_API_VERSION)
  options = {:access_token => .access_token,
             :api_version => version}
  api_call(:delete, "groups/#{group.openstax_uid}", options)
end

.destroy_group_member(account, group_member, version = DEFAULT_API_VERSION) ⇒ Object

Deletes a group_member from the Accounts server. The given account must own the group. Also takes an optional API version parameter. API version currently defaults to :v1 (may change in the future). On failure, throws an Exception, just like api_call. On success, returns an OAuth2::Response object.



290
291
292
293
294
295
296
# File 'lib/openstax_accounts.rb', line 290

def destroy_group_member(, group_member, version = DEFAULT_API_VERSION)
  options = {:access_token => .access_token,
             :api_version => version}
  api_call(:delete,
           "groups/#{group_member.group_id}/members/#{group_member.user_id}",
           options)
end

.destroy_group_nesting(account, group_nesting, version = DEFAULT_API_VERSION) ⇒ Object

Deletes a group_nesting from the Accounts server. The given account must own either group. Also takes an optional API version parameter. API version currently defaults to :v1 (may change in the future). On failure, throws an Exception, just like api_call. On success, returns an OAuth2::Response object.



346
347
348
349
350
351
352
# File 'lib/openstax_accounts.rb', line 346

def destroy_group_nesting(, group_nesting, version = DEFAULT_API_VERSION)
  options = {:access_token => .access_token,
             :api_version => version}
  api_call(:delete,
           "groups/#{group_nesting.container_group_id}/nestings/#{group_nesting.member_group_id}",
           options)
end

.destroy_group_owner(account, group_owner, version = DEFAULT_API_VERSION) ⇒ Object

Deletes a group_owner from the Accounts server. The given account must own the group. Also takes an optional API version parameter. API version currently defaults to :v1 (may change in the future). On failure, throws an Exception, just like api_call. On success, returns an OAuth2::Response object.



318
319
320
321
322
323
324
# File 'lib/openstax_accounts.rb', line 318

def destroy_group_owner(, group_owner, version = DEFAULT_API_VERSION)
  options = {:access_token => .access_token,
             :api_version => version}
  api_call(:delete,
           "groups/#{group_owner.group_id}/owners/#{group_owner.user_id}",
           options)
end

.get_application_account_updates(version = DEFAULT_API_VERSION) ⇒ Object

Retrieves information about accounts that have been recently updated. Results are limited to accounts that have used the current app. On failure, throws an Exception, just like api_call. On success, returns an OAuth2::Response object.



186
187
188
189
# File 'lib/openstax_accounts.rb', line 186

def (version = DEFAULT_API_VERSION)
  options = {:api_version => version}
  api_call(:get, 'application_users/updates', options)
end

.get_application_group_updates(version = DEFAULT_API_VERSION) ⇒ Object

Retrieves information about groups that have been recently updated. Results are limited to groups that users of the current app have access to. On failure, throws an Exception, just like api_call. On success, returns an OAuth2::Response object.



210
211
212
213
# File 'lib/openstax_accounts.rb', line 210

def get_application_group_updates(version = DEFAULT_API_VERSION)
  options = {:api_version => version}
  api_call(:get, 'application_groups/updates', options)
end

.mark_account_updates_as_read(application_users, version = DEFAULT_API_VERSION) ⇒ 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. On failure, throws an Exception, just like api_call. On success, returns an OAuth2::Response object.



199
200
201
202
203
# File 'lib/openstax_accounts.rb', line 199

def (application_users, version = DEFAULT_API_VERSION)
  options = {:api_version => version,
             :body => application_users.to_json}
  api_call(:put, 'application_users/updated', options)
end

.mark_group_updates_as_read(application_groups, version = DEFAULT_API_VERSION) ⇒ 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. On failure, throws an Exception, just like api_call. On success, returns an OAuth2::Response object.



223
224
225
226
227
# File 'lib/openstax_accounts.rb', line 223

def mark_group_updates_as_read(application_groups, version = DEFAULT_API_VERSION)
  options = {:api_version => version,
             :body => application_groups.to_json}
  api_call(:put, 'application_groups/updated', options)
end

.search_accounts(query, version = DEFAULT_API_VERSION) ⇒ Object

Performs an account search in the Accounts server. Results are limited to 10 accounts maximum. Takes a query parameter and an optional API version parameter. API version currently defaults to :v1 (may change in the future). On failure, throws an Exception, just like api_call. On success, returns an OAuth2::Response object.



149
150
151
152
153
# File 'lib/openstax_accounts.rb', line 149

def search_accounts(query, version = DEFAULT_API_VERSION)
  options = {:params => {:q => query},
             :api_version => version}
  api_call(:get, 'users', options)
end

.search_application_accounts(query, version = DEFAULT_API_VERSION) ⇒ 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 optional API version parameter. API version currently defaults to :v1 (may change in the future). On failure, throws an Exception, just like api_call. On success, returns an OAuth2::Response object.



175
176
177
178
179
# File 'lib/openstax_accounts.rb', line 175

def search_application_accounts(query, version = DEFAULT_API_VERSION)
  options = {:params => {:q => query},
             :api_version => version}
  api_call(:get, 'application_users', options)
end

.update_account(account, version = DEFAULT_API_VERSION) ⇒ Object

Updates a user account in the Accounts server. The account is determined by the OAuth access token. Also takes an optional API version parameter. API version currently defaults to :v1 (may change in the future). On failure, throws an Exception, just like api_call. On success, returns an OAuth2::Response object.



161
162
163
164
165
166
167
# File 'lib/openstax_accounts.rb', line 161

def (, version = DEFAULT_API_VERSION)
  options = {:access_token => .access_token,
             :api_version => version,
             :body => .attributes.slice('username', 'first_name',
                        'last_name', 'full_name', 'title').to_json}
  api_call(:put, 'user', options)
end

.update_group(account, group, version = DEFAULT_API_VERSION) ⇒ Object

Updates a group in the Accounts server. The given account must own the group. Also takes an optional API version parameter. API version currently defaults to :v1 (may change in the future). On failure, throws an Exception, just like api_call. On success, returns an OAuth2::Response object.



251
252
253
254
255
256
# File 'lib/openstax_accounts.rb', line 251

def update_group(, group, version = DEFAULT_API_VERSION)
  options = {:access_token => .access_token,
             :api_version => version,
             :body => group.attributes.slice('name', 'is_public').to_json}
  api_call(:put, "groups/#{group.openstax_uid}", options)
end