Address Ruby stylistic concerns (#3)

Address Ruby stylistic concerns (#3)

  • Use more idiomatic Ruby loop syntax
  • Import .rubocop.yml from discourse/discourse

Taken from discourse/discourse commit c6adf7f0.

  • rubocop: Remove exclusions

These paths apply to discourse/discourse but they do not apply here.

  • rubocop: Allow hard tabs

All existing source in this repo uses hard tabs for indentation.

  • rubocop: Reject redundant returns

  • Address all outstanding rubocop policy violations

  • Wrap all check-my-work helpers with make

This is the simplest thing that could possibly work.

From bada511638eacf2746406315888ad1b4b48c4ab2 Mon Sep 17 00:00:00 2001
From: Saj Goonatilleke <sg@redu.cx>
Date: Fri, 30 Nov 2018 21:17:33 +1100
Subject: [PATCH] Address Ruby stylistic concerns (#3)

* Use more idiomatic Ruby loop syntax

https://github.com/discourse/mail-receiver/commit/2b4566ca7b2c41b860862290f50c1128c4ee8934#r31497482

* Import .rubocop.yml from discourse/discourse

Taken from discourse/discourse commit c6adf7f0.

* rubocop: Remove exclusions

These paths apply to discourse/discourse but they do not apply here.

* rubocop: Allow hard tabs

All existing source in this repo uses hard tabs for indentation.

* rubocop: Reject redundant returns

* Address all outstanding rubocop policy violations

* Wrap all check-my-work helpers with make

This is the simplest thing that could possibly work.

diff --git a/.rubocop.yml b/.rubocop.yml
new file mode 100644
index 0000000..f540ae9
--- /dev/null
+++ b/.rubocop.yml
@@ -0,0 +1,105 @@
+AllCops:
+  TargetRubyVersion: 2.4
+  DisabledByDefault: true
+
+# Prefer &&/|| over and/or.
+Style/AndOr:
+  Enabled: true
+
+# Do not use braces for hash literals when they are the last argument of a
+# method call.
+Style/BracesAroundHashParameters:
+  Enabled: true
+
+# Align `when` with `case`.
+Layout/CaseIndentation:
+  Enabled: true
+
+# Align comments with method definitions.
+Layout/CommentIndentation:
+  Enabled: true
+
+# No extra empty lines.
+Layout/EmptyLines:
+  Enabled: true
+
+# Use Ruby >= 1.9 syntax for hashes. Prefer { a: :b } over { :a => :b }.
+Style/HashSyntax:
+  Enabled: true
+
+Layout/SpaceAfterColon:
+  Enabled: true
+
+Layout/SpaceAfterComma:
+  Enabled: true
+
+Layout/SpaceAroundEqualsInParameterDefault:
+  Enabled: true
+
+Layout/SpaceAroundKeyword:
+  Enabled: true
+
+Layout/SpaceAroundOperators:
+  Enabled: true
+
+Layout/SpaceBeforeFirstArg:
+    Enabled: true
+
+# Defining a method with parameters needs parentheses.
+Style/MethodDefParentheses:
+  Enabled: true
+
+# Use `foo {}` not `foo{}`.
+Layout/SpaceBeforeBlockBraces:
+  Enabled: true
+
+# Use `foo { bar }` not `foo {bar}`.
+Layout/SpaceInsideBlockBraces:
+  Enabled: true
+
+# Use `{ a: 1 }` not `{a:1}`.
+Layout/SpaceInsideHashLiteralBraces:
+  Enabled: true
+
+Layout/SpaceInsideParens:
+  Enabled: true
+
+# Blank lines should not have any spaces.
+Layout/TrailingBlankLines:
+  Enabled: true
+
+# No trailing whitespace.
+Layout/TrailingWhitespace:
+  Enabled: true
+
+Lint/Debugger:
+  Enabled: true
+
+Layout/BlockAlignment:
+  Enabled: true
+
+# Align `end` with the matching keyword or starting expression except for
+# assignments, where it should be aligned with the LHS.
+Layout/EndAlignment:
+  Enabled: true
+  EnforcedStyleAlignWith: variable
+
+# Use my_method(my_arg) not my_method( my_arg ) or my_method my_arg.
+Lint/RequireParentheses:
+  Enabled: true
+
+Lint/ShadowingOuterLocalVariable:
+  Enabled: true
+
+Layout/MultilineMethodCallIndentation:
+  Enabled: true
+  EnforcedStyle: indented
+
+Layout/AlignHash:
+  Enabled: true
+
+Bundler/OrderedGems:
+  Enabled: false
+
+Style/RedundantReturn:
+  Enabled: true
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..774a618
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,17 @@
+all:
+	$(MAKE) lint
+	$(MAKE) test
+
+.PHONY: all
+
+
+lint:
+	rubocop
+
+.PHONY: lint
+
+
+test:
+	rspec
+
+.PHONY: test
diff --git a/lib/mail_receiver/discourse_mail_receiver.rb b/lib/mail_receiver/discourse_mail_receiver.rb
index 7c821c1..b370c7c 100644
--- a/lib/mail_receiver/discourse_mail_receiver.rb
+++ b/lib/mail_receiver/discourse_mail_receiver.rb
@@ -6,7 +6,7 @@ require_relative 'mail_receiver_base'
 
 class DiscourseMailReceiver < MailReceiverBase
 
-	def initialize(env_file=nil, recipient=nil, mail=nil)
+	def initialize(env_file = nil, recipient = nil, mail = nil)
 		super(env_file)
 
 		@recipient = recipient
@@ -31,7 +31,7 @@ class DiscourseMailReceiver < MailReceiverBase
 	def process
 		uri = URI.parse(endpoint)
 		api_qs = "api_key=#{key}&api_username=#{username}"
-		if uri.query and !uri.query.empty?
+		if uri.query && !uri.query.empty?
 			uri.query += "&#{api_qs}"
 		else
 			uri.query = api_qs
diff --git a/lib/mail_receiver/fast_rejection.rb b/lib/mail_receiver/fast_rejection.rb
index 8221f22..087457c 100644
--- a/lib/mail_receiver/fast_rejection.rb
+++ b/lib/mail_receiver/fast_rejection.rb
@@ -35,7 +35,7 @@ class FastRejection < MailReceiverBase
 
 				args = {}  # reset for next request.
 			else
-				k,v = line.chomp.split('=', 2)
+				k, v = line.chomp.split('=', 2)
 				args[k] = v
 			end
 		end
@@ -65,7 +65,7 @@ class FastRejection < MailReceiverBase
 		toarg = CGI::escape(to)
 
 		api_qs = "api_key=#{key}&api_username=#{username}&from=#{fromarg}&to=#{toarg}"
-		if uri.query and !uri.query.empty?
+		if uri.query && !uri.query.empty?
 			uri.query += "&#{api_qs}"
 		else
 			uri.query = api_qs
@@ -94,7 +94,7 @@ class FastRejection < MailReceiverBase
 			return "defer_if_permit Internal error, API request failed"
 		end
 
-		return "dunno"  # let future tests also be allowed to reject this one.
+		"dunno"  # let future tests also be allowed to reject this one.
 	end
 
 	def endpoint
@@ -109,11 +109,12 @@ class FastRejection < MailReceiverBase
 			:maybe_reject_by_api,
 		]
 
-		for f in filters do
+		filters.each do |f|
 			action = send(f, args)
-			break unless action == "dunno"
+			return action if action != "dunno"
 		end
-		action
+
+		"dunno"
 	end
 
 	def maybe_reject_by_sender_domain(args)
@@ -131,7 +132,7 @@ class FastRejection < MailReceiverBase
 			return 'reject Invalid sender'
 		end
 
-		return "dunno"
+		"dunno"
 	end
 
 	def maybe_reject_by_api(args)

GitHub

2 Likes