Class: CartBinaryUploader::Storage

Inherits:
Object
  • Object
show all
Defined in:
lib/storage/storage_type.rb

Direct Known Subclasses

GoogleCloudStorage, S3CloudStorage

Constant Summary collapse

FRAMEWORK_EXTENSION_NAME =
'.framework'.freeze
JSON_EXTENSION_NAME =
'.json'.freeze
JSON_EXTENSION_ZIP =
'.zip'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bucket_name, framework_name, framework_version) ⇒ Storage

Returns a new instance of Storage.



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/storage/storage_type.rb', line 19

def initialize(bucket_name,
               framework_name,
               framework_version)
  @bucket_name = bucket_name
  @framework_name = framework_name
  @framework_version = framework_version

  CartLogger.log_info "Creating storage"
  create_storage
  CartLogger.log_info "Creating bucket name: #{@bucket_name}"
  create_bucket
end

Instance Attribute Details

#bucketObject

Returns the value of attribute bucket.



15
16
17
# File 'lib/storage/storage_type.rb', line 15

def bucket
  @bucket
end

#bucket_nameObject

Returns the value of attribute bucket_name.



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

def bucket_name
  @bucket_name
end

#bucket_objectObject

Returns the value of attribute bucket_object.



17
18
19
# File 'lib/storage/storage_type.rb', line 17

def bucket_object
  @bucket_object
end

#framework_nameObject

Returns the value of attribute framework_name.



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

def framework_name
  @framework_name
end

#framework_versionObject

Returns the value of attribute framework_version.



14
15
16
# File 'lib/storage/storage_type.rb', line 14

def framework_version
  @framework_version
end

#json_fileObject

Returns the value of attribute json_file.



16
17
18
# File 'lib/storage/storage_type.rb', line 16

def json_file
  @json_file
end

Instance Method Details

#copy_local_file(framework_name_source, framework_name_destination) ⇒ Object



105
106
107
# File 'lib/storage/storage_type.rb', line 105

def copy_local_file(framework_name_source, framework_name_destination)
  FileUtils.copy_file(framework_name_source, framework_name_destination)
end

#create_bucketObject



36
37
38
# File 'lib/storage/storage_type.rb', line 36

def create_bucket
  throw :not_implemented, "The current method not implemented"
end

#create_empty_json_file(file_name) ⇒ Object



125
126
127
128
# File 'lib/storage/storage_type.rb', line 125

def create_empty_json_file(file_name)
  json_object = JSON.parse('{}')
  save_json_object(file_name, json_object)
end

#create_file(file_path) ⇒ Object



81
82
83
# File 'lib/storage/storage_type.rb', line 81

def create_file(file_path)
  throw :not_implemented, "The current method not implemented"
end

#create_local_file(file_name) ⇒ Object



89
90
91
# File 'lib/storage/storage_type.rb', line 89

def create_local_file(file_name)
  throw :not_implemented, "The current method not implemented"
end

#create_public_url(file_path) ⇒ Object



85
86
87
# File 'lib/storage/storage_type.rb', line 85

def create_public_url(file_path)
  throw :not_implemented, "The current method not implemented"
end

#create_storageObject



32
33
34
# File 'lib/storage/storage_type.rb', line 32

def create_storage
  throw :not_implemented, "The current method not implemented"
end

#download_config_json_file(from_file) ⇒ Object



97
98
99
# File 'lib/storage/storage_type.rb', line 97

def download_config_json_file(from_file)
  throw :not_implemented, "The current method not implemented"
end

#file_on_storage_cloud(file) ⇒ Object



93
94
95
# File 'lib/storage/storage_type.rb', line 93

def file_on_storage_cloud(file)
  throw :not_implemented, "The current method not implemented"
end

#load_json_object(json_path) ⇒ Object



109
110
111
112
113
114
115
116
# File 'lib/storage/storage_type.rb', line 109

def load_json_object(json_path)
  CartLogger.log_info 'Loading JSON file'
  json = File.read(json_path)
  object = JSON.parse(json)
  CartLogger.log_info object
  CartLogger.log_info 'JSON Loaded'
  object
end

#save_json_object(json_path, json_object) ⇒ Object



118
119
120
121
122
123
# File 'lib/storage/storage_type.rb', line 118

def save_json_object(json_path, json_object)
  binary_json = JSON.pretty_generate(json_object)
  CartLogger.log_info "Saving JSON Object in: #{json_path} JSON: #{binary_json}"
  File.write(json_path, binary_json)
  CartLogger.log_info 'JSON Saved'
end

#upload_frameworkObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/storage/storage_type.rb', line 40

def upload_framework
  CartLogger.log_info "Prepering to upload file to cloud"
  framework_name_source = @framework_name + FRAMEWORK_EXTENSION_NAME + JSON_EXTENSION_ZIP
  framework_name_destination = @framework_name + FRAMEWORK_EXTENSION_NAME + "." + @framework_version + JSON_EXTENSION_ZIP
  json_path = @framework_name + JSON_EXTENSION_NAME

  CartLogger.log_info "Framework Source: #{framework_name_source}"
  CartLogger.log_info "Framework Destination: #{framework_name_destination}"
  CartLogger.log_info "JSON Path: #{json_path}"

  CartLogger.log_info "Verifying if the version file #{framework_name_destination} already exists"
  unless file_on_storage_cloud framework_name_destination
    CartLogger.log_info "File version #{@framework_version} not exists yet, starting generate file on cloud"

    if file_on_storage_cloud(json_path)
      @json_file = download_config_json_file(json_path)
      if @json_file.nil?
        throw :could_not_download_json_file, "JSON With name: #{json_path}"
      end
    else
      CartLogger.log_warn "Creating empty json because it not exsits yet on cloud"
      create_empty_json_file(json_path)
    end

    copy_local_file(framework_name_source, framework_name_destination)

    shared_url = create_file(framework_name_destination)

    json_object = load_json_object json_path
    json_object[@framework_version] = shared_url

    save_json_object(json_path, json_object)

    CartLogger.log_info 'Starting upload file to storage cloud'
    upload_json json_path
    CartLogger.log_info 'Uploaded complete'
  else
    throw :the_version_file_already_exists, "The current version: #{@framework_version} already exists on cloud"
  end
end

#upload_json(jsonPath) ⇒ Object



101
102
103
# File 'lib/storage/storage_type.rb', line 101

def upload_json jsonPath
  throw :not_implemented, "The current method not implemented"
end