Class: Applicaster::Accounts
- Inherits:
-
Object
- Object
- Applicaster::Accounts
- Defined in:
- lib/applicaster/accounts.rb,
lib/applicaster/accounts/user.rb,
lib/applicaster/accounts/account.rb,
lib/applicaster/accounts/permission.rb,
lib/applicaster/accounts/configuration.rb
Defined Under Namespace
Classes: Account, Configuration, Permission, User
Constant Summary collapse
- RETRYABLE_STATUS_CODES =
[500, 502, 503, 504]
Class Method Summary collapse
- .accounts_from_token(token) ⇒ Object
- .config ⇒ Object
- .configure {|config| ... } ⇒ Object
- .connection(options = {}) ⇒ Object
- .oauth_client(config = config()) ⇒ Object
- .user_by_id_and_token(id, token) ⇒ Object
- .user_from_token(token) ⇒ Object
Instance Method Summary collapse
- #accounts ⇒ Object
- #connection(*args) ⇒ Object
- #find_user_by_id(id) ⇒ Object
- #user_data_from_omniauth(omniauth_credentials) ⇒ Object
Class Method Details
.accounts_from_token(token) ⇒ Object
71 72 73 74 75 76 |
# File 'lib/applicaster/accounts.rb', line 71 def accounts_from_token(token) connection(token: token) .get("/api/v1/accounts.json") .body .map {|a| Account.new(a) } end |
.config ⇒ Object
78 79 80 |
# File 'lib/applicaster/accounts.rb', line 78 def config @config ||= Configuration.new end |
.configure {|config| ... } ⇒ Object
82 83 84 |
# File 'lib/applicaster/accounts.rb', line 82 def configure yield config end |
.connection(options = {}) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/applicaster/accounts.rb', line 15 def connection( = {}) conn_opts = { url: config.base_url, request: { timeout: config.timeout } } Faraday.new(conn_opts) do |conn| if [:token] conn.request :oauth2, [:token], token_type: 'param' end conn.request :json conn.request :retry, max: config.retries, interval: 0.05, backoff_factor: 2, exceptions: [Faraday::ClientError, Faraday::TimeoutError, Faraday::ConnectionFailed], methods: [], retry_if: ->(env, exception) { env[:method] == :get && (exception.is_a?(Faraday::TimeoutError) || RETRYABLE_STATUS_CODES.include?(env[:status])) } conn.response :json, content_type: /\bjson$/ # conn.response :logger, Rails.logger # conn.response :logger, Logger.new(STDOUT) conn.response :raise_error conn.adapter config.faraday_adapter end end |
.oauth_client(config = config()) ⇒ Object
86 87 88 89 90 91 92 93 94 |
# File 'lib/applicaster/accounts.rb', line 86 def oauth_client(config = config()) ::OAuth2::Client.new( config.client_id, config.client_secret, site: config.base_url, authorize_url: "/oauth/authorize", auth_scheme: :basic_auth, ) end |
.user_by_id_and_token(id, token) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/applicaster/accounts.rb', line 63 def user_by_id_and_token(id, token) Applicaster::Accounts::User.new( connection(token: token) .get("/api/v1/users/#{id}.json") .body ) end |
.user_from_token(token) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/applicaster/accounts.rb', line 49 def user_from_token(token) Applicaster::Accounts::User.new( connection(token: token) .get("/api/v1/users/current.json") .body ) rescue Faraday::ClientError => e if e.response && e.response[:status] == 401 nil else raise end end |
Instance Method Details
#accounts ⇒ Object
101 102 103 |
# File 'lib/applicaster/accounts.rb', line 101 def accounts self.class.accounts_from_token(client_credentials_token.token) end |
#connection(*args) ⇒ Object
111 112 113 |
# File 'lib/applicaster/accounts.rb', line 111 def connection(*args) self.class.connection(*args) end |
#find_user_by_id(id) ⇒ Object
105 106 107 108 109 |
# File 'lib/applicaster/accounts.rb', line 105 def find_user_by_id(id) self.class.user_by_id_and_token(id, client_credentials_token.token) rescue Faraday::ResourceNotFound nil end |
#user_data_from_omniauth(omniauth_credentials) ⇒ Object
97 98 99 |
# File 'lib/applicaster/accounts.rb', line 97 def user_data_from_omniauth(omniauth_credentials) access_token(omniauth_credentials).get("/api/v1/users/current.json").parsed end |