Class: CloudflareClient::Zone

Inherits:
CloudflareClient show all
Defined in:
lib/cloudflare_client/zone.rb

Direct Known Subclasses

Base

Defined Under Namespace

Classes: Analytics, Base, CustomHostname, CustomHostnameV2, CustomPage, CustomSSL, DNS, Firewall, KeylessSSL, Log, PageRule, RailgunConnections, RateLimit, SSL, Subscription

Constant Summary collapse

VALID_ZONE_STATUSES =
%w[active pending initializing moved deleted deactivated].freeze

Constants inherited from CloudflareClient

API_BASE, POSSIBLE_API_SETTINGS, VALID_BUNDLE_METHODS, VALID_DIRECTIONS, VALID_MATCHES, VERSION

Instance Method Summary collapse

Methods inherited from CloudflareClient

#initialize

Constructor Details

This class inherits a constructor from CloudflareClient

Instance Method Details

#create_zone(name:, jump_start: true, organization: {id: nil, name: nil}) ⇒ Object

create’s a zone with a given name create_zone(name: name_of_zone, jump_start: true|false (default true), organization: org_id, name: org_name)



30
31
32
33
34
35
36
37
# File 'lib/cloudflare_client/zone.rb', line 30

def create_zone(name:, jump_start: true, organization: {id: nil, name: nil})
  raise('Zone name required') if name.nil?
  unless organization[:id].nil? && organization[:name].nil
    org_data = organization.merge(status: 'active', permissions: ['#zones:read'])
  end
  data = {name: name, jump_start: jump_start, organization: org_data}
  cf_post(path: '/zones', data: data)
end

#delete_zone(zone_id:) ⇒ Object

delete a given zone delete_zone(zone_id: id_of_zone



87
88
89
90
# File 'lib/cloudflare_client/zone.rb', line 87

def delete_zone(zone_id:)
  raise('zone_id required') if zone_id.nil?
  cf_delete(path: "/zones/#{zone_id}")
end

#edit_zone(zone_id:, paused: nil, vanity_name_servers: [], plan: {id: nil}) ⇒ Object

edit the properties of a zone NOTE: some of these options require an enterprise account edit_zone(zone_id: id_of_zone, paused: true|false, vanity_name_servers: [‘ns1.foo.bar’, ‘ns2.foo.bar’], plan: plan_id)



60
61
62
63
64
65
66
67
# File 'lib/cloudflare_client/zone.rb', line 60

def edit_zone(zone_id:, paused: nil, vanity_name_servers: [], plan: {id: nil})
  raise('zone_id required') if zone_id.nil?
  data                       = {}
  data[:paused]              = paused unless paused.nil?
  data[:vanity_name_servers] = vanity_name_servers unless vanity_name_servers.empty?
  data[:plan]                = plan unless plan[:id].nil?
  cf_patch(path: "/zones/#{zone_id}", data: data)
end

#purge_zone_cache(zone_id:, tags: [], files: [], purge_everything: nil) ⇒ Object

various zone caching controlls. supploy an array of tags, or files, or the purge_everything bool



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/cloudflare_client/zone.rb', line 72

def purge_zone_cache(zone_id:, tags: [], files: [], purge_everything: nil)
  raise('zone_id required') if zone_id.nil?
  if purge_everything.nil? && (tags.empty? && files.empty?)
    raise('specify a combination tags[], files[] or purge_everything')
  end
  data                    = {}
  data[:purge_everything] = purge_everything unless purge_everything.nil?
  data[:tags]             = tags unless tags.empty?
  data[:files]            = files unless files.empty?
  cf_delete(path: "/zones/#{zone_id}/purge_cache", data: data)
end

#update_zone_settings(zone_id:, settings: []) ⇒ Object

update 1 or more settings in a zone settings: [value: true,‘value’…] api.cloudflare.com/#zone-settings-properties



111
112
113
114
115
116
117
118
119
# File 'lib/cloudflare_client/zone.rb', line 111

def update_zone_settings(zone_id:, settings: [])
  raise('zone_id required') if zone_id.nil?
  data = settings.map do |setting|
    raise("setting_name \"#{setting[:name]}\" not valid") unless valid_setting?(setting[:name])
    {id: setting[:name], value: setting[:value]}
  end
  data = {items: data}
  cf_patch(path: "/zones/#{zone_id}/settings", data: data)
end

#zone(zone_id:) ⇒ Object

return all the details for a given zone_id zone_details(zone_id: id_of_my_zone



50
51
52
53
# File 'lib/cloudflare_client/zone.rb', line 50

def zone(zone_id:)
  raise('zone_id required') if zone_id.nil?
  cf_get(path: "/zones/#{zone_id}")
end

#zone_activation_check(zone_id:) ⇒ Object

request another zone activation (ssl) check zone_activation_check(zone_id:)



42
43
44
45
# File 'lib/cloudflare_client/zone.rb', line 42

def zone_activation_check(zone_id:)
  raise('zone_id required') if zone_id.nil?
  cf_put(path: "/zones/#{zone_id}/activation_check")
end

#zone_setting(zone_id:, name:) ⇒ Object

there are a lot of settings that can be returned.



101
102
103
104
105
# File 'lib/cloudflare_client/zone.rb', line 101

def zone_setting(zone_id:, name:)
  raise('zone_id required') if zone_id.nil?
  raise('setting_name not valid') if name.nil? || !valid_setting?(name)
  cf_get(path: "/zones/#{zone_id}/settings/#{name}")
end

#zone_settings(zone_id:) ⇒ Object

return all settings for a given zone



94
95
96
97
# File 'lib/cloudflare_client/zone.rb', line 94

def zone_settings(zone_id:)
  raise('zone_id required') if zone_id.nil?
  cf_get(path: "/zones/#{zone_id}/settings")
end

#zones(name: nil, status: nil, per_page: 50, page: 1) ⇒ Object

list_zones will either list all zones or search for zones based on params results are paginated! list_zones(name: name_of_zone, status: active|pending, page: page_no)



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cloudflare_client/zone.rb', line 12

def zones(name: nil, status: nil, per_page: 50, page: 1)
  params            = {}
  params[:per_page] = per_page
  params[:page]     = page
  params[:name]     = name unless name.nil?

  unless status.nil?
    raise "status must be one of #{VALID_ZONE_STATUSES.flatten}" unless VALID_ZONE_STATUSES.include?(status)
    params[:status] = status
  end

  cf_get(path: '/zones', params: params)
end