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
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/applicaster/accounts.rb', line 80 def accounts_from_token(token) Rails.logger.info("Fetching accounts with token: #{token}") connection(token: token) .get("/api/v1/accounts.json") .body .map {|a| Account.new(a) } rescue Faraday::ClientError => e Rails.logger.error("Failed to fetch accounts. Token: #{token}, Error: #{e.}") raise end |
.config ⇒ Object
91 92 93 |
# File 'lib/applicaster/accounts.rb', line 91 def config @config ||= Configuration.new end |
.configure {|config| ... } ⇒ Object
95 96 97 |
# File 'lib/applicaster/accounts.rb', line 95 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 |
# 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 :raise_error # conn.response :logger, Rails.logger # conn.response :logger, Logger.new(STDOUT) conn.adapter config.faraday_adapter end end |
.oauth_client(config = config()) ⇒ Object
99 100 101 102 103 104 105 106 107 |
# File 'lib/applicaster/accounts.rb', line 99 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
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/applicaster/accounts.rb', line 69 def user_by_id_and_token(id, token) Applicaster::Accounts::User.new( connection(token: token) .get("/api/v1/users/#{id}.json") .body ) rescue Faraday::ResourceNotFound Rails.logger.error("[Login Failed] - User not found. ID: #{id}, Token: #{token}, IP: #{request.remote_ip}, User Agent: #{request.user_agent}") nil end |
.user_from_token(token) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/applicaster/accounts.rb', line 48 def user_from_token(token) Rails.logger.info("Fetching user with token: #{token}") user = Applicaster::Accounts::User.new( connection(token: token) .get("/api/v1/users/current.json") .body ) if user.nil? Rails.logger.error("[Login Failed] - User fetch failed. Token: #{token}, IP: #{request.remote_ip}, User Agent: #{request.user_agent}") end user rescue Faraday::ClientError => e if e.response && e.response[:status] == 401 Rails.logger.error("[Login Failed] - Unauthorized access attempt detected. Invalid token: #{token}, IP: #{request.remote_ip}, User Agent: #{request.user_agent}, Error: #{e.}") nil else Rails.logger.error("[Login Failed] - Error fetching user. Token: #{token}, IP: #{request.remote_ip}, User Agent: #{request.user_agent}, Error: #{e.}") raise end end |
Instance Method Details
#accounts ⇒ Object
117 118 119 |
# File 'lib/applicaster/accounts.rb', line 117 def accounts self.class.accounts_from_token(client_credentials_token.token) end |
#connection(*args) ⇒ Object
129 130 131 |
# File 'lib/applicaster/accounts.rb', line 129 def connection(*args) self.class.connection(*args) end |
#find_user_by_id(id) ⇒ Object
121 122 123 124 125 126 127 |
# File 'lib/applicaster/accounts.rb', line 121 def find_user_by_id(id) Rails.logger.info("Finding user by ID: #{id}") self.class.user_by_id_and_token(id, client_credentials_token.token) rescue Faraday::ResourceNotFound Rails.logger.error("[Login Failed] - User not found by ID: #{id}") nil end |
#user_data_from_omniauth(omniauth_credentials) ⇒ Object
110 111 112 113 114 115 |
# File 'lib/applicaster/accounts.rb', line 110 def user_data_from_omniauth(omniauth_credentials) access_token(omniauth_credentials).get("/api/v1/users/current.json").parsed rescue Faraday::ClientError => e Rails.logger.error("[Login Failed] - Failed to fetch user data from Omniauth. Error: #{e.}") raise end |