Class: Cloudflare::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/record_store/provider/cloudflare/client.rb

Constant Summary collapse

API_FQDN =
'api.cloudflare.com'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(api_token) ⇒ Client

Returns a new instance of Client.



8
9
10
# File 'lib/record_store/provider/cloudflare/client.rb', line 8

def initialize(api_token)
  @api_token = api_token
end

Instance Method Details

#delete(endpoint) ⇒ Object



36
37
38
39
40
# File 'lib/record_store/provider/cloudflare/client.rb', line 36

def delete(endpoint)
  uri = build_uri(endpoint)
  response = request(:delete, uri)
  Cloudflare::Response.new(response)
end

#get(endpoint, params = {}) ⇒ Object



12
13
14
15
16
# File 'lib/record_store/provider/cloudflare/client.rb', line 12

def get(endpoint, params = {})
  uri = build_uri(endpoint, params)
  response = request(:get, uri)
  Cloudflare::Response.new(response)
end

#patch(endpoint, body = nil) ⇒ Object



30
31
32
33
34
# File 'lib/record_store/provider/cloudflare/client.rb', line 30

def patch(endpoint, body = nil)
  uri = build_uri(endpoint)
  response = request(:patch, uri, body: body)
  Cloudflare::Response.new(response)
end

#post(endpoint, body = nil) ⇒ Object



18
19
20
21
22
# File 'lib/record_store/provider/cloudflare/client.rb', line 18

def post(endpoint, body = nil)
  uri = build_uri(endpoint)
  response = request(:post, uri, body: body)
  Cloudflare::Response.new(response)
end

#put(endpoint, body = nil) ⇒ Object



24
25
26
27
28
# File 'lib/record_store/provider/cloudflare/client.rb', line 24

def put(endpoint, body = nil)
  uri = build_uri(endpoint)
  response = request(:put, uri, body: body)
  Cloudflare::Response.new(response)
end

#request(method, uri, body: nil) ⇒ Object



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
69
70
71
72
73
74
75
# File 'lib/record_store/provider/cloudflare/client.rb', line 42

def request(method, uri, body: nil)
  Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |conn|
    request = case method
    when :get
      Net::HTTP::Get.new(uri)
    when :post
      request = Net::HTTP::Post.new(uri)
      request.body = body.to_json if body
      request
    when :put
      request = Net::HTTP::Put.new(uri)
      request.body = body.to_json if body
      request
    when :patch
      request = Net::HTTP::Patch.new(uri)
      request.body = body.to_json if body
      request
    when :delete
      Net::HTTP::Delete.new(uri)
    end

    cloudflare_headers.each { |k, v| request[k] = v }

    begin
      response = conn.request(request)
      response.value
      response
    rescue Net::HTTPRetriableError, Net::HTTPFatalError => e
      raise RecordStore::Provider::RetriableError, e.message
    rescue Net::HTTPClientException, Net::HTTPError => e
      raise RecordStore::Provider::Error, e.message
    end
  end
end