Class: HighLevel::Storage::ActiveRecord

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

Base::DEFAULT_EXPIRY_MS

Instance Method Summary collapse

Constructor Details

#initialize(model: Session) ⇒ ActiveRecord

Returns a new instance of ActiveRecord.

Parameters:

  • model (Class) (defaults to: Session)

    the ActiveRecord model class to persist through



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.

Parameters:

  • resource_id (String)

    a company or location id



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

#disconnectvoid

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.

Parameters:

  • resource_id (String)

    a company or location id

Returns:

  • (String, nil)


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.

Parameters:

  • resource_id (String)

    a company or location id

Returns:

  • (Hash, nil)

    the session data, or nil when absent



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

#initvoid

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.

Parameters:

  • client_id (String)

Raises:

  • (ArgumentError)


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.

Parameters:

  • resource_id (String)

    a company or location id

  • session_data (Hash)

    the session payload



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