Class: CartBinaryUploader::GoogleCloudStorage

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

Constant Summary collapse

FRAMEWORK_EXTENSION_NAME =
".framework"
JSON_EXTENSION_NAME =
".json"
JSON_EXTENSION_ZIP =
".zip"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(projectId, credentialsFile, bucketName, frameworkName, frameworkVersion) ⇒ GoogleCloudStorage

Returns a new instance of GoogleCloudStorage.



23
24
25
26
27
28
29
30
31
# File 'lib/google_cloud_storage.rb', line 23

def initialize( projectId, credentialsFile, bucketName, frameworkName, frameworkVersion )
  @projectId = projectId
  @credentialsFilePath = credentialsFile
  @bucketName = bucketName
  @frameworkName = frameworkName
  @frameworkVersion = frameworkVersion
  createStorage
  createBucket
end

Instance Attribute Details

#bucketObject

Returns the value of attribute bucket.



21
22
23
# File 'lib/google_cloud_storage.rb', line 21

def bucket
  @bucket
end

#bucketNameObject

Returns the value of attribute bucketName.



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

def bucketName
  @bucketName
end

#credentialsFilePathObject

Returns the value of attribute credentialsFilePath.



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

def credentialsFilePath
  @credentialsFilePath
end

#frameworkNameObject

Returns the value of attribute frameworkName.



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

def frameworkName
  @frameworkName
end

#frameworkVersionObject

Returns the value of attribute frameworkVersion.



18
19
20
# File 'lib/google_cloud_storage.rb', line 18

def frameworkVersion
  @frameworkVersion
end

#projectIdObject

Returns the value of attribute projectId.



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

def projectId
  @projectId
end

#storageObject

Returns the value of attribute storage.



20
21
22
# File 'lib/google_cloud_storage.rb', line 20

def storage
  @storage
end

Instance Method Details

#createBucketObject



41
42
43
44
# File 'lib/google_cloud_storage.rb', line 41

def createBucket
  CartLogger.logInfo "Creating bucket name: " + @bucketName
  @bucket = @storage.bucket @bucketName
end

#createStorageObject



33
34
35
36
37
38
39
# File 'lib/google_cloud_storage.rb', line 33

def createStorage
  CartLogger.logInfo "Creating google storage with id: " + @projectId + "credenciasPath: " + @credentialsFilePath
  @storage = Google::Cloud::Storage.new(
      project_id: @projectId,
      credentials: @credentialsFilePath
  )
end

#downloadZConfigJsonFile(fromFile) ⇒ Object



84
85
86
87
88
89
# File 'lib/google_cloud_storage.rb', line 84

def downloadZConfigJsonFile fromFile
  #jsonPath = 'CarrefourAPI.json'
  jsonFile = @bucket.file fromFile
  jsonFile.download fromFile
  jsonFile
end

#hasFileOnGoogleCloud(file) ⇒ Object



78
79
80
81
82
# File 'lib/google_cloud_storage.rb', line 78

def hasFileOnGoogleCloud file
  CartLogger.logInfo "Verifying if the version file "+ file +" already exists"
  bucketFile = bucket.file file
  !bucketFile.nil?
end

#loadJSONObject(jsonPath) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/google_cloud_storage.rb', line 91

def loadJSONObject jsonPath
  CartLogger.logInfo "Loading JSON file"
  json = File.read(jsonPath)
  object = JSON.parse(json)
  CartLogger.logInfo object
  CartLogger.logInfo "JSON Loaded"
  object
end

#saveJSONObject(jsonPath, jsonObject) ⇒ Object



100
101
102
103
104
105
# File 'lib/google_cloud_storage.rb', line 100

def saveJSONObject(jsonPath, jsonObject)
  binaryJson = JSON.pretty_generate(jsonObject)
  CartLogger.logInfo "Saving JSON Object in: " + jsonPath + "JSON: " + binaryJson
  File.write(jsonPath, binaryJson)
  CartLogger.logInfo "JSON Saved"
end

#uploadFrameWorkObject



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
# File 'lib/google_cloud_storage.rb', line 46

def uploadFrameWork
  CartLogger.logInfo "Prepering to upload file to google cloud"
  frameworkNameSource = @frameworkName + FRAMEWORK_EXTENSION_NAME + JSON_EXTENSION_ZIP
  frameworkNameDestination = @frameworkName + FRAMEWORK_EXTENSION_NAME + "." + @frameworkVersion + JSON_EXTENSION_ZIP
  jsonPath = @frameworkName + JSON_EXTENSION_NAME

  CartLogger.logInfo "Framework Source: " + frameworkNameSource
  CartLogger.logInfo "Framework Destination: " + frameworkNameDestination
  CartLogger.logInfo "JSON Path: " + jsonPath

  unless !hasFileOnGoogleCloud frameworkNameDestination
    throw :the_version_file_already_exists, "The current version: " + @frameworkVersion + " already exists on google cloud"
  else
    CartLogger.logInfo "File version "+ @frameworkVersion +" not exists yet, starting generate file on google cloud"
    jsonFile = downloadZConfigJsonFile(jsonPath)

    if jsonFile.nil?
      throw :could_not_download_json_file, "JSON With name: " + jsonPath
    end

    frameworkFile = bucket.create_file(frameworkNameSource, frameworkNameDestination)
    sharedUrl = frameworkFile.signed_url(method: 'GET', expires: 3.154e+8)

    jsonObject = loadJSONObject jsonPath
    jsonObject[@frameworkVersion] = sharedUrl

    saveJSONObject(jsonPath, jsonObject)

    uploadJson jsonPath
  end
end

#uploadJson(jsonPath) ⇒ Object



108
109
110
111
112
# File 'lib/google_cloud_storage.rb', line 108

def uploadJson jsonPath
  CartLogger.logInfo "Starting upload file to google cloud"
  @bucket.create_file(jsonPath, jsonPath)
  CartLogger.logInfo "Uploaded complete"
end