Class: Applicaster::Accounts
- Inherits:
-
Object
- Object
- Applicaster::Accounts
show all
- Defined in:
- lib/applicaster/accounts.rb,
lib/applicaster/accounts/user.rb,
lib/applicaster/accounts/account.rb,
lib/applicaster/accounts/configuration.rb
Defined Under Namespace
Classes: Account, Configuration, User
Constant Summary
collapse
- RETRYABLE_STATUS_CODES =
[500, 503, 502]
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.accounts_from_token(token) ⇒ Object
62
63
64
65
66
67
|
# File 'lib/applicaster/accounts.rb', line 62
def accounts_from_token(token)
connection(token: token)
.get("/api/v1/accounts.json")
.body
.map {|a| Account.new(a) }
end
|
.config ⇒ Object
69
70
71
|
# File 'lib/applicaster/accounts.rb', line 69
def config
@config ||= Configuration.new
end
|
73
74
75
|
# File 'lib/applicaster/accounts.rb', line 73
def configure
yield config
end
|
.connection(options = {}) ⇒ Object
14
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
|
# File 'lib/applicaster/accounts.rb', line 14
def connection(options = {})
conn_opts = {
url: config.base_url,
request: { timeout: config.timeout }
}
Faraday.new(conn_opts) do |conn|
if options[:token]
conn.request :oauth2, options[:token]
end
conn.request :json
conn.request :retry,
max: config.retries,
interval: 0.05,
backoff_factor: 2,
exceptions: [Faraday::ClientError, Faraday::TimeoutError],
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 :raise_error
conn.adapter Faraday.default_adapter
end
end
|
.oauth_client(config = config) ⇒ Object
77
78
79
80
81
82
83
84
|
# File 'lib/applicaster/accounts.rb', line 77
def oauth_client(config = config)
::OAuth2::Client.new(
config.client_id,
config.client_secret,
site: config.base_url,
authorize_url: "/oauth/authorize",
)
end
|
.user_from_token(token) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/applicaster/accounts.rb', line 48
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[:status] == 401
nil
else
raise
end
end
|
Instance Method Details
#accounts ⇒ Object
91
92
93
|
# File 'lib/applicaster/accounts.rb', line 91
def accounts
self.class.accounts_from_token(client_credentials_token.token)
end
|
#connection(*args) ⇒ Object
95
96
97
|
# File 'lib/applicaster/accounts.rb', line 95
def connection(*args)
self.class.connection(*args)
end
|
#user_data_from_omniauth(omniauth_credentials) ⇒ Object
87
88
89
|
# File 'lib/applicaster/accounts.rb', line 87
def user_data_from_omniauth(omniauth_credentials)
access_token(omniauth_credentials).get("/api/v1/users/current.json").parsed
end
|