Class: BunBun::Client
- Inherits:
-
Object
- Object
- BunBun::Client
- Defined in:
- lib/bunbun/client.rb
Defined Under Namespace
Classes: ApiKey, Country, Namespace, PullZone, Region, Statistics, StorageZone
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#country ⇒ Object
readonly
Returns the value of attribute country.
-
#pull_zone ⇒ Object
readonly
Returns the value of attribute pull_zone.
-
#region ⇒ Object
readonly
Returns the value of attribute region.
-
#statistics ⇒ Object
readonly
Returns the value of attribute statistics.
-
#storage_zone ⇒ Object
readonly
Returns the value of attribute storage_zone.
Instance Method Summary collapse
- #delete(path) ⇒ Object
- #download(path, destination = nil) ⇒ Object
- #get(path) ⇒ Object
-
#initialize(access_key: nil, host: nil, open_timeout: 2, read_timeout: 2) ⇒ Client
constructor
A new instance of Client.
- #inspect ⇒ Object
- #post(path, params = nil) ⇒ Object
- #purge(url:, async: nil) ⇒ Object
- #upload(path, content, content_type: 'application/octet-stream') ⇒ Object
Constructor Details
#initialize(access_key: nil, host: nil, open_timeout: 2, read_timeout: 2) ⇒ Client
Returns a new instance of Client.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/bunbun/client.rb', line 14 def initialize(access_key: nil, host: nil, open_timeout: 2, read_timeout: 2) @access_key = access_key || ENV.fetch('BUNNY_ACCESS_KEY') @host = host || ENV['BUNNY_HOST'] || 'api.bunny.net' @port = Net::HTTP.https_default_port @opts = { use_ssl: true, open_timeout: open_timeout, read_timeout: read_timeout } @user_agent = "ruby/#{RUBY_VERSION} bunbun/#{BunBun::VERSION}" @api_key = BunBun::Client::ApiKey.new(self) @country = BunBun::Client::Country.new(self) @pull_zone = BunBun::Client::PullZone.new(self) @region = BunBun::Client::Region.new(self) @statistics = BunBun::Client::Statistics.new(self) @storage_zone = BunBun::Client::StorageZone.new(self) end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
32 33 34 |
# File 'lib/bunbun/client.rb', line 32 def api_key @api_key end |
#country ⇒ Object (readonly)
Returns the value of attribute country.
34 35 36 |
# File 'lib/bunbun/client.rb', line 34 def country @country end |
#pull_zone ⇒ Object (readonly)
Returns the value of attribute pull_zone.
103 104 105 |
# File 'lib/bunbun/client.rb', line 103 def pull_zone @pull_zone end |
#region ⇒ Object (readonly)
Returns the value of attribute region.
111 112 113 |
# File 'lib/bunbun/client.rb', line 111 def region @region end |
#statistics ⇒ Object (readonly)
Returns the value of attribute statistics.
113 114 115 |
# File 'lib/bunbun/client.rb', line 113 def statistics @statistics end |
#storage_zone ⇒ Object (readonly)
Returns the value of attribute storage_zone.
115 116 117 |
# File 'lib/bunbun/client.rb', line 115 def storage_zone @storage_zone end |
Instance Method Details
#delete(path) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/bunbun/client.rb', line 36 def delete(path) start do |http| = Net::HTTP::Delete.new(path) ['AccessKey'] = @access_key ['User-Agent'] = @user_agent BunBun::Response.parse(http.request()) end end |
#download(path, destination = nil) ⇒ Object
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 |
# File 'lib/bunbun/client.rb', line 46 def download(path, destination = nil) start do |http| = Net::HTTP::Get.new(path) ['Accept'] = '*/*' ['AccessKey'] = @access_key ['User-Agent'] = @user_agent http.request() do |response| unless response.is_a?(Net::HTTPSuccess) raise BunBun::Response.error(response) end if destination File.open(destination, 'wb') do |file| response.read_body do |chunk| file.write(chunk) end end else yield response if block_given? end response end end end |
#get(path) ⇒ Object
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/bunbun/client.rb', line 73 def get(path) start do |http| = Net::HTTP::Get.new(path) ['Accept'] = 'application/json' ['AccessKey'] = @access_key ['User-Agent'] = @user_agent BunBun::Response.parse(http.request()) end end |
#inspect ⇒ Object
84 85 86 |
# File 'lib/bunbun/client.rb', line 84 def inspect "<#{self.class.name}: host=#{@host}>" end |
#post(path, params = nil) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/bunbun/client.rb', line 88 def post(path, params = nil) start do |http| = Net::HTTP::Post.new(path) ['AccessKey'] = @access_key ['User-Agent'] = @user_agent unless params.nil? ['Content-Type'] = 'application/json' .body = JSON.generate(params) end BunBun::Response.parse(http.request()) end end |
#purge(url:, async: nil) ⇒ Object
105 106 107 108 109 |
# File 'lib/bunbun/client.rb', line 105 def purge(url:, async: nil) params = {url: url, async: async} post(BunBun::URI.join('/purge', params)) end |
#upload(path, content, content_type: 'application/octet-stream') ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/bunbun/client.rb', line 117 def upload(path, content, content_type: 'application/octet-stream') start do |http| = Net::HTTP::Put.new(path) ['AccessKey'] = @access_key ['Content-Type'] = content_type ['User-Agent'] = @user_agent if content.respond_to?(:read) size = content.respond_to?(:path) ? File.size(content.path) : content.size ['Content-Length'] = size .body_stream = content else .body = content end http.request() end end |