Skip to content

Commit

Permalink
Add rubocop fixups and configuration
Browse files Browse the repository at this point in the history
Use the rubocop generated configuration to ignore any major changes that
can't be auto-corrected.
  • Loading branch information
benwh committed May 17, 2020
1 parent d0e017e commit 3351fab
Show file tree
Hide file tree
Showing 11 changed files with 418 additions and 292 deletions.
11 changes: 11 additions & 0 deletions .rubocop.yml
@@ -1,4 +1,6 @@
---
inherit_from: .rubocop_todo.yml

AllCops:
# Matches the minimum version in .travis.yml
TargetRubyVersion: 2.4
Expand Down Expand Up @@ -33,3 +35,12 @@ Style/HashTransformValues:

Style/SlicingWithRange:
Enabled: true

Style/TrailingCommaInHashLiteral:
EnforcedStyleForMultiline: comma

Style/TrailingCommaInArrayLiteral:
EnforcedStyleForMultiline: comma

Style/TrailingCommaInArguments:
EnforcedStyleForMultiline: comma
98 changes: 98 additions & 0 deletions .rubocop_todo.yml
@@ -0,0 +1,98 @@
---
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2020-05-12 17:34:19 +0100 using RuboCop version 0.83.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 3
# Configuration parameters: IgnoredMethods.
Metrics/AbcSize:
Max: 50

Metrics/BlockLength:
Enabled: false

Metrics/ClassLength:
Enabled: false

Metrics/MethodLength:
Max: 30

# Offense count: 1
# Configuration parameters: IgnoredPatterns.
# SupportedStyles: snake_case, camelCase
Naming/MethodName:
EnforcedStyle: snake_case

# Offense count: 3
# Cop supports --auto-correct.
# Configuration parameters: AutoCorrect, EnforcedStyle.
# SupportedStyles: nested, compact
Style/ClassAndModuleChildren:
Exclude:
- 'lib/fluent/plugin/in_gcloud_pubsub.rb'
- 'lib/fluent/plugin/out_gcloud_pubsub.rb'
- 'test/test_helper.rb'

# Offense count: 6
Style/Documentation:
Exclude:
- 'spec/**/*'
- 'test/**/*'
- 'lib/fluent/plugin/gcloud_pubsub/client.rb'
- 'lib/fluent/plugin/in_gcloud_pubsub.rb'
- 'lib/fluent/plugin/out_gcloud_pubsub.rb'

# Offense count: 1
# Configuration parameters: AllowedVariables.
Style/GlobalVars:
Exclude:
- 'test/test_helper.rb'

# Offense count: 1
# Configuration parameters: MinBodyLength.
Style/GuardClause:
Exclude:
- 'lib/fluent/plugin/gcloud_pubsub/client.rb'

# Offense count: 2
# Cop supports --auto-correct.
Style/IfUnlessModifier:
Exclude:
- 'lib/fluent/plugin/gcloud_pubsub/client.rb'

# Offense count: 1
Style/MethodMissingSuper:
Exclude:
- 'test/test_helper.rb'

# Offense count: 1
Style/MissingRespondToMissing:
Exclude:
- 'test/test_helper.rb'

# Offense count: 260
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline.
# SupportedStyles: single_quotes, double_quotes
Style/StringLiterals:
Exclude:
- 'Gemfile'
- 'Rakefile'
- 'fluent-plugin-gcloud-pubsub-custom.gemspec'
- 'lib/fluent/plugin/gcloud_pubsub/client.rb'
- 'lib/fluent/plugin/in_gcloud_pubsub.rb'
- 'lib/fluent/plugin/out_gcloud_pubsub.rb'
- 'test/plugin/test_in_gcloud_pubsub.rb'
- 'test/plugin/test_out_gcloud_pubsub.rb'
- 'test/test_helper.rb'

# Offense count: 36
# Cop supports --auto-correct.
# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Layout/LineLength:
Max: 120
4 changes: 3 additions & 1 deletion Gemfile
@@ -1,3 +1,5 @@
source 'https://rubygems.org'
# frozen_string_literal: true

source "https://rubygems.org"

gemspec
12 changes: 7 additions & 5 deletions Rakefile
@@ -1,12 +1,14 @@
require 'bundler'
# frozen_string_literal: true

require "bundler"
Bundler::GemHelper.install_tasks

require 'rake/testtask'
require "rake/testtask"

Rake::TestTask.new(:test) do |test|
test.libs << 'lib' << 'test'
test.test_files = FileList['test/plugin/test_*.rb']
test.libs << "lib" << "test"
test.test_files = FileList["test/plugin/test_*.rb"]
test.verbose = true
end

task :default => [:build]
task default: [:build]
9 changes: 5 additions & 4 deletions fluent-plugin-gcloud-pubsub-custom.gemspec
@@ -1,5 +1,6 @@
# encoding: utf-8
$:.push File.expand_path('../lib', __FILE__)
# frozen_string_literal: true

$LOAD_PATH.push File.expand_path("lib", __dir__)

Gem::Specification.new do |gem|
gem.name = "fluent-plugin-gcloud-pubsub-custom"
Expand All @@ -12,8 +13,8 @@ Gem::Specification.new do |gem|
gem.email = "msparrow17@gmail.com"
gem.files = `git ls-files`.split("\n")
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
gem.require_paths = ['lib']
gem.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
gem.require_paths = ["lib"]

gem.add_runtime_dependency "fluentd", [">= 0.14.15", "< 2"]
gem.add_runtime_dependency "google-cloud-pubsub", "~> 0.30.0"
Expand Down
33 changes: 16 additions & 17 deletions lib/fluent/plugin/gcloud_pubsub/client.rb
@@ -1,4 +1,6 @@
require 'google/cloud/pubsub'
# frozen_string_literal: true

require "google/cloud/pubsub"

module Fluent
module GcloudPubSub
Expand All @@ -9,12 +11,13 @@ class RetryableError < Error

class Message
attr_reader :message, :attributes
def initialize(message, attributes={})

def initialize(message, attributes = {})
@message = message
@attributes = attributes
end

def bytesize()
def bytesize
attr_size = 0
@attributes.each do |key, val|
attr_size += key.bytesize + val.bytesize
Expand All @@ -31,15 +34,11 @@ def initialize(project, key, autocreate_topic)
end

def topic(topic_name)
return @topics[topic_name] if @topics.has_key? topic_name
return @topics[topic_name] if @topics.key? topic_name

client = @pubsub.topic topic_name
if client.nil? && @autocreate_topic
client = @pubsub.create_topic topic_name
end
if client.nil?
raise Error.new "topic:#{topic_name} does not exist."
end
client = @pubsub.create_topic topic_name if client.nil? && @autocreate_topic
raise Error, "topic:#{topic_name} does not exist." if client.nil?

@topics[topic_name] = client
client
Expand All @@ -51,8 +50,8 @@ def publish(topic_name, messages)
batch.publish m.message, m.attributes
end
end
rescue Google::Cloud::UnavailableError, Google::Cloud::DeadlineExceededError, Google::Cloud::InternalError => ex
raise RetryableError.new "Google api returns error:#{ex.class.to_s} message:#{ex.to_s}"
rescue Google::Cloud::UnavailableError, Google::Cloud::DeadlineExceededError, Google::Cloud::InternalError => e
raise RetryableError, "Google api returns error:#{e.class} message:#{e}"
end
end

Expand All @@ -65,19 +64,19 @@ def initialize(project, key, topic_name, subscription_name)
topic = pubsub.topic topic_name
@client = topic.subscription subscription_name
end
raise Error.new "subscription:#{subscription_name} does not exist." if @client.nil?
raise Error, "subscription:#{subscription_name} does not exist." if @client.nil?
end

def pull(immediate, max)
@client.pull immediate: immediate, max: max
rescue Google::Cloud::UnavailableError, Google::Cloud::DeadlineExceededError, Google::Cloud::InternalError => ex
raise RetryableError.new "Google pull api returns error:#{ex.class.to_s} message:#{ex.to_s}"
rescue Google::Cloud::UnavailableError, Google::Cloud::DeadlineExceededError, Google::Cloud::InternalError => e
raise RetryableError, "Google pull api returns error:#{e.class} message:#{e}"
end

def acknowledge(messages)
@client.acknowledge messages
rescue Google::Cloud::UnavailableError, Google::Cloud::DeadlineExceededError, Google::Cloud::InternalError => ex
raise RetryableError.new "Google acknowledge api returns error:#{ex.class.to_s} message:#{ex.to_s}"
rescue Google::Cloud::UnavailableError, Google::Cloud::DeadlineExceededError, Google::Cloud::InternalError => e
raise RetryableError, "Google acknowledge api returns error:#{e.class} message:#{e}"
end
end
end
Expand Down

0 comments on commit 3351fab

Please sign in to comment.