Class: HighLevel::Storage::Mongo
- Defined in:
- lib/high_level/storage/mongo.rb
Overview
MongoDB-backed session storage. Maintains behavioral parity with the TS SDK’s ‘MongoSessionStorage` (collection name and document shape) so a TS-Ruby polyglot deployment can share a single Mongo store. See vendor/highlevel-api-sdk/lib/storage/mongodb-session-storage.ts.
Sessions live in ‘gohighlevel_sessions` keyed by a compound `resource_id` unique index. `expire_at` carries a TTL index so MongoDB auto-purges expired sessions.
Constant Summary collapse
- COLLECTION =
The MongoDB collection name; matches the TS SDK for cross-SDK store sharing.
"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(client: nil, **client_options) ⇒ Mongo
constructor
A new instance of Mongo.
-
#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(client: nil, **client_options) ⇒ Mongo
Returns a new instance of Mongo.
23 24 25 26 27 28 29 |
# File 'lib/high_level/storage/mongo.rb', line 23 def initialize(client: nil, **) super() @client = client || ::Mongo::Client.new([:url] || "mongodb://localhost:27017/gohighlevel", **.except(:url)) @client_id = nil @indexes_ensured = false end |
Instance Method Details
#delete_session(resource_id) ⇒ void
This method returns an undefined value.
Remove a stored session.
84 85 86 87 |
# File 'lib/high_level/storage/mongo.rb', line 84 def delete_session(resource_id) collection.delete_one(application_id: application_id, resource_id: resource_id) nil end |
#disconnect ⇒ void
This method returns an undefined value.
Release any resources held by the backend.
37 38 39 |
# File 'lib/high_level/storage/mongo.rb', line 37 def disconnect @client.close end |
#get_access_token(resource_id) ⇒ String?
Fetch just the access token from a stored session.
78 79 80 81 |
# File 'lib/high_level/storage/mongo.rb', line 78 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.
70 71 72 73 74 75 |
# File 'lib/high_level/storage/mongo.rb', line 70 def get_session(resource_id) doc = collection.find(application_id: application_id, resource_id: resource_id).first return nil if doc.nil? symbolize_keys(doc).except(:_id, :application_id, :resource_id, :created_at, :updated_at) end |
#init ⇒ void
This method returns an undefined value.
Initialize the backend (open connections, ensure schema, …).
32 33 34 |
# File 'lib/high_level/storage/mongo.rb', line 32 def init ensure_indexes! 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.
42 43 44 45 46 |
# File 'lib/high_level/storage/mongo.rb', line 42 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. rubocop:disable Metrics/MethodLength
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/high_level/storage/mongo.rb', line 50 def set_session(resource_id, session_data) ensure_indexes! data = symbolize_keys(session_data) document = data.merge( application_id: application_id, resource_id: resource_id, expire_at: Time.at(calculate_expire_at(data[:expires_in]) / 1000.0), updated_at: Time.now ) collection.update_one( { application_id: application_id, resource_id: resource_id }, { "$set" => document, "$setOnInsert" => { created_at: Time.now } }, upsert: true ) nil end |