Skip to content

Commit

Permalink
fix for rubocop errors
Browse files Browse the repository at this point in the history
  • Loading branch information
hophacker authored and hophacker committed Aug 30, 2019
1 parent 38afed0 commit 2a329cf
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 63 deletions.
11 changes: 9 additions & 2 deletions .rubocop.yml
Expand Up @@ -4,6 +4,7 @@ AllCops:
Exclude:
- 'bin/**/*'
- 'spec/**/*.rb'
- 'lib/generators/wechat/templates/app/controllers/wechats_controller.rb'
DisplayCopNames: true
StyleGuideCopsOnly: false
TargetRubyVersion: 2.3
Expand All @@ -21,7 +22,7 @@ Metrics/ClassLength:
Max: 250

Metrics/ModuleLength:
Max: 150
Max: 180

Metrics/MethodLength:
Max: 40
Expand All @@ -33,7 +34,13 @@ Style/NumericLiterals:
MinDigits: 7

Metrics/PerceivedComplexity:
Max: 12
Max: 25

Metrics/CyclomaticComplexity:
Max: 20

Style/AsciiComments:
Enabled: false

Metrics/BlockNesting:
Max: 4
10 changes: 7 additions & 3 deletions lib/generators/wechat/config_generator.rb
Expand Up @@ -22,12 +22,16 @@ def copy_wechat_config_model
template 'app/models/wechat_config.rb'
end

private
class << self
private

def self.next_migration_number(dirname)
::ActiveRecord::Generators::Base.next_migration_number(dirname)
def next_migration_number(dirname)
::ActiveRecord::Generators::Base.next_migration_number(dirname)
end
end

private

def migration_version
"[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]" if Rails.version >= '5.0.0'
end
Expand Down
10 changes: 7 additions & 3 deletions lib/generators/wechat/session_generator.rb
Expand Up @@ -22,12 +22,16 @@ def copy_wechat_session_model
template 'app/models/wechat_session.rb'
end

private
class << self
private

def self.next_migration_number(dirname)
::ActiveRecord::Generators::Base.next_migration_number(dirname)
def next_migration_number(dirname)
::ActiveRecord::Generators::Base.next_migration_number(dirname)
end
end

private

def migration_version
"[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]" if Rails.version >= '5.0.0'
end
Expand Down
6 changes: 3 additions & 3 deletions lib/wechat.rb
Expand Up @@ -38,15 +38,15 @@ def self.reload_config!
ApiLoader.reload_config!
end

def self.decrypt(encrypted_data, session_key, iv)
def self.decrypt(encrypted_data, session_key, ivector)
cipher = OpenSSL::Cipher.new('AES-128-CBC')
cipher.decrypt

cipher.key = Base64.decode64(session_key)
cipher.iv = Base64.decode64(iv)
cipher.iv = Base64.decode64(ivector)
decrypted_data = Base64.decode64(encrypted_data)
JSON.parse(cipher.update(decrypted_data) + cipher.final)
rescue Exception => e
rescue StandardError => e
{ 'errcode': 41003, 'errmsg': e.message }
end
end
Expand Down
43 changes: 21 additions & 22 deletions lib/wechat/api_loader.rb
Expand Up @@ -37,11 +37,9 @@ def self.reload_config!

configs.symbolize_keys!
configs.each do |key, cfg|
if cfg.is_a?(Hash)
cfg.symbolize_keys!
else
raise "wrong wechat configuration format for #{key}"
end
raise "wrong wechat configuration format for #{key}" unless cfg.is_a?(Hash)

cfg.symbolize_keys!
end

if defined?(::Rails)
Expand Down Expand Up @@ -97,26 +95,27 @@ def self.reload_config!
end

private_class_method def self.resolve_config_file(config_file, env)
if File.exist?(config_file)
# rubocop:disable Security/YAMLLoad
raw_data = YAML.load(ERB.new(File.read(config_file)).result)
# rubocop:enable Security/YAMLLoad
configs = {}
if env
# Process multiple accounts when env is given
raw_data.each do |key, value|
if key == env
configs[:default] = value
elsif m = /(.*?)_#{env}$/.match(key)
configs[m[1].to_sym] = value
end
return unless File.exist?(config_file)

# rubocop:disable Security/YAMLLoad
raw_data = YAML.load(ERB.new(File.read(config_file)).result)
# rubocop:enable Security/YAMLLoad
configs = {}
if env
# Process multiple accounts when env is given
raw_data.each do |key, value|
if key == env
configs[:default] = value
else
m = /(.*?)_#{env}$/.match(key)
configs[m[1].to_sym] = value if m
end
else
# Treat is as one account when env is omitted
configs[:default] = raw_data
end
configs
else
# Treat is as one account when env is omitted
configs[:default] = raw_data
end
configs
end

private_class_method def self.config_from_environment
Expand Down
2 changes: 1 addition & 1 deletion lib/wechat/cipher.rb
Expand Up @@ -53,7 +53,7 @@ def unpack(msg)
def encode_padding(data)
length = data.bytes.length
amount_to_pad = BLOCK_SIZE - (length % BLOCK_SIZE)
amount_to_pad = BLOCK_SIZE if amount_to_pad == 0
amount_to_pad = BLOCK_SIZE if amount_to_pad.zero?
padding = ([amount_to_pad].pack('c') * amount_to_pad)
data + padding
end
Expand Down
6 changes: 3 additions & 3 deletions lib/wechat/controller_api.rb
Expand Up @@ -22,10 +22,10 @@ def wechat_oauth2(scope = 'snsapi_base', page_url = nil, account = nil, &block)
if account
config = Wechat.config(account)
appid = config.corpid || config.appid
is_crop_account = !!config.corpid
is_crop_account = config.corpid.present?
else
appid = self.class.corpid || self.class.appid
is_crop_account = !!self.class.corpid
is_crop_account = self.class.corpid.present?
end

raise 'Can not get corpid or appid, so please configure it first to using wechat_oauth2' if appid.blank?
Expand Down Expand Up @@ -82,7 +82,7 @@ def generate_redirect_uri(account = nil)
Wechat.config(account).trusted_domain_fullname
else
self.class.trusted_domain_fullname
end
end
page_url = domain_name ? "#{domain_name}#{request.original_fullpath}" : request.original_url
safe_query = request.query_parameters.reject { |k, _| %w[code state access_token].include? k }.to_query
page_url.sub(request.query_string, safe_query)
Expand Down
4 changes: 2 additions & 2 deletions lib/wechat/helpers.rb
Expand Up @@ -24,7 +24,7 @@ def wechat_config_js(config_options = {})
else
controller.request.original_url
end
page_url = page_url.split('#').first if is_ios?
page_url = page_url.split('#').first if ios?
js_hash = api.jsapi_ticket.signature(page_url)

config_js = <<~WECHAT_CONFIG_JS
Expand All @@ -42,7 +42,7 @@ def wechat_config_js(config_options = {})

private

def is_ios?
def ios?
controller.request.user_agent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)
end
end
Expand Down
12 changes: 5 additions & 7 deletions lib/wechat/http_client.rb
Expand Up @@ -74,7 +74,7 @@ def request(path, header = {}, &_block)
end
end

def parse_response(response, as)
def parse_response(response, as_type)
content_type = response.headers[:content_type]
parse_as = {
%r{^application\/json} => :json,
Expand All @@ -83,7 +83,7 @@ def parse_response(response, as)
%r{^voice\/.*} => :file,
%r{^text\/html} => :xml,
%r{^text\/plain} => :probably_json
}.each_with_object([]) { |match, memo| memo << match[1] if content_type =~ match[0] }.first || as || :text
}.each_with_object([]) { |match, memo| memo << match[1] if content_type =~ match[0] }.first || as_type || :text

# try to parse response as json, fallback to user-specified format or text if failed
if parse_as == :probably_json
Expand All @@ -92,11 +92,9 @@ def parse_response(response, as)
rescue StandardError
nil
end
if data
return yield(:json, data)
else
parse_as = as || :text
end
return yield(:json, data) if data

parse_as = as_type || :text
end

case parse_as
Expand Down
30 changes: 13 additions & 17 deletions lib/wechat/responder.rb
Expand Up @@ -44,14 +44,12 @@ def on(message_type, with: nil, respond: nil, &block)

config[:with] = with
if message_type == :scan
if with.is_a?(String)
self.known_scan_key_lists = with
else
raise 'on :scan only support string in parameter with, detail see https://github.com/Eric-Guo/wechat/issues/84'
end
raise 'on :scan only support string in parameter with, detail see https://github.com/Eric-Guo/wechat/issues/84' unless with.is_a?(String)

self.known_scan_key_lists = with
end
else
raise 'Message type click, view, scan and batch_job must specify :with parameters' if %i[click view scan batch_job].include?(message_type)
elsif %i[click view scan batch_job].include?(message_type)
raise 'Message type click, view, scan and batch_job must specify :with parameters'
end

case message_type
Expand Down Expand Up @@ -90,15 +88,15 @@ def user_defined_batch_job_responders(with)
end

def user_defined_scan_responders
@scan_responders ||= []
@user_defined_scan_responders ||= []
end

def user_defined_location_responders
@location_responders ||= []
@user_defined_location_responders ||= []
end

def user_defined_label_location_responders
@label_location_responders ||= []
@user_defined_label_location_responders ||= []
end

def user_defined_responders(type)
Expand Down Expand Up @@ -149,8 +147,8 @@ def match_responders(responders, value)

if condition.is_a? Regexp
memo[:scoped] ||= [responder] + $LAST_MATCH_INFO.captures if value =~ condition
else
memo[:scoped] ||= [responder, value] if value == condition
elsif value == condition
memo[:scoped] ||= [responder, value]
end
end
matched[:scoped] || matched[:general]
Expand Down Expand Up @@ -185,12 +183,10 @@ def show
else
render text: echostr
end
elsif Rails::VERSION::MAJOR >= 4
render plain: params[:echostr]
else
if Rails::VERSION::MAJOR >= 4
render plain: params[:echostr]
else
render text: params[:echostr]
end
render text: params[:echostr]
end
end

Expand Down

0 comments on commit 2a329cf

Please sign in to comment.