1
0
mirror of https://github.com/cmur2/dyndnsd.git synced 2025-07-01 22:30:19 +02:00

gem: fix applicable rubocop todos

This commit is contained in:
cn
2018-02-23 12:54:43 +01:00
parent 9a7c20babb
commit 21857959b5
14 changed files with 142 additions and 401 deletions

View File

@ -4,27 +4,27 @@ require 'forwardable'
module Dyndnsd
class Database
extend Forwardable
def_delegators :@db, :[], :[]=, :each, :has_key?
def initialize(db_file)
@db_file = db_file
end
def load
if File.file?(@db_file)
@db = JSON.load(File.open(@db_file, 'r') { |f| f.read })
@db = JSON.parse(File.open(@db_file, 'r', &:read))
else
@db = {}
end
@db_hash = @db.hash
end
def save
File.open(@db_file, 'w') { |f| JSON.dump(@db, f) }
@db_hash = @db.hash
end
def changed?
@db_hash != @db.hash
end

View File

@ -14,21 +14,21 @@ module Dyndnsd
out = []
out << "$TTL #{@ttl}"
out << "$ORIGIN #{@domain}."
out << ""
out << ''
out << "@ IN SOA #{@dns} #{@email_addr} ( #{zone['serial']} 3h 5m 1w 1h )"
out << "@ IN NS #{@dns}"
out << ""
zone['hosts'].each do |hostname,ips|
out << ''
zone['hosts'].each do |hostname, ips|
ips.each do |ip|
ip = IPAddr.new(ip).native
type = ip.ipv6? ? "AAAA" : "A"
type = ip.ipv6? ? 'AAAA' : 'A'
name = hostname.chomp('.' + @domain)
out << "#{name} IN #{type} #{ip}"
end
end
out << ""
out << ''
out << @additional_zone_content
out << ""
out << ''
out.join("\n")
end
end

View File

@ -3,21 +3,19 @@ require 'ipaddr'
module Dyndnsd
class Helper
def self.is_fqdn_valid?(hostname, domain)
def self.fqdn_valid?(hostname, domain)
return false if hostname.length < domain.length + 2
return false if not hostname.end_with?(domain)
return false if !hostname.end_with?(domain)
name = hostname.chomp(domain)
return false if not name.match(/^[a-zA-Z0-9_-]+\.$/)
return false if !name.match(/^[a-zA-Z0-9_-]+\.$/)
true
end
def self.is_ip_valid?(ip)
begin
IPAddr.new(ip)
return true
rescue ArgumentError
return false
end
def self.ip_valid?(ip)
IPAddr.new(ip)
return true
rescue ArgumentError
return false
end
end
end

View File

@ -8,7 +8,7 @@ module Dyndnsd
def call(env)
@app.call(env).tap do |status_code, headers, body|
if headers.has_key?("X-DynDNS-Response")
if headers.key?('X-DynDNS-Response')
return decorate_dyndnsd_response(status_code, headers, body)
else
return decorate_other_response(status_code, headers, body)
@ -20,17 +20,17 @@ module Dyndnsd
def decorate_dyndnsd_response(status_code, headers, body)
if status_code == 200
[200, {"Content-Type" => "text/plain"}, [get_success_body(body[0], body[1])]]
[200, {'Content-Type' => 'text/plain'}, [get_success_body(body[0], body[1])]]
elsif status_code == 422
get_error_response_map[headers["X-DynDNS-Response"]]
error_response_map[headers['X-DynDNS-Response']]
end
end
def decorate_other_response(status_code, headers, body)
def decorate_other_response(status_code, headers, _body)
if status_code == 400
[status_code, headers, ["Bad Request"]]
[status_code, headers, ['Bad Request']]
elsif status_code == 401
[status_code, headers, ["badauth"]]
[status_code, headers, ['badauth']]
end
end
@ -38,15 +38,15 @@ module Dyndnsd
changes.map { |change| "#{change} #{myips.join(' ')}" }.join("\n")
end
def get_error_response_map
def error_response_map
{
# general http errors
'method_forbidden' => [405, {"Content-Type" => "text/plain"}, ["Method Not Allowed"]],
'not_found' => [404, {"Content-Type" => "text/plain"}, ["Not Found"]],
'method_forbidden' => [405, {'Content-Type' => 'text/plain'}, ['Method Not Allowed']],
'not_found' => [404, {'Content-Type' => 'text/plain'}, ['Not Found']],
# specific errors
'hostname_missing' => [200, {"Content-Type" => "text/plain"}, ["notfqdn"]],
'hostname_malformed' => [200, {"Content-Type" => "text/plain"}, ["notfqdn"]],
'host_forbidden' => [200, {"Content-Type" => "text/plain"}, ["nohost"]]
'hostname_missing' => [200, {'Content-Type' => 'text/plain'}, ['notfqdn']],
'hostname_malformed' => [200, {'Content-Type' => 'text/plain'}, ['notfqdn']],
'host_forbidden' => [200, {'Content-Type' => 'text/plain'}, ['nohost']]
}
end
end

View File

@ -8,7 +8,7 @@ module Dyndnsd
def call(env)
@app.call(env).tap do |status_code, headers, body|
if headers.has_key?("X-DynDNS-Response")
if headers.key?('X-DynDNS-Response')
return decorate_dyndnsd_response(status_code, headers, body)
else
return decorate_other_response(status_code, headers, body)
@ -20,17 +20,17 @@ module Dyndnsd
def decorate_dyndnsd_response(status_code, headers, body)
if status_code == 200
[200, {"Content-Type" => "text/plain"}, [get_success_body(body[0], body[1])]]
[200, {'Content-Type' => 'text/plain'}, [get_success_body(body[0], body[1])]]
elsif status_code == 422
get_error_response_map[headers["X-DynDNS-Response"]]
error_response_map[headers['X-DynDNS-Response']]
end
end
def decorate_other_response(status_code, headers, body)
def decorate_other_response(status_code, headers, _body)
if status_code == 400
[status_code, headers, ["Bad Request"]]
[status_code, headers, ['Bad Request']]
elsif status_code == 401
[status_code, headers, ["Unauthorized"]]
[status_code, headers, ['Unauthorized']]
end
end
@ -38,15 +38,15 @@ module Dyndnsd
changes.map { |change| change == :good ? "Changed to #{myips.join(' ')}" : "No change needed for #{myips.join(' ')}" }.join("\n")
end
def get_error_response_map
def error_response_map
{
# general http errors
'method_forbidden' => [405, {"Content-Type" => "text/plain"}, ["Method Not Allowed"]],
'not_found' => [404, {"Content-Type" => "text/plain"}, ["Not Found"]],
'method_forbidden' => [405, {'Content-Type' => 'text/plain'}, ['Method Not Allowed']],
'not_found' => [404, {'Content-Type' => 'text/plain'}, ['Not Found']],
# specific errors
'hostname_missing' => [422, {"Content-Type" => "text/plain"}, ["Hostname missing"]],
'hostname_malformed' => [422, {"Content-Type" => "text/plain"}, ["Hostname malformed"]],
'host_forbidden' => [403, {"Content-Type" => "text/plain"}, ["Forbidden"]]
'hostname_missing' => [422, {'Content-Type' => 'text/plain'}, ['Hostname missing']],
'hostname_malformed' => [422, {'Content-Type' => 'text/plain'}, ['Hostname malformed']],
'host_forbidden' => [403, {'Content-Type' => 'text/plain'}, ['Forbidden']]
}
end
end

View File

@ -7,7 +7,7 @@ module Dyndnsd
@command = config['command']
@generator = Generator::Bind.new(domain, config)
end
def update(zone)
# write zone file in bind syntax
File.open(@zone_file, 'w') { |f| f.write(@generator.generate(zone)) }

View File

@ -1,4 +1,4 @@
module Dyndnsd
VERSION = "1.6.1"
VERSION = '1.6.1'.freeze
end