Class: Bunny::Client

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

Constant Summary collapse

BASE_URL =
"https://api.bunny.net"

Class Method Summary collapse

Class Method Details

.connectionObject



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/bunny/client.rb', line 6

def connection
  @connection ||= Faraday.new(BASE_URL) do |conn|
    conn.headers = {
      "AccessKey" => Bunny.config.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



35
36
37
# File 'lib/bunny/client.rb', line 35

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

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



19
20
21
# File 'lib/bunny/client.rb', line 19

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

.handle_response(response) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/bunny/client.rb', line 39

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

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



31
32
33
# File 'lib/bunny/client.rb', line 31

def patch_request(url, body:, headers: {})
  handle_response connection.patch(url, body, headers)
end

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



23
24
25
# File 'lib/bunny/client.rb', line 23

def post_request(url, body: {}, headers: {})
  handle_response connection.post(url, body, headers)
end

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



27
28
29
# File 'lib/bunny/client.rb', line 27

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