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



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

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



107
108
109
110
111
112
113
114
# File 'lib/bunny_cdn/storage.rb', line 107

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



58
59
60
61
62
63
64
65
# File 'lib/bunny_cdn/storage.rb', line 58

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



45
46
47
48
49
50
51
52
# File 'lib/bunny_cdn/storage.rb', line 45

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



36
37
38
39
40
# File 'lib/bunny_cdn/storage.rb', line 36

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
24
25
26
27
28
# 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
  if BunnyCdn.configuration.region.nil? || BunnyCdn.configuration.region == 'de'
    'https://storage.bunnycdn.com'
  else
    "https://#{BunnyCdn.configuration.region}.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



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/bunny_cdn/storage.rb', line 71

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



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/bunny_cdn/storage.rb', line 89

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