Class: Aspera::DataRepository
- Inherits:
-
Object
- Object
- Aspera::DataRepository
- Includes:
- Singleton
- Defined in:
- lib/aspera/data_repository.rb
Overview
a simple binary data repository
Constant Summary collapse
- ELEMENTS =
in same order as elements in folder
%i[dsa rsa uuid aspera.global-cli-client aspera.drive license]
Class Method Summary collapse
-
.instance ⇒ DataRepository
Returns the singleton instance of DataRepository.
Instance Method Summary collapse
-
#item(name) ⇒ String
decode data as expected as string.
Class Method Details
.instance ⇒ DataRepository
Returns the singleton instance of DataRepository
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/aspera/data_repository.rb', line 13 class DataRepository include Singleton # in same order as elements in folder ELEMENTS = %i[dsa rsa uuid aspera.global-cli-client aspera.drive license] START_INDEX = 1 DATA_FOLDER_NAME = 'data' # decode data as expected as string # @param name [Symbol] name of the data item # @return [String] decoded data def item(name) index = ELEMENTS.index(name) raise ParameterError, "Unknown data item #{name} (#{name.class})" unless index raw_data = data(START_INDEX + index) case name when :dsa, :rsa # generate PEM from DER return OpenSSL::PKey.const_get(name.to_s.upcase).new(raw_data).to_pem when :license return Zlib::Inflate.inflate(raw_data) when :uuid return format('%08x-%04x-%04x-%04x-%04x%08x', *raw_data.unpack('NnnnnN')) when :'aspera.global-cli-client', :'aspera.drive' return Base64.urlsafe_encode64(raw_data) else Aspera.error_unexpected_value(name) end end private private_constant :START_INDEX, :DATA_FOLDER_NAME # get binary value from data repository def data(id) File.read(File.join(__dir__, DATA_FOLDER_NAME, id.to_s), mode: 'rb') end end |
Instance Method Details
#item(name) ⇒ String
decode data as expected as string
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/aspera/data_repository.rb', line 24 def item(name) index = ELEMENTS.index(name) raise ParameterError, "Unknown data item #{name} (#{name.class})" unless index raw_data = data(START_INDEX + index) case name when :dsa, :rsa # generate PEM from DER return OpenSSL::PKey.const_get(name.to_s.upcase).new(raw_data).to_pem when :license return Zlib::Inflate.inflate(raw_data) when :uuid return format('%08x-%04x-%04x-%04x-%04x%08x', *raw_data.unpack('NnnnnN')) when :'aspera.global-cli-client', :'aspera.drive' return Base64.urlsafe_encode64(raw_data) else Aspera.error_unexpected_value(name) end end |