bunny_storage_client 1.0.0

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 +7 -0
  2. data/lib/bunny_storage_client.rb +149 -0
  3. metadata +62 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3f0aea9ab1dacb2b662005e40f68f20c0d685eedb868b45741fc940f60f75d4a
4
+ data.tar.gz: ac6d9ed2ddc79efa19dcf068c0d43093fea1c0f4d0b0324d219aff66b449c149
5
+ SHA512:
6
+ metadata.gz: dddf5d629fe5cc9d6f42fdb9f71af078dba20b2de12cc2be2c531977c725464c1dda3c985deccfae851f203fcd4f0e7cd0ffa6870ea88744b708ac9610ec85cc
7
+ data.tar.gz: 3ea20f37ed5010d17efe7e30a9b55a6e9dd84cb9d50acd612a0aa21f84d304939e8ab7b52e63b9617b3877e955d6fc6ce4f8a4cb696a320366cbb2c678f221b5
@@ -0,0 +1,149 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'net/http'
4
+ require 'uri'
5
+ require 'logger'
6
+
7
+ # BunnyStorageClient is a Ruby SDK for interacting with BunnyCDN storage services.
8
+ # API Reference: https://docs.bunny.net/reference/storage-api
9
+ class BunnyStorageClient
10
+ VERSION = '1.0.0'
11
+ BASE_URL = 'https://storage.bunnycdn.com/'
12
+
13
+ # Initializes the SDK with access credentials and optional logger.
14
+ #
15
+ # @param access_key [String] the access key for storage operations
16
+ # @param api_key [String] the API key for cache purging
17
+ # @param storage_zone_name [String, nil] the default storage zone name
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))
20
+ @access_key = access_key
21
+ @api_key = api_key
22
+ @storage_zone_name = storage_zone_name
23
+ @logger = logger
24
+ end
25
+
26
+ # Sets the object filename and optional storage zone name for operations.
27
+ #
28
+ # @param filename [String] the name of the file
29
+ # @param storage_zone_name [String, nil] the storage zone name
30
+ # @return [BunnyStorageClient] the instance of the SDK
31
+ def object(filename, storage_zone_name = nil)
32
+ @filename = filename
33
+ @storage_zone_name = storage_zone_name || @storage_zone_name
34
+ self
35
+ end
36
+
37
+ # Retrieves a file from BunnyCDN storage.
38
+ #
39
+ # @param storage_zone_name [String, nil] the storage zone name
40
+ # @param filename [String, nil] the name of the file
41
+ # @return [String, nil] the file content
42
+ def get_file(storage_zone_name: nil, filename: nil, as: :string)
43
+ storage_zone_name ||= @storage_zone_name
44
+ filename ||= @filename
45
+
46
+ uri = build_uri(storage_zone_name, filename)
47
+ request = Net::HTTP::Get.new(uri)
48
+ request['AccessKey'] = @access_key
49
+ request['accept'] = '*/*'
50
+ response = make_request(uri, request)
51
+
52
+ if as == :file
53
+ generate_tempfile(filename, response.body)
54
+ else
55
+ response.body
56
+ end
57
+ rescue StandardError => e
58
+ @logger.error("Failed to get file from #{storage_zone_name}/#{filename}: #{e.message}")
59
+ nil
60
+ end
61
+
62
+ # Uploads a file to BunnyCDN storage.
63
+ #
64
+ # @param storage_zone_name [String, nil] the storage zone name
65
+ # @param filename [String, nil] the name of the file
66
+ # @param body [String, File, Tempfile] the file content
67
+ def upload_file(storage_zone_name: nil, filename: nil, body:)
68
+ storage_zone_name ||= @storage_zone_name
69
+ filename ||= @filename
70
+ uri = build_uri(storage_zone_name, filename)
71
+ request = Net::HTTP::Put.new(uri)
72
+ request['AccessKey'] = @access_key
73
+ request['content-type'] = 'application/octet-stream'
74
+
75
+ body = body.read if body.is_a?(File) || body.is_a?(Tempfile)
76
+ request.body = body
77
+ response = make_request(uri, request)
78
+
79
+ raise StandardError, "Response code #{response.code} is not OK!" unless success_code?(response.code)
80
+ rescue StandardError => e
81
+ @logger.error("Failed to upload file to #{storage_zone_name}/#{filename}: #{e.message}")
82
+ raise
83
+ end
84
+
85
+ # Deletes a file from BunnyCDN storage.
86
+ #
87
+ # @param storage_zone_name [String, nil] the storage zone name
88
+ # @param filename [String, nil] the name of the file
89
+ # @return [Bool, nil] true if the file was deleted successfully
90
+ def delete_file(storage_zone_name: nil, filename: nil)
91
+ storage_zone_name ||= @storage_zone_name
92
+ filename ||= @filename
93
+ uri = build_uri(storage_zone_name, filename)
94
+ request = Net::HTTP::Delete.new(uri)
95
+ request['AccessKey'] = @access_key
96
+ response = make_request(uri, request)
97
+ raise StandardError, "Response code #{response.code} is not OK!" unless success_code?(response.code)
98
+
99
+ true
100
+ rescue StandardError => e
101
+ @logger.error("Failed to delete file: #{e.message}")
102
+ raise
103
+ end
104
+
105
+ # Purges the cache for a specific file in BunnyCDN.
106
+ #
107
+ # @param storage_zone_name [String, nil] the storage zone name
108
+ # @param filename [String, nil] the name of the file
109
+ # @return [String, nil] the response code
110
+ def purge_cache(storage_zone_name: nil, filename: nil)
111
+ storage_zone_name ||= @storage_zone_name
112
+ filename ||= @filename
113
+ url = "https://#{storage_zone_name}.b-cdn.net/#{filename}"
114
+ uri = URI("https://api.bunny.net/purge?url=#{url}&async=true")
115
+ request = Net::HTTP::Post.new(uri)
116
+ request['AccessKey'] = @api_key
117
+ response = make_request(uri, request)
118
+
119
+ response.code
120
+ rescue StandardError => e
121
+ @logger.error("Failed to purge cache for #{storage_zone_name}/#{filename}: #{e.message}")
122
+ nil
123
+ end
124
+
125
+ private
126
+
127
+ def build_uri(storage_zone_name, filename)
128
+ url = File.join(BASE_URL, storage_zone_name, filename)
129
+ url = url[...-1] if url.end_with?(File::SEPARATOR)
130
+ URI(url)
131
+ end
132
+
133
+ def make_request(uri, request)
134
+ https = Net::HTTP.new(uri.host, uri.port)
135
+ https.use_ssl = true
136
+ https.request(request)
137
+ end
138
+
139
+ def generate_tempfile(filename, body)
140
+ file = Tempfile.new(filename)
141
+ file.write(body)
142
+ file.rewind
143
+ file
144
+ end
145
+
146
+ def success_code?(code)
147
+ code.to_i.between?(200, 299)
148
+ end
149
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bunny_storage_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ramit Koul
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-06-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: net-http
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.0
27
+ description: BunnyStorage Client is a Ruby SDK for interacting with BunnyCDN storage
28
+ services.
29
+ email:
30
+ - ramitkaul@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - lib/bunny_storage_client.rb
36
+ homepage: https://github.com/rkwap/bunny_storage_client
37
+ licenses:
38
+ - MIT
39
+ metadata:
40
+ homepage_uri: https://github.com/rkwap/bunny_storage_client
41
+ source_code_uri: https://github.com/rkwap/bunny_storage_client
42
+ changelog_uri: https://github.com/rkwap/bunny_storage_client/CHANGELOG.md
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '2.5'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubygems_version: 3.2.33
59
+ signing_key:
60
+ specification_version: 4
61
+ summary: A Ruby Client SDK for interacting with BunnyCDN storage services.
62
+ test_files: []