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

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



131
132
133
134
135
136
137
138
139
# File 'lib/openstax/accounts/api.rb', line 131

def self.create_group(, group, options = {})
  response = ActiveSupport::JSON.decode(
    (, :post, 'groups', options.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.



168
169
170
171
172
173
174
175
# File 'lib/openstax/accounts/api.rb', line 168

def self.create_group_member(, group_member, options = {})
  (
    ,
    :post,
    "groups/#{group_member.group_id}/members/#{group_member.user_id}",
    options
  )
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.



224
225
226
227
228
229
230
231
232
# File 'lib/openstax/accounts/api.rb', line 224

def self.create_group_nesting(, group_nesting, options = {})
  (
    ,
    :post,
    "groups/#{group_nesting.container_group_id}/nestings/#{
              group_nesting.member_group_id}",
    options
  )
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.



196
197
198
199
200
201
202
203
# File 'lib/openstax/accounts/api.rb', line 196

def self.create_group_owner(, group_owner, options = {})
  (
    ,
    :post,
    "groups/#{group_owner.group_id}/owners/#{group_owner.user_id}",
    options
  )
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.



159
160
161
# File 'lib/openstax/accounts/api.rb', line 159

def self.destroy_group(, group, options = {})
  (, :delete, "groups/#{group.openstax_uid}", options)
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.



182
183
184
185
186
187
188
189
# File 'lib/openstax/accounts/api.rb', line 182

def self.destroy_group_member(, group_member, options = {})
  (
    ,
    :delete,
    "groups/#{group_member.group_id}/members/#{group_member.user_id}",
    options
  )
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.



239
240
241
242
243
244
245
246
247
# File 'lib/openstax/accounts/api.rb', line 239

def self.destroy_group_nesting(, group_nesting, options = {})
  (
    ,
    :delete,
    "groups/#{group_nesting.container_group_id}/nestings/#{
              group_nesting.member_group_id}",
    options
  )
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.



210
211
212
213
214
215
216
217
# File 'lib/openstax/accounts/api.rb', line 210

def self.destroy_group_owner(, group_owner, options = {})
  (
    ,
    :delete,
    "groups/#{group_owner.group_id}/owners/#{group_owner.user_id}",
    options
  )
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.



253
254
255
# File 'lib/openstax/accounts/api.rb', line 253

def self.(attributes, options = {})
  request(:post, "user/find-or-create", options.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.



82
83
84
85
# File 'lib/openstax/accounts/api.rb', line 82

def self.(options = {})
  limit = OpenStax::Accounts.configuration.max_user_updates_per_request
  request(:get, "application_users/updates#{ '?limit=' + limit.to_s if !limit.blank? }", options)
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.



107
108
109
# File 'lib/openstax/accounts/api.rb', line 107

def self.get_application_group_updates(options = {})
  request(:get, 'application_groups/updates', options)
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.



96
97
98
# File 'lib/openstax/accounts/api.rb', line 96

def self.(application_users, options = {})
  request(:put, 'application_users/updated', options.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.



120
121
122
123
124
# File 'lib/openstax/accounts/api.rb', line 120

def self.mark_group_updates_as_read(application_groups, options = {})
  request(:put, 'application_groups/updated', options.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, options = {})
  version = options.delete(:api_version) || DEFAULT_API_VERSION
  unless version.blank?
    options[:headers] ||= {}
    options[:headers].merge!({
      'Accept' => "application/vnd.accounts.openstax.#{version.to_s}",
      'Content-Type' => 'application/json'
    })
  end

  token_string = options.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, options)
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.(, http_method, path, options = {})
  request(http_method, path,
          options.merge(access_token: .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, options = {})
  request(:get, 'users', options.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, options = {})
  request(:get, 'application_users', options.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.(, options = {})
  (
    , :put, 'user', options.merge(
      body: .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.



146
147
148
149
150
151
152
# File 'lib/openstax/accounts/api.rb', line 146

def self.update_group(, group, options = {})
  (, :put, "groups/#{group.openstax_uid}",
    options.merge(
      body: group.attributes.slice('name', 'is_public').to_json
    )
  )
end