Class: Cloudflared::Resource
- Inherits:
-
Object
- Object
- Cloudflared::Resource
- Defined in:
- lib/cloudflared/resource.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Instance Method Summary collapse
- #default_headers ⇒ Object
- #delete_request(url, params: {}, headers: {}) ⇒ Object
- #get_request(url, params: {}, headers: {}) ⇒ Object
- #handle_response(response) ⇒ Object
-
#initialize(client) ⇒ Resource
constructor
A new instance of Resource.
- #patch_request(url, body:, headers: {}) ⇒ Object
- #post_request(url, body:, headers: {}) ⇒ Object
- #put_request(url, body:, headers: {}) ⇒ Object
- #upload_request(url, file:, params: {}, headers: {}) ⇒ Object
Constructor Details
#initialize(client) ⇒ Resource
Returns a new instance of Resource.
8 9 10 |
# File 'lib/cloudflared/resource.rb', line 8 def initialize(client) @client = client end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
7 8 9 |
# File 'lib/cloudflared/resource.rb', line 7 def client @client end |
Instance Method Details
#default_headers ⇒ Object
41 42 43 |
# File 'lib/cloudflared/resource.rb', line 41 def default_headers {Authorization: "Bearer #{client.api_key}"} end |
#delete_request(url, params: {}, headers: {}) ⇒ Object
37 38 39 |
# File 'lib/cloudflared/resource.rb', line 37 def delete_request(url, params: {}, headers: {}) handle_response client.connection.delete(url, params, default_headers.merge(headers)) end |
#get_request(url, params: {}, headers: {}) ⇒ Object
12 13 14 |
# File 'lib/cloudflared/resource.rb', line 12 def get_request(url, params: {}, headers: {}) handle_response client.connection.get(url, params, default_headers.merge(headers)) end |
#handle_response(response) ⇒ Object
45 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 72 |
# File 'lib/cloudflared/resource.rb', line 45 def handle_response(response) = if response.body.is_a? Hash response.body["errors"]&.map { |error| error["message"] }&.join(" ") else response.body.inspect end case response.status when 400 raise Error, "Bad request, the request was invalid. #{}" when 401 raise Error, "Unauthorized, the user does not have permission. #{}" when 403 raise Error, "Forbidden, the request was not authenticated. #{}" when 404 raise Error, "Not found, the resource was not found. #{}" when 429 raise Error, "Too many requests, client is rate limited. #{}" when 405 raise Error, "Method not allowed, incorrect HTTP method provided. #{}" when 415 raise Error, "Unsupported media type, response is not valid JSON. #{}" when 500 raise Error, "Server error. #{}" else response end end |
#patch_request(url, body:, headers: {}) ⇒ Object
33 34 35 |
# File 'lib/cloudflared/resource.rb', line 33 def patch_request(url, body:, headers: {}) handle_response client.connection.patch(url, body, default_headers.merge(headers)) end |
#post_request(url, body:, headers: {}) ⇒ Object
16 17 18 |
# File 'lib/cloudflared/resource.rb', line 16 def post_request(url, body:, headers: {}) handle_response client.connection.post(url, body, default_headers.merge(headers)) end |
#put_request(url, body:, headers: {}) ⇒ Object
29 30 31 |
# File 'lib/cloudflared/resource.rb', line 29 def put_request(url, body:, headers: {}) handle_response client.connection.put(url, body, default_headers.merge(headers)) end |
#upload_request(url, file:, params: {}, headers: {}) ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/cloudflared/resource.rb', line 20 def upload_request(url, file:, params: {}, headers: {}) params[:file] = Faraday::Multipart::FilePart.new( file, Marcel::MimeType.for(Pathname.new(file)) ) handle_response client.connection(request_type: :multipart).post(url, params, default_headers.merge(headers)) end |