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



40
41
42
43
44
45
# File 'lib/record_store/provider/cloudflare/client.rb', line 40

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
17
# 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



33
34
35
36
37
38
# File 'lib/record_store/provider/cloudflare/client.rb', line 33

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



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

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



26
27
28
29
30
31
# File 'lib/record_store/provider/cloudflare/client.rb', line 26

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



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
76
77
# File 'lib/record_store/provider/cloudflare/client.rb', line 47

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)
    rescue StandardError => e
      raise "HTTP error: #{e.message}"
    end
    response
  end
end