Class: BunnyCdn::Storage

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

Class Method Summary collapse

Class Method Details

.apiKeyObject

Sets the apiKey to that in configuration



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

def self.apiKey
  BunnyCdn.configuration.accessKey
end

.deleteFile(path = '', file) ⇒ Object

Deletes a file from the storage zone Params:

path

path to delete file from

file

specific file to delete from storage zone



102
103
104
105
106
107
108
109
# File 'lib/bunny_cdn/storage.rb', line 102

def self.deleteFile(path= '', file)
  begin
    response = RestClient.delete("#{set_region_url}/#{storageZone}/#{path}/#{file}", headers)
  rescue RestClient::ExceptionWithResponse => exception
    return exception
  end
  return response.body
end

.getFile(path = '', file) ⇒ Object

Gets a single file from the storage zone Params:

path

desired path to get file

file

specific file to get from storage zone



53
54
55
56
57
58
59
60
# File 'lib/bunny_cdn/storage.rb', line 53

def self.getFile(path= '', file)
  begin
    response = RestClient.get("#{set_region_url}/#{storageZone}/#{path}/#{file}", headers)
  rescue RestClient::ExceptionWithResponse => exception
    return exception
  end
  return response.body
end

.getZoneFiles(path = '') ⇒ Object

Gets all the files from the storage zone Params:

path

desired path to get files



40
41
42
43
44
45
46
47
# File 'lib/bunny_cdn/storage.rb', line 40

def self.getZoneFiles(path= '')
  begin
    response = RestClient.get("#{set_region_url}/#{storageZone}/#{path}", headers)
  rescue RestClient::ExceptionWithResponse => exception
    return exception
  end
  return response.body
end

.headersObject

Sets the necessary headers to make requests to the BunnyCDN API



31
32
33
34
35
# File 'lib/bunny_cdn/storage.rb', line 31

def self.headers
  {
    :accesskey => apiKey
  }
end

.set_region_urlObject

Sets the proper URL based on the region set in configuration



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/bunny_cdn/storage.rb', line 12

def self.set_region_url
  case BunnyCdn.configuration.region
  when nil || 'eu'
    'https://storage.bunnycdn.com'
  when 'ny'
    'https://ny.storage.bunnycdn.com'
  when 'la'
    'https://la.storage.bunnycdn.com'
  when 'sg'
    'https://sg.storage.bunnycdn.com'
  end
end

.storageZoneObject

Sets the storage zone as set in configuration



7
8
9
# File 'lib/bunny_cdn/storage.rb', line 7

def self.storageZone
  BunnyCdn.configuration.storageZone
end

.uploadFile(path = '', file) ⇒ Object

Uploads a file on the system to the storage zone Params:

path

desired path to upload file

file

specific file to upload to storage zone



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/bunny_cdn/storage.rb', line 66

def self.uploadFile(path= '', file)
  fileName = File.basename(file)
  headers = {
    :accessKey => apiKey,
    :checksum => ''
  }
  begin
    response = RestClient.put("#{set_region_url}/#{storageZone}/#{path}/#{fileName}", File.read(file), headers)
  rescue RestClient::ExceptionWithResponse => exception
    return exception
  end
  return response.body
end

.uploadFormFile(path = '', file) ⇒ Object

Uploads a file from a file input to the storage zone Params:

path

desired path to upload file

file

specific file to upload to storage zone



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/bunny_cdn/storage.rb', line 84

def self.uploadFormFile(path= '', file)
  fileName = file.original_filename
  headers = {
    :accessKey => apiKey,
    :checksum => ''
  }
  begin
    response = RestClient.put("#{set_region_url}/#{storageZone}/#{path}/#{fileName}", File.read(file.tempfile), headers)
  rescue RestClient::ExceptionWithResponse => exception
    return exception
  end
  return response.body
end