Class: Source::GcpStorageRepository

Inherits:
Object
  • Object
show all
Includes:
Repository
Defined in:
lib/gcp_storage_repository.rb

Instance Attribute Summary collapse

Attributes included from Repository

#data, #lock, #name

Instance Method Summary collapse

Methods included from Repository

#get_data, #get_name

Constructor Details

#initialize(name:, bucket_name:, object_name:) ⇒ GcpStorageRepository

Returns a new instance of GcpStorageRepository.



14
15
16
17
18
19
# File 'lib/gcp_storage_repository.rb', line 14

def initialize(name:, bucket_name:, object_name:)
  super(name: name)
  @bucket_name = bucket_name
  @object_name = object_name
  @client = nil
end

Instance Attribute Details

#bucket_nameObject (readonly)

Returns the value of attribute bucket_name.



12
13
14
# File 'lib/gcp_storage_repository.rb', line 12

def bucket_name
  @bucket_name
end

#clientObject (readonly)

Returns the value of attribute client.



12
13
14
# File 'lib/gcp_storage_repository.rb', line 12

def client
  @client
end

#object_nameObject (readonly)

Returns the value of attribute object_name.



12
13
14
# File 'lib/gcp_storage_repository.rb', line 12

def object_name
  @object_name
end

Instance Method Details

#refreshObject

Refresh reads the YAML file from the GCS bucket, unmarshal it into the data map.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/gcp_storage_repository.rb', line 22

def refresh
  @lock.synchronize do
    # If the GCS client does not exist, create it.
    if @client.nil?
      @client = Google::Cloud::Storage.new()
    end

    # Open the YAML file from the GCS bucket.
    bucket = @client.bucket(@bucket_name)
    file = bucket.file(@object_name)
    file_content = file.download

    # Unmarshal the YAML data into the data map.
    @data = YAML.safe_load(file_content)
    @raw_data = file_content
  end
rescue StandardError => e
  raise "Error refreshing configuration data: #{e.message}"
end