Class: Encryptbot::Services::Cloudflare
- Inherits:
-
Object
- Object
- Encryptbot::Services::Cloudflare
- Defined in:
- lib/encryptbot/services/cloudflare.rb
Instance Attribute Summary collapse
-
#api_email ⇒ Object
Returns the value of attribute api_email.
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#dns_entry ⇒ Object
Returns the value of attribute dns_entry.
-
#dns_record ⇒ Object
Returns the value of attribute dns_record.
-
#dns_record_id ⇒ Object
Returns the value of attribute dns_record_id.
-
#domain ⇒ Object
Returns the value of attribute domain.
-
#zone_id ⇒ Object
Returns the value of attribute zone_id.
Instance Method Summary collapse
- #add_challenge ⇒ Object
- #add_dns_record ⇒ Object
- #find_dns_record ⇒ Object
- #get_zone_id ⇒ Object
-
#initialize(domain, dns_entry) ⇒ Cloudflare
constructor
A new instance of Cloudflare.
- #setup_dns_record ⇒ Object
- #update_dns_record ⇒ Object
Constructor Details
#initialize(domain, dns_entry) ⇒ Cloudflare
Returns a new instance of Cloudflare.
11 12 13 14 15 16 17 |
# File 'lib/encryptbot/services/cloudflare.rb', line 11 def initialize(domain, dns_entry) @domain = domain.to_s.gsub("*.", "") # cleanup wildcard by removing *. infront @api_key = Encryptbot.configuration.cloudflare_api_key @api_email = Encryptbot.configuration.cloudflare_email @dns_entry = dns_entry # {content: "txt-record-content", type: "TXT", name: "_acme-challenge.domain.com"} @dns_record = "#{dns_entry[:name]}.#{@domain}" end |
Instance Attribute Details
#api_email ⇒ Object
Returns the value of attribute api_email.
9 10 11 |
# File 'lib/encryptbot/services/cloudflare.rb', line 9 def api_email @api_email end |
#api_key ⇒ Object
Returns the value of attribute api_key.
9 10 11 |
# File 'lib/encryptbot/services/cloudflare.rb', line 9 def api_key @api_key end |
#dns_entry ⇒ Object
Returns the value of attribute dns_entry.
9 10 11 |
# File 'lib/encryptbot/services/cloudflare.rb', line 9 def dns_entry @dns_entry end |
#dns_record ⇒ Object
Returns the value of attribute dns_record.
9 10 11 |
# File 'lib/encryptbot/services/cloudflare.rb', line 9 def dns_record @dns_record end |
#dns_record_id ⇒ Object
Returns the value of attribute dns_record_id.
9 10 11 |
# File 'lib/encryptbot/services/cloudflare.rb', line 9 def dns_record_id @dns_record_id end |
#domain ⇒ Object
Returns the value of attribute domain.
9 10 11 |
# File 'lib/encryptbot/services/cloudflare.rb', line 9 def domain @domain end |
#zone_id ⇒ Object
Returns the value of attribute zone_id.
9 10 11 |
# File 'lib/encryptbot/services/cloudflare.rb', line 9 def zone_id @zone_id end |
Instance Method Details
#add_challenge ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/encryptbot/services/cloudflare.rb', line 19 def add_challenge begin get_zone_id setup_dns_record rescue => e raise Encryptbot::Error::CloudflareDNSError, e end end |
#add_dns_record ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/encryptbot/services/cloudflare.rb', line 53 def add_dns_record response = post("/zones/#{@zone_id}/dns_records", { type: @dns_entry[:type], name: @dns_record, content: @dns_entry[:content], ttl: 120 }) response["success"] end |
#find_dns_record ⇒ Object
46 47 48 49 50 51 |
# File 'lib/encryptbot/services/cloudflare.rb', line 46 def find_dns_record response = get("/zones/#{@zone_id}/dns_records?name=#{@dns_record}&type=#{@dns_entry[:type]}") if response["result"].any? @dns_record_id = response["result"].first["id"] end end |
#get_zone_id ⇒ Object
28 29 30 31 32 33 |
# File 'lib/encryptbot/services/cloudflare.rb', line 28 def get_zone_id response = get("/zones?name=#{@domain}") if response["result"].any? @zone_id = response["result"].first["id"] end end |
#setup_dns_record ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/encryptbot/services/cloudflare.rb', line 35 def setup_dns_record find_dns_record return false if @zone_id.nil? if @dns_record_id update_dns_record else add_dns_record end end |
#update_dns_record ⇒ Object
63 64 65 66 67 68 69 70 71 |
# File 'lib/encryptbot/services/cloudflare.rb', line 63 def update_dns_record response = put("/zones/#{@zone_id}/dns_records/#{@dns_record_id}", { type: @dns_entry[:type], name: @dns_record, content: @dns_entry[:content], ttl: 120 }) response["success"] end |