Class: HighLevel::Storage::Mongo

Inherits:
Base
  • Object
show all
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

Base::DEFAULT_EXPIRY_MS

Instance Method Summary collapse

Constructor Details

#initialize(client: nil, **client_options) ⇒ Mongo

Returns a new instance of Mongo.

Parameters:

  • client (::Mongo::Client, nil) (defaults to: nil)

    an existing client, or nil to build one from client_options

  • client_options (Hash)

    :url plus any ::Mongo::Client options



23
24
25
26
27
28
29
# File 'lib/high_level/storage/mongo.rb', line 23

def initialize(client: nil, **client_options)
  super()
  @client = client || ::Mongo::Client.new(client_options[:url] || "mongodb://localhost:27017/gohighlevel",
                                          **client_options.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.

Parameters:

  • resource_id (String)

    a company or location id



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

#disconnectvoid

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.

Parameters:

  • resource_id (String)

    a company or location id

Returns:

  • (String, nil)


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.

Parameters:

  • resource_id (String)

    a company or location id

Returns:

  • (Hash, nil)

    the session data, or nil when absent



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

#initvoid

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.

Parameters:

  • client_id (String)

Raises:

  • (ArgumentError)


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

Parameters:

  • resource_id (String)

    a company or location id

  • session_data (Hash)

    the session payload



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