Browse Source

gem: fix applicable rubocop todos

cn 8 months ago
parent
commit
21857959b5

+ 43
- 0
.rubocop.yml View File

@@ -6,8 +6,51 @@ AllCops:
6 6
 Gemspec/OrderedDependencies:
7 7
   Enabled: false
8 8
 
9
+# allows nicer usage of private_class_method
10
+Layout/EmptyLinesAroundArguments:
11
+  Enabled: false
12
+
13
+Layout/SpaceInsideHashLiteralBraces:
14
+  Enabled: false
15
+
16
+Metrics/AbcSize:
17
+  Enabled: false
18
+
19
+Metrics/BlockLength:
20
+  Enabled: false
21
+
22
+Metrics/ClassLength:
23
+  Enabled: false
24
+
9 25
 Metrics/LineLength:
10 26
   Max: 200
11 27
 
28
+Metrics/MethodLength:
29
+  Enabled: false
30
+
31
+Style/ConditionalAssignment:
32
+  Enabled: false
33
+
12 34
 Style/Documentation:
13 35
   Enabled: false
36
+
37
+Style/FormatStringToken:
38
+  Enabled: false
39
+
40
+Style/FrozenStringLiteralComment:
41
+  Enabled: false
42
+
43
+Style/GuardClause:
44
+  Enabled: false
45
+
46
+Style/IdenticalConditionalBranches:
47
+  Enabled: false
48
+
49
+Style/InverseMethods:
50
+  Enabled: false
51
+
52
+Style/NegatedIf:
53
+  Enabled: false
54
+
55
+Style/SymbolArray:
56
+  Enabled: false

+ 1
- 302
.rubocop_todo.yml View File

@@ -1,308 +1,7 @@
1 1
 # This configuration was generated by
2 2
 # `rubocop --auto-gen-config`
3
-# on 2018-02-23 11:13:02 +0100 using RuboCop version 0.52.1.
3
+# on 2018-02-23 12:54:10 +0100 using RuboCop version 0.52.1.
4 4
 # The point is for the user to remove these configuration records
5 5
 # one by one as the offenses are removed from the code base.
6 6
 # Note that changes in the inspected code, or installation of new
7 7
 # versions of RuboCop, may require this file to be generated again.
8
-
9
-# Offense count: 1
10
-# Cop supports --auto-correct.
11
-# Configuration parameters: AllowForAlignment, ForceEqualSignAlignment.
12
-Layout/ExtraSpacing:
13
-  Exclude:
14
-    - 'dyndnsd.gemspec'
15
-
16
-# Offense count: 1
17
-# Cop supports --auto-correct.
18
-Layout/SpaceAfterComma:
19
-  Exclude:
20
-    - 'lib/dyndnsd/generator/bind.rb'
21
-
22
-# Offense count: 3
23
-# Cop supports --auto-correct.
24
-# Configuration parameters: AllowForAlignment.
25
-Layout/SpaceAroundOperators:
26
-  Exclude:
27
-    - 'dyndnsd.gemspec'
28
-
29
-# Offense count: 38
30
-# Cop supports --auto-correct.
31
-# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces.
32
-# SupportedStyles: space, no_space, compact
33
-# SupportedStylesForEmptyBraces: space, no_space
34
-Layout/SpaceInsideHashLiteralBraces:
35
-  Exclude:
36
-    - 'lib/dyndnsd.rb'
37
-    - 'lib/dyndnsd/responder/dyndns_style.rb'
38
-    - 'lib/dyndnsd/responder/rest_style.rb'
39
-
40
-# Offense count: 1
41
-# Cop supports --auto-correct.
42
-# Configuration parameters: EnforcedStyle.
43
-# SupportedStyles: final_newline, final_blank_line
44
-Layout/TrailingBlankLines:
45
-  Exclude:
46
-    - 'spec/support/dummy_database.rb'
47
-
48
-# Offense count: 8
49
-# Cop supports --auto-correct.
50
-Layout/TrailingWhitespace:
51
-  Exclude:
52
-    - 'lib/dyndnsd/database.rb'
53
-    - 'lib/dyndnsd/updater/command_with_bind_zone.rb'
54
-    - 'spec/support/dummy_database.rb'
55
-
56
-# Offense count: 4
57
-Lint/IneffectiveAccessModifier:
58
-  Exclude:
59
-    - 'lib/dyndnsd.rb'
60
-
61
-# Offense count: 1
62
-# Cop supports --auto-correct.
63
-Lint/ScriptPermission:
64
-  Exclude:
65
-    - 'lib/dyndnsd.rb'
66
-
67
-# Offense count: 5
68
-# Cop supports --auto-correct.
69
-# Configuration parameters: AllowUnusedKeywordArguments, IgnoreEmptyMethods.
70
-Lint/UnusedMethodArgument:
71
-  Exclude:
72
-    - 'lib/dyndnsd.rb'
73
-    - 'lib/dyndnsd/responder/dyndns_style.rb'
74
-    - 'lib/dyndnsd/responder/rest_style.rb'
75
-
76
-# Offense count: 1
77
-Lint/UselessAssignment:
78
-  Exclude:
79
-    - 'spec/daemon_spec.rb'
80
-
81
-# Offense count: 5
82
-Metrics/AbcSize:
83
-  Max: 31
84
-
85
-# Offense count: 1
86
-# Configuration parameters: CountComments, ExcludedMethods.
87
-Metrics/BlockLength:
88
-  Max: 159
89
-
90
-# Offense count: 1
91
-# Configuration parameters: CountComments.
92
-Metrics/ClassLength:
93
-  Max: 149
94
-
95
-# Offense count: 6
96
-# Configuration parameters: CountComments.
97
-Metrics/MethodLength:
98
-  Max: 19
99
-
100
-# Offense count: 2
101
-Naming/AccessorMethodName:
102
-  Exclude:
103
-    - 'lib/dyndnsd/responder/dyndns_style.rb'
104
-    - 'lib/dyndnsd/responder/rest_style.rb'
105
-
106
-# Offense count: 3
107
-# Configuration parameters: NamePrefix, NamePrefixBlacklist, NameWhitelist, MethodDefinitionMacros.
108
-# NamePrefix: is_, has_, have_
109
-# NamePrefixBlacklist: is_, has_, have_
110
-# NameWhitelist: is_a?
111
-# MethodDefinitionMacros: define_method, define_singleton_method
112
-Naming/PredicateName:
113
-  Exclude:
114
-    - 'spec/**/*'
115
-    - 'lib/dyndnsd.rb'
116
-    - 'lib/dyndnsd/helper.rb'
117
-
118
-# Offense count: 1
119
-# Cop supports --auto-correct.
120
-# Configuration parameters: AutoCorrect.
121
-Security/JSONLoad:
122
-  Exclude:
123
-    - 'lib/dyndnsd/database.rb'
124
-
125
-# Offense count: 1
126
-# Cop supports --auto-correct.
127
-Security/YAMLLoad:
128
-  Exclude:
129
-    - 'lib/dyndnsd.rb'
130
-
131
-# Offense count: 4
132
-# Cop supports --auto-correct.
133
-# Configuration parameters: EnforcedStyle.
134
-# SupportedStyles: always, conditionals
135
-Style/AndOr:
136
-  Exclude:
137
-    - 'lib/dyndnsd.rb'
138
-
139
-# Offense count: 1
140
-# Cop supports --auto-correct.
141
-Style/ColonMethodCall:
142
-  Exclude:
143
-    - 'lib/dyndnsd.rb'
144
-
145
-# Offense count: 3
146
-# Cop supports --auto-correct.
147
-# Configuration parameters: EnforcedStyle, SingleLineConditionsOnly, IncludeTernaryExpressions.
148
-# SupportedStyles: assign_to_condition, assign_inside_condition
149
-Style/ConditionalAssignment:
150
-  Exclude:
151
-    - 'lib/dyndnsd.rb'
152
-    - 'lib/dyndnsd/database.rb'
153
-
154
-# Offense count: 2
155
-# Cop supports --auto-correct.
156
-Style/DefWithParentheses:
157
-  Exclude:
158
-    - 'lib/dyndnsd.rb'
159
-
160
-# Offense count: 1
161
-# Cop supports --auto-correct.
162
-# Configuration parameters: EnforcedStyle.
163
-# SupportedStyles: format, sprintf, percent
164
-Style/FormatString:
165
-  Exclude:
166
-    - 'lib/dyndnsd.rb'
167
-
168
-# Offense count: 3
169
-# Configuration parameters: .
170
-# SupportedStyles: annotated, template, unannotated
171
-Style/FormatStringToken:
172
-  EnforcedStyle: unannotated
173
-
174
-# Offense count: 15
175
-# Cop supports --auto-correct.
176
-# Configuration parameters: EnforcedStyle.
177
-# SupportedStyles: when_needed, always, never
178
-Style/FrozenStringLiteralComment:
179
-  Exclude:
180
-    - 'Gemfile'
181
-    - 'Rakefile'
182
-    - 'dyndnsd.gemspec'
183
-    - 'lib/dyndnsd.rb'
184
-    - 'lib/dyndnsd/database.rb'
185
-    - 'lib/dyndnsd/generator/bind.rb'
186
-    - 'lib/dyndnsd/helper.rb'
187
-    - 'lib/dyndnsd/responder/dyndns_style.rb'
188
-    - 'lib/dyndnsd/responder/rest_style.rb'
189
-    - 'lib/dyndnsd/updater/command_with_bind_zone.rb'
190
-    - 'lib/dyndnsd/version.rb'
191
-    - 'spec/daemon_spec.rb'
192
-    - 'spec/spec_helper.rb'
193
-    - 'spec/support/dummy_database.rb'
194
-    - 'spec/support/dummy_updater.rb'
195
-
196
-# Offense count: 2
197
-# Configuration parameters: MinBodyLength.
198
-Style/GuardClause:
199
-  Exclude:
200
-    - 'lib/dyndnsd/responder/dyndns_style.rb'
201
-    - 'lib/dyndnsd/responder/rest_style.rb'
202
-
203
-# Offense count: 3
204
-# Cop supports --auto-correct.
205
-# Configuration parameters: UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols.
206
-# SupportedStyles: ruby19, hash_rockets, no_mixed_keys, ruby19_no_mixed_keys
207
-Style/HashSyntax:
208
-  EnforcedStyle: hash_rockets
209
-
210
-# Offense count: 2
211
-Style/IdenticalConditionalBranches:
212
-  Exclude:
213
-    - 'lib/dyndnsd.rb'
214
-
215
-# Offense count: 1
216
-# Cop supports --auto-correct.
217
-# Configuration parameters: InverseMethods, InverseBlocks.
218
-Style/InverseMethods:
219
-  Exclude:
220
-    - 'lib/dyndnsd.rb'
221
-
222
-# Offense count: 1
223
-# Cop supports --auto-correct.
224
-Style/MethodCallWithoutArgsParentheses:
225
-  Exclude:
226
-    - 'lib/dyndnsd.rb'
227
-
228
-# Offense count: 1
229
-# Cop supports --auto-correct.
230
-Style/MutableConstant:
231
-  Exclude:
232
-    - 'lib/dyndnsd/version.rb'
233
-
234
-# Offense count: 6
235
-# Cop supports --auto-correct.
236
-# Configuration parameters: EnforcedStyle.
237
-# SupportedStyles: both, prefix, postfix
238
-Style/NegatedIf:
239
-  Exclude:
240
-    - 'lib/dyndnsd.rb'
241
-    - 'lib/dyndnsd/helper.rb'
242
-
243
-# Offense count: 8
244
-# Cop supports --auto-correct.
245
-Style/Not:
246
-  Exclude:
247
-    - 'lib/dyndnsd.rb'
248
-    - 'lib/dyndnsd/helper.rb'
249
-
250
-# Offense count: 6
251
-# Cop supports --auto-correct.
252
-# Configuration parameters: EnforcedStyle.
253
-# SupportedStyles: short, verbose
254
-Style/PreferredHashMethods:
255
-  Exclude:
256
-    - 'lib/dyndnsd.rb'
257
-    - 'lib/dyndnsd/responder/dyndns_style.rb'
258
-    - 'lib/dyndnsd/responder/rest_style.rb'
259
-
260
-# Offense count: 1
261
-# Cop supports --auto-correct.
262
-Style/RedundantBegin:
263
-  Exclude:
264
-    - 'lib/dyndnsd/helper.rb'
265
-
266
-# Offense count: 1
267
-# Cop supports --auto-correct.
268
-# Configuration parameters: AllowAsExpressionSeparator.
269
-Style/Semicolon:
270
-  Exclude:
271
-    - 'lib/dyndnsd.rb'
272
-
273
-# Offense count: 2
274
-# Cop supports --auto-correct.
275
-# Configuration parameters: .
276
-# SupportedStyles: use_perl_names, use_english_names
277
-Style/SpecialGlobalVars:
278
-  EnforcedStyle: use_perl_names
279
-
280
-# Offense count: 80
281
-# Cop supports --auto-correct.
282
-# Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline.
283
-# SupportedStyles: single_quotes, double_quotes
284
-Style/StringLiterals:
285
-  Exclude:
286
-    - 'dyndnsd.gemspec'
287
-    - 'lib/dyndnsd.rb'
288
-    - 'lib/dyndnsd/generator/bind.rb'
289
-    - 'lib/dyndnsd/responder/dyndns_style.rb'
290
-    - 'lib/dyndnsd/responder/rest_style.rb'
291
-    - 'lib/dyndnsd/version.rb'
292
-    - 'spec/daemon_spec.rb'
293
-
294
-# Offense count: 1
295
-# Cop supports --auto-correct.
296
-# Configuration parameters: MinSize.
297
-# SupportedStyles: percent, brackets
298
-Style/SymbolArray:
299
-  EnforcedStyle: brackets
300
-
301
-# Offense count: 2
302
-# Cop supports --auto-correct.
303
-# Configuration parameters: IgnoredMethods.
304
-# IgnoredMethods: respond_to, define_method
305
-Style/SymbolProc:
306
-  Exclude:
307
-    - 'lib/dyndnsd.rb'
308
-    - 'lib/dyndnsd/database.rb'

+ 1
- 1
Rakefile View File

@@ -5,4 +5,4 @@ require 'rubocop/rake_task'
5 5
 RSpec::Core::RakeTask.new(:spec)
6 6
 RuboCop::RakeTask.new
7 7
 
8
-task :default => [:rubocop, :spec]
8
+task default: [:rubocop, :spec]

+ 5
- 5
dyndnsd.gemspec View File

@@ -1,19 +1,19 @@
1 1
 
2
-$:.push File.expand_path("../lib", __FILE__)
2
+$LOAD_PATH.push File.expand_path('../lib', __FILE__)
3 3
 
4 4
 require 'dyndnsd/version'
5 5
 
6 6
 Gem::Specification.new do |s|
7
-  s.name  = 'dyndnsd'
7
+  s.name = 'dyndnsd'
8 8
   s.version = Dyndnsd::VERSION
9 9
   s.summary = 'dyndnsd.rb'
10 10
   s.description = 'A small, lightweight and extensible DynDNS server written with Ruby and Rack.'
11
-  s.author  = 'Christian Nicolai'
11
+  s.author = 'Christian Nicolai'
12 12
   s.email = 'chrnicolai@gmail.com'
13
-  s.homepage  = 'https://github.com/cmur2/dyndnsd'
13
+  s.homepage = 'https://github.com/cmur2/dyndnsd'
14 14
   s.license = 'Apache-2.0'
15 15
 
16
-  s.files = `git ls-files`.split($/)
16
+  s.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
17 17
   s.test_files = s.files.grep(%r{^(test|spec|features)/})
18 18
   s.require_paths = ['lib']
19 19
   s.executables = ['dyndnsd']

+ 42
- 39
lib/dyndnsd.rb View File

@@ -27,8 +27,8 @@ module Dyndnsd
27 27
   end
28 28
 
29 29
   class LogFormatter
30
-    def call(lvl, time, progname, msg)
31
-      "[%s] %-5s %s\n" % [Time.now.strftime('%Y-%m-%d %H:%M:%S'), lvl, msg.to_s]
30
+    def call(lvl, _time, _progname, msg)
31
+      format("[%s] %-5s %s\n", Time.now.strftime('%Y-%m-%d %H:%M:%S'), lvl, msg.to_s)
32 32
     end
33 33
   end
34 34
 
@@ -42,12 +42,15 @@ module Dyndnsd
42 42
       @db.load
43 43
       @db['serial'] ||= 1
44 44
       @db['hosts'] ||= {}
45
-      (@db.save; @updater.update(@db)) if @db.changed?
45
+      if @db.changed?
46
+        @db.save
47
+        @updater.update(@db)
48
+      end
46 49
     end
47 50
 
48
-    def is_authorized?(username, password)
49
-      allow = ((@users.has_key? username) and (@users[username]['password'] == password))
50
-      if not allow
51
+    def authorized?(username, password)
52
+      allow = ((@users.key? username) && (@users[username]['password'] == password))
53
+      if !allow
51 54
         Dyndnsd.logger.warn "Login failed for #{username}"
52 55
         Metriks.meter('requests.auth_failed').mark
53 56
       end
@@ -55,40 +58,40 @@ module Dyndnsd
55 58
     end
56 59
 
57 60
     def call(env)
58
-      return [422, {'X-DynDNS-Response' => 'method_forbidden'}, []] if env["REQUEST_METHOD"] != "GET"
59
-      return [422, {'X-DynDNS-Response' => 'not_found'}, []] if env["PATH_INFO"] != "/nic/update"
61
+      return [422, {'X-DynDNS-Response' => 'method_forbidden'}, []] if env['REQUEST_METHOD'] != 'GET'
62
+      return [422, {'X-DynDNS-Response' => 'not_found'}, []] if env['PATH_INFO'] != '/nic/update'
60 63
 
61 64
       handle_dyndns_request(env)
62 65
     end
63 66
 
64 67
     def self.run!
65 68
       if ARGV.length != 1
66
-        puts "Usage: dyndnsd config_file"
69
+        puts 'Usage: dyndnsd config_file'
67 70
         exit 1
68 71
       end
69 72
 
70 73
       config_file = ARGV[0]
71 74
 
72
-      if not File.file?(config_file)
73
-        puts "Config file not found!"
75
+      if !File.file?(config_file)
76
+        puts 'Config file not found!'
74 77
         exit 1
75 78
       end
76 79
 
77 80
       puts "DynDNSd version #{Dyndnsd::VERSION}"
78 81
       puts "Using config file #{config_file}"
79 82
 
80
-      config = YAML::load(File.open(config_file, 'r') { |f| f.read })
83
+      config = YAML.safe_load(File.open(config_file, 'r', &:read))
81 84
 
82 85
       setup_logger(config)
83 86
 
84
-      Dyndnsd.logger.info "Starting..."
87
+      Dyndnsd.logger.info 'Starting...'
85 88
 
86 89
       # drop priviliges as soon as possible
87 90
       # NOTE: first change group than user
88 91
       Process::Sys.setgid(Etc.getgrnam(config['group']).gid) if config['group']
89 92
       Process::Sys.setuid(Etc.getpwnam(config['user']).uid) if config['user']
90 93
 
91
-      setup_traps()
94
+      setup_traps
92 95
 
93 96
       setup_monitoring(config)
94 97
 
@@ -97,12 +100,12 @@ module Dyndnsd
97 100
 
98 101
     private
99 102
 
100
-    def extract_v4_and_v6_address(env, params)
101
-      return [] if not params["myip"]
103
+    def extract_v4_and_v6_address(params)
104
+      return [] if !(params['myip'])
102 105
       begin
103
-        IPAddr.new(params["myip"], Socket::AF_INET)
104
-        IPAddr.new(params["myip6"], Socket::AF_INET6)
105
-        [params["myip"], params["myip6"]]
106
+        IPAddr.new(params['myip'], Socket::AF_INET)
107
+        IPAddr.new(params['myip6'], Socket::AF_INET6)
108
+        [params['myip'], params['myip6']]
106 109
       rescue ArgumentError
107 110
         []
108 111
       end
@@ -110,23 +113,23 @@ module Dyndnsd
110 113
 
111 114
     def extract_myips(env, params)
112 115
       # require presence of myip parameter as valid IPAddr (v4) and valid myip6
113
-      return extract_v4_and_v6_address(env, params) if params.has_key?("myip6")
116
+      return extract_v4_and_v6_address(params) if params.key?('myip6')
114 117
 
115 118
       # check whether myip parameter has valid IPAddr
116
-      return [params["myip"]] if params.has_key?("myip") and Helper.is_ip_valid?(params["myip"])
119
+      return [params['myip']] if params.key?('myip') && Helper.ip_valid?(params['myip'])
117 120
 
118 121
       # check whether X-Real-IP header has valid IPAddr
119
-      return [env["HTTP_X_REAL_IP"]] if env.has_key?("HTTP_X_REAL_IP") and Helper.is_ip_valid?(env["HTTP_X_REAL_IP"])
122
+      return [env['HTTP_X_REAL_IP']] if env.key?('HTTP_X_REAL_IP') && Helper.ip_valid?(env['HTTP_X_REAL_IP'])
120 123
 
121 124
       # fallback value, always present
122
-      [env["REMOTE_ADDR"]]
125
+      [env['REMOTE_ADDR']]
123 126
     end
124 127
 
125 128
     def process_changes(hostnames, myips)
126 129
       changes = []
127 130
       hostnames.each do |hostname|
128 131
         # myips order is always deterministic
129
-        if (not @db['hosts'].include? hostname) or (@db['hosts'][hostname] != myips)
132
+        if (!@db['hosts'].include? hostname) || (@db['hosts'][hostname] != myips)
130 133
           @db['hosts'][hostname] = myips
131 134
           changes << :good
132 135
           Metriks.meter('requests.good').mark
@@ -138,7 +141,7 @@ module Dyndnsd
138 141
       changes
139 142
     end
140 143
 
141
-    def update_db()
144
+    def update_db
142 145
       @db['serial'] += 1
143 146
       Dyndnsd.logger.info "Committing update ##{@db['serial']}"
144 147
       @db.save
@@ -147,18 +150,18 @@ module Dyndnsd
147 150
     end
148 151
 
149 152
     def handle_dyndns_request(env)
150
-      params = Rack::Utils.parse_query(env["QUERY_STRING"])
153
+      params = Rack::Utils.parse_query(env['QUERY_STRING'])
151 154
 
152 155
       # require hostname parameter
153
-      return [422, {'X-DynDNS-Response' => 'hostname_missing'}, []] if not params["hostname"]
156
+      return [422, {'X-DynDNS-Response' => 'hostname_missing'}, []] if !(params['hostname'])
154 157
 
155
-      hostnames = params["hostname"].split(',')
158
+      hostnames = params['hostname'].split(',')
156 159
 
157 160
       # check for invalid hostnames
158
-      invalid_hostnames = hostnames.select { |hostname| not Helper.is_fqdn_valid?(hostname, @domain) }
161
+      invalid_hostnames = hostnames.select { |hostname| !Helper.fqdn_valid?(hostname, @domain) }
159 162
       return [422, {'X-DynDNS-Response' => 'hostname_malformed'}, []] if invalid_hostnames.any?
160 163
 
161
-      user = env["REMOTE_USER"]
164
+      user = env['REMOTE_USER']
162 165
 
163 166
       # check for hostnames that the user does not own
164 167
       forbidden_hostnames = hostnames - @users[user]['hosts']
@@ -181,29 +184,29 @@ module Dyndnsd
181 184
 
182 185
     # SETUP
183 186
 
184
-    def self.setup_logger(config)
187
+    private_class_method def self.setup_logger(config)
185 188
       if config['logfile']
186 189
         Dyndnsd.logger = Logger.new(config['logfile'])
187 190
       else
188 191
         Dyndnsd.logger = Logger.new(STDOUT)
189 192
       end
190 193
 
191
-      Dyndnsd.logger.progname = "dyndnsd"
194
+      Dyndnsd.logger.progname = 'dyndnsd'
192 195
       Dyndnsd.logger.formatter = LogFormatter.new
193 196
     end
194 197
 
195
-    def self.setup_traps()
198
+    private_class_method def self.setup_traps
196 199
       Signal.trap('INT') do
197
-        Dyndnsd.logger.info "Quitting..."
200
+        Dyndnsd.logger.info 'Quitting...'
198 201
         Rack::Handler::WEBrick.shutdown
199 202
       end
200 203
       Signal.trap('TERM') do
201
-        Dyndnsd.logger.info "Quitting..."
204
+        Dyndnsd.logger.info 'Quitting...'
202 205
         Rack::Handler::WEBrick.shutdown
203 206
       end
204 207
     end
205 208
 
206
-    def self.setup_monitoring(config)
209
+    private_class_method def self.setup_monitoring(config)
207 210
       # configure metriks
208 211
       if config['graphite']
209 212
         host = config['graphite']['host'] || 'localhost'
@@ -224,14 +227,14 @@ module Dyndnsd
224 227
       end
225 228
     end
226 229
 
227
-    def self.setup_rack(config)
230
+    private_class_method def self.setup_rack(config)
228 231
       # configure daemon
229 232
       db = Database.new(config['db'])
230 233
       updater = Updater::CommandWithBindZone.new(config['domain'], config['updater']['params']) if config['updater']['name'] == 'command_with_bind_zone'
231 234
       daemon = Daemon.new(config, db, updater)
232 235
 
233 236
       # configure rack
234
-      app = Rack::Auth::Basic.new(daemon, "DynDNS", &daemon.method(:is_authorized?))
237
+      app = Rack::Auth::Basic.new(daemon, 'DynDNS', &daemon.method(:authorized?))
235 238
 
236 239
       if config['responder'] == 'RestStyle'
237 240
         app = Responder::RestStyle.new(app)
@@ -239,7 +242,7 @@ module Dyndnsd
239 242
         app = Responder::DynDNSStyle.new(app)
240 243
       end
241 244
 
242
-      Rack::Handler::WEBrick.run app, :Host => config['host'], :Port => config['port']
245
+      Rack::Handler::WEBrick.run app, Host: config['host'], Port: config['port']
243 246
     end
244 247
   end
245 248
 end

+ 6
- 6
lib/dyndnsd/database.rb View File

@@ -4,27 +4,27 @@ require 'forwardable'
4 4
 module Dyndnsd
5 5
   class Database
6 6
     extend Forwardable
7
-    
7
+
8 8
     def_delegators :@db, :[], :[]=, :each, :has_key?
9
-  
9
+
10 10
     def initialize(db_file)
11 11
       @db_file = db_file
12 12
     end
13
-    
13
+
14 14
     def load
15 15
       if File.file?(@db_file)
16
-        @db = JSON.load(File.open(@db_file, 'r') { |f| f.read })
16
+        @db = JSON.parse(File.open(@db_file, 'r', &:read))
17 17
       else
18 18
         @db = {}
19 19
       end
20 20
       @db_hash = @db.hash
21 21
     end
22
-    
22
+
23 23
     def save
24 24
       File.open(@db_file, 'w') { |f| JSON.dump(@db, f) }
25 25
       @db_hash = @db.hash
26 26
     end
27
-    
27
+
28 28
     def changed?
29 29
       @db_hash != @db.hash
30 30
     end

+ 6
- 6
lib/dyndnsd/generator/bind.rb View File

@@ -14,21 +14,21 @@ module Dyndnsd
14 14
         out = []
15 15
         out << "$TTL #{@ttl}"
16 16
         out << "$ORIGIN #{@domain}."
17
-        out << ""
17
+        out << ''
18 18
         out << "@ IN SOA #{@dns} #{@email_addr} ( #{zone['serial']} 3h 5m 1w 1h )"
19 19
         out << "@ IN NS #{@dns}"
20
-        out << ""
21
-        zone['hosts'].each do |hostname,ips|
20
+        out << ''
21
+        zone['hosts'].each do |hostname, ips|
22 22
           ips.each do |ip|
23 23
             ip = IPAddr.new(ip).native
24
-            type = ip.ipv6? ? "AAAA" : "A"
24
+            type = ip.ipv6? ? 'AAAA' : 'A'
25 25
             name = hostname.chomp('.' + @domain)
26 26
             out << "#{name} IN #{type} #{ip}"
27 27
           end
28 28
         end
29
-        out << ""
29
+        out << ''
30 30
         out << @additional_zone_content
31
-        out << ""
31
+        out << ''
32 32
         out.join("\n")
33 33
       end
34 34
     end

+ 8
- 10
lib/dyndnsd/helper.rb View File

@@ -3,21 +3,19 @@ require 'ipaddr'
3 3
 
4 4
 module Dyndnsd
5 5
   class Helper
6
-    def self.is_fqdn_valid?(hostname, domain)
6
+    def self.fqdn_valid?(hostname, domain)
7 7
       return false if hostname.length < domain.length + 2
8
-      return false if not hostname.end_with?(domain)
8
+      return false if !hostname.end_with?(domain)
9 9
       name = hostname.chomp(domain)
10
-      return false if not name.match(/^[a-zA-Z0-9_-]+\.$/)
10
+      return false if !name.match(/^[a-zA-Z0-9_-]+\.$/)
11 11
       true
12 12
     end
13 13
 
14
-    def self.is_ip_valid?(ip)
15
-      begin
16
-        IPAddr.new(ip)
17
-        return true
18
-      rescue ArgumentError
19
-        return false
20
-      end
14
+    def self.ip_valid?(ip)
15
+      IPAddr.new(ip)
16
+      return true
17
+    rescue ArgumentError
18
+      return false
21 19
     end
22 20
   end
23 21
 end

+ 12
- 12
lib/dyndnsd/responder/dyndns_style.rb View File

@@ -8,7 +8,7 @@ module Dyndnsd
8 8
 
9 9
       def call(env)
10 10
         @app.call(env).tap do |status_code, headers, body|
11
-          if headers.has_key?("X-DynDNS-Response")
11
+          if headers.key?('X-DynDNS-Response')
12 12
             return decorate_dyndnsd_response(status_code, headers, body)
13 13
           else
14 14
             return decorate_other_response(status_code, headers, body)
@@ -20,17 +20,17 @@ module Dyndnsd
20 20
 
21 21
       def decorate_dyndnsd_response(status_code, headers, body)
22 22
         if status_code == 200
23
-          [200, {"Content-Type" => "text/plain"}, [get_success_body(body[0], body[1])]]
23
+          [200, {'Content-Type' => 'text/plain'}, [get_success_body(body[0], body[1])]]
24 24
         elsif status_code == 422
25
-          get_error_response_map[headers["X-DynDNS-Response"]]
25
+          error_response_map[headers['X-DynDNS-Response']]
26 26
         end
27 27
       end
28 28
 
29
-      def decorate_other_response(status_code, headers, body)
29
+      def decorate_other_response(status_code, headers, _body)
30 30
         if status_code == 400
31
-          [status_code, headers, ["Bad Request"]]
31
+          [status_code, headers, ['Bad Request']]
32 32
         elsif status_code == 401
33
-          [status_code, headers, ["badauth"]]
33
+          [status_code, headers, ['badauth']]
34 34
         end
35 35
       end
36 36
 
@@ -38,15 +38,15 @@ module Dyndnsd
38 38
         changes.map { |change| "#{change} #{myips.join(' ')}" }.join("\n")
39 39
       end
40 40
 
41
-      def get_error_response_map
41
+      def error_response_map
42 42
         {
43 43
           # general http errors
44
-          'method_forbidden'   => [405, {"Content-Type" => "text/plain"}, ["Method Not Allowed"]],
45
-          'not_found'          => [404, {"Content-Type" => "text/plain"}, ["Not Found"]],
44
+          'method_forbidden'   => [405, {'Content-Type' => 'text/plain'}, ['Method Not Allowed']],
45
+          'not_found'          => [404, {'Content-Type' => 'text/plain'}, ['Not Found']],
46 46
           # specific errors
47
-          'hostname_missing'   => [200, {"Content-Type" => "text/plain"}, ["notfqdn"]],
48
-          'hostname_malformed' => [200, {"Content-Type" => "text/plain"}, ["notfqdn"]],
49
-          'host_forbidden'     => [200, {"Content-Type" => "text/plain"}, ["nohost"]]
47
+          'hostname_missing'   => [200, {'Content-Type' => 'text/plain'}, ['notfqdn']],
48
+          'hostname_malformed' => [200, {'Content-Type' => 'text/plain'}, ['notfqdn']],
49
+          'host_forbidden'     => [200, {'Content-Type' => 'text/plain'}, ['nohost']]
50 50
         }
51 51
       end
52 52
     end

+ 12
- 12
lib/dyndnsd/responder/rest_style.rb View File

@@ -8,7 +8,7 @@ module Dyndnsd
8 8
 
9 9
       def call(env)
10 10
         @app.call(env).tap do |status_code, headers, body|
11
-          if headers.has_key?("X-DynDNS-Response")
11
+          if headers.key?('X-DynDNS-Response')
12 12
             return decorate_dyndnsd_response(status_code, headers, body)
13 13
           else
14 14
             return decorate_other_response(status_code, headers, body)
@@ -20,17 +20,17 @@ module Dyndnsd
20 20
 
21 21
       def decorate_dyndnsd_response(status_code, headers, body)
22 22
         if status_code == 200
23
-          [200, {"Content-Type" => "text/plain"}, [get_success_body(body[0], body[1])]]
23
+          [200, {'Content-Type' => 'text/plain'}, [get_success_body(body[0], body[1])]]
24 24
         elsif status_code == 422
25
-          get_error_response_map[headers["X-DynDNS-Response"]]
25
+          error_response_map[headers['X-DynDNS-Response']]
26 26
         end
27 27
       end
28 28
 
29
-      def decorate_other_response(status_code, headers, body)
29
+      def decorate_other_response(status_code, headers, _body)
30 30
         if status_code == 400
31
-          [status_code, headers, ["Bad Request"]]
31
+          [status_code, headers, ['Bad Request']]
32 32
         elsif status_code == 401
33
-          [status_code, headers, ["Unauthorized"]]
33
+          [status_code, headers, ['Unauthorized']]
34 34
         end
35 35
       end
36 36
 
@@ -38,15 +38,15 @@ module Dyndnsd
38 38
         changes.map { |change| change == :good ? "Changed to #{myips.join(' ')}" : "No change needed for #{myips.join(' ')}" }.join("\n")
39 39
       end
40 40
 
41
-      def get_error_response_map
41
+      def error_response_map
42 42
         {
43 43
           # general http errors
44
-          'method_forbidden'   => [405, {"Content-Type" => "text/plain"}, ["Method Not Allowed"]],
45
-          'not_found'          => [404, {"Content-Type" => "text/plain"}, ["Not Found"]],
44
+          'method_forbidden'   => [405, {'Content-Type' => 'text/plain'}, ['Method Not Allowed']],
45
+          'not_found'          => [404, {'Content-Type' => 'text/plain'}, ['Not Found']],
46 46
           # specific errors
47
-          'hostname_missing'   => [422, {"Content-Type" => "text/plain"}, ["Hostname missing"]],
48
-          'hostname_malformed' => [422, {"Content-Type" => "text/plain"}, ["Hostname malformed"]],
49
-          'host_forbidden'     => [403, {"Content-Type" => "text/plain"}, ["Forbidden"]]
47
+          'hostname_missing'   => [422, {'Content-Type' => 'text/plain'}, ['Hostname missing']],
48
+          'hostname_malformed' => [422, {'Content-Type' => 'text/plain'}, ['Hostname malformed']],
49
+          'host_forbidden'     => [403, {'Content-Type' => 'text/plain'}, ['Forbidden']]
50 50
         }
51 51
       end
52 52
     end

+ 1
- 1
lib/dyndnsd/updater/command_with_bind_zone.rb View File

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

+ 1
- 1
lib/dyndnsd/version.rb View File

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

+ 3
- 3
spec/daemon_spec.rb View File

@@ -20,9 +20,9 @@ describe Dyndnsd::Daemon do
20 20
     updater = Dyndnsd::Updater::Dummy.new
21 21
     daemon = Dyndnsd::Daemon.new(config, db, updater)
22 22
 
23
-    app = Rack::Auth::Basic.new(daemon, "DynDNS", &daemon.method(:is_authorized?))
23
+    app = Rack::Auth::Basic.new(daemon, 'DynDNS', &daemon.method(:authorized?))
24 24
 
25
-    app = Dyndnsd::Responder::DynDNSStyle.new(app)
25
+    Dyndnsd::Responder::DynDNSStyle.new(app)
26 26
   end
27 27
 
28 28
   it 'requires authentication' do
@@ -189,7 +189,7 @@ describe Dyndnsd::Daemon do
189 189
 
190 190
     get '/nic/update?hostname=foo.example.org&myip=1.2.3.4&myip6=2001:db8::1'
191 191
     expect(last_response).to be_ok
192
-    expect(last_response.body).to eq("good 1.2.3.4 2001:db8::1")
192
+    expect(last_response.body).to eq('good 1.2.3.4 2001:db8::1')
193 193
 
194 194
     get '/nic/update?hostname=foo.example.org&myip=BROKENIP&myip6=2001:db8::1'
195 195
     expect(last_response).to be_ok

+ 1
- 3
spec/support/dummy_database.rb View File

@@ -4,7 +4,7 @@ require 'forwardable'
4 4
 module Dyndnsd
5 5
   class DummyDatabase
6 6
     extend Forwardable
7
-  
7
+
8 8
     def_delegators :@db, :[], :[]=, :each, :has_key?
9 9
 
10 10
     def initialize(db_init)
@@ -25,5 +25,3 @@ module Dyndnsd
25 25
     end
26 26
   end
27 27
 end
28
-
29
-