Class: HighLevel::Storage::ActiveRecord
- Defined in:
- lib/high_level/storage/active_record.rb
Overview
ActiveRecord-backed session storage. Designed for Rails apps with an existing database connection — the gem ships the model class plus a migration helper; the user adds the migration to their ‘db/migrate/` directory and runs `rake db:migrate`.
Schema:
gohighlevel_sessions(application_id, resource_id, payload TEXT,
expire_at DATETIME, created_at, updated_at)
with a unique index on (application_id, resource_id).
Defined Under Namespace
Modules: Migration Classes: Session
Constant Summary collapse
- TABLE_NAME =
The backing table name.
"gohighlevel_sessions"
Constants inherited from Base
Instance Method Summary collapse
-
#delete_session(resource_id) ⇒ void
Remove a stored session.
-
#disconnect ⇒ void
Release any resources held by the backend.
-
#get_access_token(resource_id) ⇒ String?
Fetch just the access token from a stored session.
-
#get_session(resource_id) ⇒ Hash?
Fetch a stored session.
-
#init ⇒ void
Initialize the backend (open connections, ensure schema, …).
-
#initialize(model: Session) ⇒ ActiveRecord
constructor
A new instance of ActiveRecord.
-
#set_client_id(client_id) ⇒ void
Set the OAuth client id; its first hyphen-separated segment becomes the application namespace for stored sessions.
-
#set_session(resource_id, session_data) ⇒ void
Store (or replace) a session, stamping
:expire_at.
Constructor Details
#initialize(model: Session) ⇒ ActiveRecord
Returns a new instance of ActiveRecord.
51 52 53 54 55 |
# File 'lib/high_level/storage/active_record.rb', line 51 def initialize(model: Session) super() @model = model @client_id = nil end |
Instance Method Details
#delete_session(resource_id) ⇒ void
This method returns an undefined value.
Remove a stored session.
101 102 103 104 |
# File 'lib/high_level/storage/active_record.rb', line 101 def delete_session(resource_id) @model.where(application_id: application_id, resource_id: resource_id).delete_all nil end |
#disconnect ⇒ void
This method returns an undefined value.
Release any resources held by the backend.
63 64 65 |
# File 'lib/high_level/storage/active_record.rb', line 63 def disconnect # No-op — AR pools are managed by the host app. end |
#get_access_token(resource_id) ⇒ String?
Fetch just the access token from a stored session.
95 96 97 98 |
# File 'lib/high_level/storage/active_record.rb', line 95 def get_access_token(resource_id) session = get_session(resource_id) session && session[:access_token] end |
#get_session(resource_id) ⇒ Hash?
Fetch a stored session.
87 88 89 90 91 92 |
# File 'lib/high_level/storage/active_record.rb', line 87 def get_session(resource_id) row = @model.find_by(application_id: application_id, resource_id: resource_id) return nil if row.nil? JSON.parse(row.payload, symbolize_names: true) end |
#init ⇒ void
This method returns an undefined value.
Initialize the backend (open connections, ensure schema, …).
58 59 60 |
# File 'lib/high_level/storage/active_record.rb', line 58 def init # No-op — assumes the user has run the migration. end |
#set_client_id(client_id) ⇒ void
This method returns an undefined value.
Set the OAuth client id; its first hyphen-separated segment becomes the application namespace for stored sessions.
68 69 70 71 72 |
# File 'lib/high_level/storage/active_record.rb', line 68 def set_client_id(client_id) raise ArgumentError, "client_id is required" if client_id.nil? || client_id.to_s.empty? @client_id = client_id.to_s end |
#set_session(resource_id, session_data) ⇒ void
This method returns an undefined value.
Store (or replace) a session, stamping :expire_at.
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/high_level/storage/active_record.rb', line 75 def set_session(resource_id, session_data) data = symbolize_keys(session_data) document = data.merge(expire_at: calculate_expire_at(data[:expires_in])) record = @model.find_or_initialize_by(application_id: application_id, resource_id: resource_id) record.payload = JSON.generate(document) record.expire_at = Time.at(document[:expire_at] / 1000.0) record.save! nil end |