Class: BunBun::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/bunbun/client.rb

Defined Under Namespace

Classes: ApiKey, Country, Namespace, PullZone, Region, Statistics, StorageZone

Instance Attribute Summary collapse

Instance Method Summary collapse

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_keyObject (readonly)

Returns the value of attribute api_key.



32
33
34
# File 'lib/bunbun/client.rb', line 32

def api_key
  @api_key
end

#countryObject (readonly)

Returns the value of attribute country.



34
35
36
# File 'lib/bunbun/client.rb', line 34

def country
  @country
end

#pull_zoneObject (readonly)

Returns the value of attribute pull_zone.



103
104
105
# File 'lib/bunbun/client.rb', line 103

def pull_zone
  @pull_zone
end

#regionObject (readonly)

Returns the value of attribute region.



111
112
113
# File 'lib/bunbun/client.rb', line 111

def region
  @region
end

#statisticsObject (readonly)

Returns the value of attribute statistics.



113
114
115
# File 'lib/bunbun/client.rb', line 113

def statistics
  @statistics
end

#storage_zoneObject (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|
    message = Net::HTTP::Delete.new(path)
    message['AccessKey'] = @access_key
    message['User-Agent'] = @user_agent

    BunBun::Response.parse(http.request(message))
  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|
    message = Net::HTTP::Get.new(path)
    message['Accept'] = '*/*'
    message['AccessKey'] = @access_key
    message['User-Agent'] = @user_agent

    http.request(message) 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|
    message = Net::HTTP::Get.new(path)
    message['Accept'] = 'application/json'
    message['AccessKey'] = @access_key
    message['User-Agent'] = @user_agent

    BunBun::Response.parse(http.request(message))
  end
end

#inspectObject



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|
    message = Net::HTTP::Post.new(path)
    message['AccessKey'] = @access_key
    message['User-Agent'] = @user_agent

    unless params.nil?
      message['Content-Type'] = 'application/json'
      message.body = JSON.generate(params)
    end

    BunBun::Response.parse(http.request(message))
  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|
    message = Net::HTTP::Put.new(path)
    message['AccessKey'] = @access_key
    message['Content-Type'] = content_type
    message['User-Agent'] = @user_agent

    if content.respond_to?(:read)
      size = content.respond_to?(:path) ? File.size(content.path) : content.size

      message['Content-Length'] = size
      message.body_stream = content
    else
      message.body = content
    end

    http.request(message)
  end
end