Class: Bunny::Edge::Client

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

Class Method Summary collapse

Class Method Details

.connectionObject



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bunny/edge/client.rb', line 32

def connection
  @connection ||= Faraday.new(url) do |conn|
    conn.headers = {
      "AccessKey" => Bunny.config.edge_api_token,
      "User-Agent" => "bunnyrb/v#{VERSION} (github.com/deanpcmad/bunnyrb)"
    }

    conn.request :json

    conn.response :json, content_type: "application/json"
  end
end

.delete_request(url, headers: {}) ⇒ Object

def patch_request(url, body:, headers: {})

handle_response connection.patch(url, body, headers)

end



76
77
78
# File 'lib/bunny/edge/client.rb', line 76

def delete_request(url, headers: {})
  handle_response connection.delete(url, headers)
end

.get_request(url, params: {}, headers: {}) ⇒ Object



60
61
62
# File 'lib/bunny/edge/client.rb', line 60

def get_request(url, params: {}, headers: {})
  handle_response connection.get(url, params, headers)
end

.handle_response(response) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/bunny/edge/client.rb', line 80

def handle_response(response)
  case response.status
  when 400
    raise Error, "Error 400: Your request was malformed. '#{response.body["Message"]}'"
  when 401
    raise Error, "Error 401: You did not supply valid authentication credentials. '#{response.body["Message"]}'"
  when 403
    raise Error, "Error 403: You are not allowed to perform that action. '#{response.body["Message"]}'"
  when 404
    raise Error, "Error 404: No results were found for your request. '#{response.body["Message"]}'"
  when 405
    raise Error, "Error 405: Method Not Allowed. '#{response.body["Message"]}'"
  when 409
    raise Error, "Error 409: Your request was a conflict. '#{response.body["Message"]}'"
  when 422
    raise Error, "Error 422: Unprocessable Content. '#{response.body["Message"]}'"
  when 429
    raise Error, "Error 429: Your request exceeded the API rate limit. '#{response.body["Message"]}'"
  when 500
    raise Error, "Error 500: We were unable to perform the request due to server-side problems. '#{response.body["Message"]}'"
  when 503
    raise Error, "Error 503: You have been rate limited for sending more than 20 requests per second. '#{response.body["Message"]}'"
  when 501
    raise Error, "Error 501: This resource has not been implemented. '#{response.body["Message"]}'"
  when 204
    return true
  end

  response
end

.put_request(url, body:, headers: {}) ⇒ Object

def post_request(url, body: {}, headers: {})

handle_response connection.post(url, body, headers)

end



68
69
70
# File 'lib/bunny/edge/client.rb', line 68

def put_request(url, body:, headers: {})
  handle_response upload_connection.put(url, body, headers)
end

.upload_connectionObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/bunny/edge/client.rb', line 45

def upload_connection
  @connection ||= Faraday.new(url) do |conn|
    conn.headers = {
      "AccessKey" => Bunny.config.edge_api_token,
      "User-Agent" => "bunnyrb/v#{VERSION} (github.com/deanpcmad/bunnyrb)"
    }

    conn.request :multipart, flat_encode: true

    # conn.request :json

    # conn.response :json, content_type: "application/json"
  end
end

.urlObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/bunny/edge/client.rb', line 5

def url
  raise "An Edge Region must be specified" unless Bunny.config.edge_region
  case Bunny.config.edge_region.downcase

  when "de"
    "https://storage.bunnycdn.com"
  when "uk"
    "https://uk.storage.bunnycdn.com"
  when "ny"
    "https://ny.storage.bunnycdn.com"
  when "la"
    "https://la.storage.bunnycdn.com"
  when "sg"
    "https://sg.storage.bunnycdn.com"
  when "se"
    "https://se.storage.bunnycdn.com"
  when "br"
    "https://br.storage.bunnycdn.com"
  when "sa"
    "https://jh.storage.bunnycdn.com"
  when "syd"
    "https://syd.storage.bunnycdn.com"
  else
    "https://storage.bunnycdn.com"
  end
end