bunny_storage_client 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/bunny_storage_client.rb +42 -6
  3. metadata +4 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3f0aea9ab1dacb2b662005e40f68f20c0d685eedb868b45741fc940f60f75d4a
4
- data.tar.gz: ac6d9ed2ddc79efa19dcf068c0d43093fea1c0f4d0b0324d219aff66b449c149
3
+ metadata.gz: a47aba2b6b09ecdcc4d403f38277681fd74e025217f59d73b7586526c35adc97
4
+ data.tar.gz: 0d78f8d530772f3cfa651a9633b5eb51db0055c42069b16092cd84c7d80f970c
5
5
  SHA512:
6
- metadata.gz: dddf5d629fe5cc9d6f42fdb9f71af078dba20b2de12cc2be2c531977c725464c1dda3c985deccfae851f203fcd4f0e7cd0ffa6870ea88744b708ac9610ec85cc
7
- data.tar.gz: 3ea20f37ed5010d17efe7e30a9b55a6e9dd84cb9d50acd612a0aa21f84d304939e8ab7b52e63b9617b3877e955d6fc6ce4f8a4cb696a320366cbb2c678f221b5
6
+ metadata.gz: 8dd4261af13d733df413a768131fc5ffe2b1bfa30a480641ddfef25fc0262609e57736837c802661bc6d2c55bc9a72613761a433eeffa0c404d0c28e4ea547b4
7
+ data.tar.gz: a0dcb474c93610736d98c99c478438e032cc6735546ce92d72a3b5f9ade46a691b46bbd7be899751eab97fef45e0d06284cc5b3d8f97452975710edec16bc905
@@ -7,7 +7,7 @@ require 'logger'
7
7
  # BunnyStorageClient is a Ruby SDK for interacting with BunnyCDN storage services.
8
8
  # API Reference: https://docs.bunny.net/reference/storage-api
9
9
  class BunnyStorageClient
10
- VERSION = '1.0.0'
10
+ VERSION = '1.0.1'
11
11
  BASE_URL = 'https://storage.bunnycdn.com/'
12
12
 
13
13
  # Initializes the SDK with access credentials and optional logger.
@@ -16,11 +16,14 @@ class BunnyStorageClient
16
16
  # @param api_key [String] the API key for cache purging
17
17
  # @param storage_zone_name [String, nil] the default storage zone name
18
18
  # @param logger [Logger] the logger instance for logging errors and info
19
- def initialize(access_key, api_key, storage_zone_name = nil, logger = Logger.new($stdout))
19
+ def initialize(access_key, api_key, storage_zone_name = nil, region = nil, logger = Logger.new($stdout))
20
20
  @access_key = access_key
21
21
  @api_key = api_key
22
+ @region = region
22
23
  @storage_zone_name = storage_zone_name
23
24
  @logger = logger
25
+
26
+ @base_url = @region ? "https://#{@region}.storage.bunnycdn.com/" : BASE_URL
24
27
  end
25
28
 
26
29
  # Sets the object filename and optional storage zone name for operations.
@@ -49,6 +52,8 @@ class BunnyStorageClient
49
52
  request['accept'] = '*/*'
50
53
  response = make_request(uri, request)
51
54
 
55
+ raise StandardError, "#{response.code} #{response.body}" unless success_code?(response.code)
56
+
52
57
  if as == :file
53
58
  generate_tempfile(filename, response.body)
54
59
  else
@@ -59,6 +64,28 @@ class BunnyStorageClient
59
64
  nil
60
65
  end
61
66
 
67
+ def exist?(storage_zone_name: nil, filename: nil)
68
+ storage_zone_name ||= @storage_zone_name
69
+ filename ||= @filename
70
+
71
+ uri = build_uri(storage_zone_name, filename)
72
+
73
+ # First, send a GET request to check if the file exists
74
+ # Maybe HEAD will be available in the future
75
+ head_request = Net::HTTP::Get.new(uri)
76
+ head_request['AccessKey'] = @access_key
77
+ head_request['accept'] = '*/*'
78
+
79
+ head_response = make_request(uri, head_request)
80
+
81
+ # Check if the file exists (status code 200)
82
+ if head_response.code != '200'
83
+ return false
84
+ end
85
+
86
+ true
87
+ end
88
+
62
89
  # Uploads a file to BunnyCDN storage.
63
90
  #
64
91
  # @param storage_zone_name [String, nil] the storage zone name
@@ -72,8 +99,13 @@ class BunnyStorageClient
72
99
  request['AccessKey'] = @access_key
73
100
  request['content-type'] = 'application/octet-stream'
74
101
 
75
- body = body.read if body.is_a?(File) || body.is_a?(Tempfile)
76
- request.body = body
102
+ if body.respond_to?(:read)
103
+ body.rewind if body.respond_to?(:rewind) # Reset to the beginning if needed
104
+ request.body = body.read
105
+ else
106
+ request.body = body.to_s # Default to converting the body to a string
107
+ end
108
+
77
109
  response = make_request(uri, request)
78
110
 
79
111
  raise StandardError, "Response code #{response.code} is not OK!" unless success_code?(response.code)
@@ -94,7 +126,9 @@ class BunnyStorageClient
94
126
  request = Net::HTTP::Delete.new(uri)
95
127
  request['AccessKey'] = @access_key
96
128
  response = make_request(uri, request)
97
- raise StandardError, "Response code #{response.code} is not OK!" unless success_code?(response.code)
129
+
130
+ # hack, 500 is also not found
131
+ raise StandardError, "Response code #{response.code} is not OK!" if !success_code?(response.code) && response.code != "404" && response.code != "500"
98
132
 
99
133
  true
100
134
  rescue StandardError => e
@@ -125,7 +159,7 @@ class BunnyStorageClient
125
159
  private
126
160
 
127
161
  def build_uri(storage_zone_name, filename)
128
- url = File.join(BASE_URL, storage_zone_name, filename)
162
+ url = File.join(@base_url, storage_zone_name, filename)
129
163
  url = url[...-1] if url.end_with?(File::SEPARATOR)
130
164
  URI(url)
131
165
  end
@@ -133,6 +167,8 @@ class BunnyStorageClient
133
167
  def make_request(uri, request)
134
168
  https = Net::HTTP.new(uri.host, uri.port)
135
169
  https.use_ssl = true
170
+ https.open_timeout = 3
171
+ https.read_timeout = 5
136
172
  https.request(request)
137
173
  end
138
174
 
metadata CHANGED
@@ -1,27 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bunny_storage_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ramit Koul
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-06-18 00:00:00.000000000 Z
11
+ date: 2024-12-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: net-http
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: 0.1.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: 0.1.0
27
27
  description: BunnyStorage Client is a Ruby SDK for interacting with BunnyCDN storage