Class: Cloudflare::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudflare/connection.rb

Overview

Internal Faraday wrapper. Singleton; shared by every resource across every product. Auth, retries, and JSON encoding live here so the Resource layer can stay focused on REST semantics.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.instanceObject



7
# File 'lib/cloudflare/connection.rb', line 7

def instance = @instance ||= new

.reset!Object



8
# File 'lib/cloudflare/connection.rb', line 8

def reset!   = @instance = nil

Instance Method Details

#request(method, path, body: nil, params: nil, api_token: nil) ⇒ Object

Sends a Cloudflare API request. The api_token kwarg is the resolved token chosen by the caller (typically Resource.configured_api_token —per-product token if set, top-level Cloudflare.api_token otherwise). It’s a kwarg rather than a hardcoded read so admin tools can override per-call.



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cloudflare/connection.rb', line 16

def request(method, path, body: nil, params: nil, api_token: nil)
  token = api_token || Cloudflare.api_token
  raise Error, "Cloudflare API token not configured" if token.nil?

  faraday.public_send(method, path.delete_prefix("/")) do |req|
    req.headers["Authorization"] = "Bearer #{token}"
    req.headers["User-Agent"]    = Cloudflare.configuration.user_agent
    req.params = compact(params) if params
    req.body   = compact(body)   if body
  end.body
rescue Faraday::Error => e
  raise translate_error(e)
end