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, 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

Constructor Details

#initialize(*args) ⇒ Zone

Zone based operations



10
11
12
# File 'lib/cloudflare_client/zone.rb', line 10

def initialize(*args)
  super
end

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)



36
37
38
39
40
41
42
43
# File 'lib/cloudflare_client/zone.rb', line 36

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



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

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)



66
67
68
69
70
71
72
73
# File 'lib/cloudflare_client/zone.rb', line 66

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



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/cloudflare_client/zone.rb', line 78

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



117
118
119
120
121
122
123
124
125
# File 'lib/cloudflare_client/zone.rb', line 117

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



56
57
58
59
# File 'lib/cloudflare_client/zone.rb', line 56

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:)



48
49
50
51
# File 'lib/cloudflare_client/zone.rb', line 48

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.



107
108
109
110
111
# File 'lib/cloudflare_client/zone.rb', line 107

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



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

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)



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cloudflare_client/zone.rb', line 18

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