Module: OpenStax::Accounts

Defined in:
lib/openstax_accounts.rb,
lib/openstax/accounts/engine.rb,
lib/openstax/accounts/version.rb,
app/models/openstax/accounts/user.rb,
lib/openstax/accounts/action_list.rb,
lib/openstax/accounts/route_helper.rb,
lib/openstax/accounts/user_provider.rb,
lib/openstax/accounts/current_user_manager.rb,
app/routines/openstax/accounts/search_users.rb,
app/routines/openstax/accounts/dev/create_user.rb,
app/handlers/openstax/accounts/dev/users_search.rb,
app/helpers/openstax/accounts/application_helper.rb,
app/controllers/openstax/accounts/dev/dev_controller.rb,
app/controllers/openstax/accounts/sessions_controller.rb,
app/controllers/openstax/accounts/dev/users_controller.rb,
app/controllers/openstax/accounts/application_controller.rb,
app/representers/openstax/accounts/api/v1/user_representer.rb,
app/handlers/openstax/accounts/sessions_omniauth_authenticated.rb,
app/representers/openstax/accounts/api/v1/user_search_representer.rb,
app/representers/openstax/accounts/api/v1/contact_info_representer.rb,
app/representers/openstax/accounts/api/v1/email_address_representer.rb,
app/representers/openstax/accounts/api/v1/application_user_representer.rb

Defined Under Namespace

Modules: Api, ApplicationHelper, Dev Classes: ActionList, ApplicationController, Configuration, CurrentUserManager, Engine, RouteHelper, SearchUsers, SessionsController, SessionsOmniauthAuthenticated, User, UserProvider

Constant Summary collapse

VERSION =
"0.3"

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.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/openstax_accounts.rb', line 90

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

Set enable_stubbing to true iff you want this engine to fake all

interaction with the accounts site.

Yields:



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

def configure
  yield configuration
end

.create_application_user(user, version = :v1) ⇒ Object

Creates an ApplicationUser in Accounts for the configured app and the given OpenStax::Accounts::User. Also takes an optional API version parameter. Defaults to :v1. On failure, throws an Exception, just like api_call. On success, returns an OAuth2::Response object.



111
112
113
114
115
# File 'lib/openstax_accounts.rb', line 111

def create_application_user(user, version = :v1)
  options = {:access_token => user.access_token,
             :api_version => version}
  api_call(:post, 'application_users', options)
end

.user_search(query, version = :v1) ⇒ Object

Performs a user search in Accounts for the configured app. Takes a query parameter and an optional API version parameter. API version currently defaults to :v1. On failure, throws an Exception, just like api_call. On success, returns an OAuth2::Response object.



122
123
124
125
126
# File 'lib/openstax_accounts.rb', line 122

def user_search(query, version = :v1)
  options = {:params => {:q => query},
             :api_version => version}
  api_call(:get, 'users/search', options)
end