Class: FirestoreClient

Inherits:
Object
  • Object
show all
Includes:
FirestoreSeeder, InventoryData
Defined in:
lib/firestore_client.rb

Overview

Client for interacting with Google Cloud Firestore for inventory management.

Constant Summary

Constants included from InventoryData

InventoryData::OLD_PRODUCTS, InventoryData::RECENT_PRODUCTS, InventoryData::RECENT_PRODUCTS_OUT_OF_STOCK

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from FirestoreSeeder

#seed

Constructor Details

#initialize ⇒ FirestoreClient

Returns a new instance of FirestoreClient.



15
16
17
18
19
20
21
22
23
24
# File 'lib/firestore_client.rb', line 15

def initialize
  # Assuming environment variables for auth are set or will be handled by library defaults
  @client = Google::Cloud::Firestore.new
  @db_running = true
  LOGGER.info 'Firestore client initialized' if defined?(LOGGER)
rescue StandardError => e
  LOGGER.error "Failed to initialize Firestore: #{e.message}" if defined?(LOGGER)
  @db_running = false
  @client = nil
end

Instance Attribute Details

#client ⇒ Object (readonly)

Returns the value of attribute client.



13
14
15
# File 'lib/firestore_client.rb', line 13

def client
  @client
end

#db_running ⇒ Object (readonly)

Returns the value of attribute db_running.



13
14
15
# File 'lib/firestore_client.rb', line 13

def db_running
  @db_running
end

Class Method Details

.instance ⇒ Object



26
27
28
# File 'lib/firestore_client.rb', line 26

def self.instance
  @instance ||= new
end

Instance Method Details

#product_by_id(id) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/firestore_client.rb', line 37

def product_by_id(id)
  return nil unless @db_running

  doc = @client.col('inventory').doc(id).get
  return nil unless doc.exists?

  doc_to_product(doc)
end

#products ⇒ Object



30
31
32
33
34
35
# File 'lib/firestore_client.rb', line 30

def products
  return [] unless @db_running

  products = @client.col('inventory').get
  products.map { |doc| doc_to_product(doc) }
end

#reset ⇒ Object



46
47
48
49
50
# File 'lib/firestore_client.rb', line 46

def reset
  return unless @db_running

  clean_firestore_collection
end