Skip to content

Refactored to comply with Rubocop rules #696

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Refactored to comply with Rubocop rules
Safe auto-correct in this batch.
  • Loading branch information
benlangfeld committed Sep 9, 2024
commit e20a189b73f9f2e8624d4466dfd2ec9414511f20
331 changes: 207 additions & 124 deletions .rubocop_todo.yml

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

**Merged pull requests:**

* Refactored to comply with Rubocop rules [\#695](https://github.com/rpush/rpush/pull/695) ([benlangfeld](https://github.com/benlangfeld))

[Full Changelog](https://github.com/rpush/rpush/compare/v9.0.0...HEAD)

## [v9.0.0](https://github.com/rpush/rpush/tree/v9.0.0) (2024-09-09)
Expand Down
23 changes: 23 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
# frozen_string_literal: true

source 'https://rubygems.org'

gemspec

gem 'appraisal'
gem 'byebug'
gem 'codeclimate-test-reporter', '1.0.7'
gem 'database_cleaner'
gem 'debug'
gem 'modis', '>= 2.0'
gem 'mysql2'
gem 'pg'
gem 'rake'
gem 'rpush-redis', '~> 1.0'
gem 'rspec'
gem 'rubocop', '~> 1.66'
gem 'rubocop-performance'
gem 'rubocop-rails'
gem 'rubocop-rake'
gem 'rubocop-rspec'
gem 'simplecov'
gem 'sqlite3'
gem 'stackprof'
gem 'timecop'
2 changes: 1 addition & 1 deletion bm/bench.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def with_profile(name, &blk)
@profiles << out
StackProf.run(mode: mode, out: out, &blk)
else
blk.call
yield
end
end

Expand Down
12 changes: 6 additions & 6 deletions examples/rpush.god
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ God.watch do |w|
w.keepalive

# determine when process has finished starting
w.transition([:start, :restart], :up) do |on|
w.transition(%i[start restart], :up) do |on|
on.condition(:process_running) do |c|
c.running = true
c.interval = 5.seconds
c.running = true
c.interval = 5.seconds
end

# failsafe
on.condition(:tries) do |c|
c.times = 5
c.transition = :start
c.interval = 5.seconds
c.times = 5
c.transition = :start
c.interval = 5.seconds
end
end
end
20 changes: 8 additions & 12 deletions lib/rpush/cli.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# encoding: UTF-8

require 'thor'
require 'rainbow'

Expand Down Expand Up @@ -38,12 +36,10 @@ def stop
Process.kill('TERM', pid)

loop do
begin
Process.getpgid(pid)
sleep 0.05
rescue Errno::ESRCH
break
end
Process.getpgid(pid)
sleep 0.05
rescue Errno::ESRCH
break
end

puts Rainbow('✔').green
Expand Down Expand Up @@ -126,12 +122,12 @@ def load_rails_environment
end

def load_standalone
if !File.exist?(options[:config])
STDERR.puts(Rainbow('ERROR: ').red + "#{options[:config]} does not exist. Please run 'rpush init' to generate it or specify the --config option.")
exit 1
else
if File.exist?(options[:config])
load options[:config]
Rpush.config.update(options)
else
STDERR.puts(Rainbow('ERROR: ').red + "#{options[:config]} does not exist. Please run 'rpush init' to generate it or specify the --config option.")
exit 1
end
end

Expand Down
1 change: 1 addition & 0 deletions lib/rpush/client/active_model/adm/data_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Adm
class DataValidator < ::ActiveModel::Validator
def validate(record)
return unless record.collapse_key.nil? && record.data.nil?

record.errors.add :data, 'must be set unless collapse_key is specified'
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rpush/client/active_model/adm/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def self.included(base)
end
end

def as_json(options = nil)
def as_json(_options = nil)
json = { 'data' => data }
json['consolidationKey'] = collapse_key if collapse_key
# number of seconds before message is expired
Expand Down
2 changes: 1 addition & 1 deletion lib/rpush/client/active_model/apns/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Apns
module App
def self.included(base)
base.instance_eval do
validates :environment, presence: true, inclusion: { in: %w(development production sandbox) }
validates :environment, presence: true, inclusion: { in: %w[development production sandbox] }
validates :certificate, presence: true
validates_with Rpush::Client::ActiveModel::CertificatePrivateKeyValidator
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ module ActiveModel
module Apns
class DeviceTokenFormatValidator < ::ActiveModel::Validator
def validate(record)
return if record.device_token =~ /\A[a-z0-9]\w+\z/i
return if /\A[a-z0-9]\w+\z/i.match?(record.device_token)

record.errors.add :device_token, "is invalid"
end
end
Expand Down
20 changes: 9 additions & 11 deletions lib/rpush/client/active_model/apns/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ def self.included(base)
validates_with Rpush::Client::ActiveModel::Apns::DeviceTokenFormatValidator
validates_with Rpush::Client::ActiveModel::Apns::NotificationPayloadSizeValidator

base.const_set('APNS_DEFAULT_EXPIRY', APNS_DEFAULT_EXPIRY) unless base.const_defined?('APNS_DEFAULT_EXPIRY')
base.const_set('APNS_PRIORITY_IMMEDIATE', APNS_PRIORITY_IMMEDIATE) unless base.const_defined?('APNS_PRIORITY_IMMEDIATE')
base.const_set('APNS_PRIORITY_CONSERVE_POWER', APNS_PRIORITY_CONSERVE_POWER) unless base.const_defined?('APNS_PRIORITY_CONSERVE_POWER')
base.const_set(:APNS_DEFAULT_EXPIRY, APNS_DEFAULT_EXPIRY) unless base.const_defined?(:APNS_DEFAULT_EXPIRY)
base.const_set(:APNS_PRIORITY_IMMEDIATE, APNS_PRIORITY_IMMEDIATE) unless base.const_defined?(:APNS_PRIORITY_IMMEDIATE)
base.const_set(:APNS_PRIORITY_CONSERVE_POWER, APNS_PRIORITY_CONSERVE_POWER) unless base.const_defined?(:APNS_PRIORITY_CONSERVE_POWER)
end
end

Expand All @@ -43,16 +43,18 @@ def mdm=(magic)
MUTABLE_CONTENT_KEY = '__rpush_mutable_content__'
def mutable_content=(bool)
return unless bool

self.data = (data || {}).merge(MUTABLE_CONTENT_KEY => true)
end

CONTENT_AVAILABLE_KEY = '__rpush_content_available__'
def content_available=(bool)
return unless bool

self.data = (data || {}).merge(CONTENT_AVAILABLE_KEY => true)
end

def as_json(options = nil) # rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity
def as_json(_options = nil) # rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity
json = ActiveSupport::OrderedHash.new

if data && data.key?(MDM_KEY)
Expand All @@ -66,16 +68,12 @@ def as_json(options = nil) # rubocop:disable Metrics/AbcSize, Metrics/PerceivedC
json['aps']['url-args'] = url_args if url_args
json['aps']['thread-id'] = thread_id if thread_id

if data && data[MUTABLE_CONTENT_KEY]
json['aps']['mutable-content'] = 1
end
json['aps']['mutable-content'] = 1 if data && data[MUTABLE_CONTENT_KEY]

if data && data[CONTENT_AVAILABLE_KEY]
json['aps']['content-available'] = 1
end
json['aps']['content-available'] = 1 if data && data[CONTENT_AVAILABLE_KEY]

if data
non_aps_attributes = data.reject { |k, _| k == CONTENT_AVAILABLE_KEY || k == MUTABLE_CONTENT_KEY }
non_aps_attributes = data.reject { |k, _| [CONTENT_AVAILABLE_KEY, MUTABLE_CONTENT_KEY].include?(k) }
non_aps_attributes.each { |k, v| json[k.to_s] = v }
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class NotificationPayloadSizeValidator < ::ActiveModel::Validator
def validate(record)
limit = record.class.max_payload_bytesize
return unless record.payload.bytesize > limit

record.errors.add :base, "APN notification cannot be larger than #{limit} bytes. Try condensing your alert and device attributes."
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rpush/client/active_model/apns2/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Apns2
module App
def self.included(base)
base.instance_eval do
validates :environment, presence: true, inclusion: { in: %w(development production sandbox) }
validates :environment, presence: true, inclusion: { in: %w[development production sandbox] }
validates :certificate, presence: true
validates_with Rpush::Client::ActiveModel::CertificatePrivateKeyValidator
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rpush/client/active_model/apnsp8/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Apnsp8
module App
def self.included(base)
base.instance_eval do
validates :environment, presence: true, inclusion: { in: %w(development production sandbox) }
validates :environment, presence: true, inclusion: { in: %w[development production sandbox] }
validates :apn_key, presence: true
validates :apn_key_id, presence: true
validates :team_id, presence: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ module Client
module ActiveModel
class CertificatePrivateKeyValidator < ::ActiveModel::Validator
def validate(record)
if record.certificate.present?
begin
x509 = OpenSSL::X509::Certificate.new(record.certificate)
pkey = OpenSSL::PKey::RSA.new(record.certificate, record.password)
x509 && pkey
rescue OpenSSL::OpenSSLError
record.errors.add :certificate, 'value must contain a certificate and a private key.'
end
return unless record.certificate.present?

begin
x509 = OpenSSL::X509::Certificate.new(record.certificate)
pkey = OpenSSL::PKey::RSA.new(record.certificate, record.password)
x509 && pkey
rescue OpenSSL::OpenSSLError
record.errors.add :certificate, 'value must contain a certificate and a private key.'
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Fcm
class ExpiryCollapseKeyMutualInclusionValidator < ::ActiveModel::Validator
def validate(record)
return unless record.collapse_key && !record.expiry

record.errors.add :expiry, 'must be set when using a collapse_key'
end
end
Expand Down
22 changes: 11 additions & 11 deletions lib/rpush/client/active_model/fcm/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,20 @@ def payload_data_size
# we do a little conversion here.
def priority=(priority)
case priority
when 'high', FCM_PRIORITY_HIGH
super(FCM_PRIORITY_HIGH)
when 'normal', FCM_PRIORITY_NORMAL
super(FCM_PRIORITY_NORMAL)
else
errors.add(:priority, 'must be one of either "normal" or "high"')
when 'high', FCM_PRIORITY_HIGH
super(FCM_PRIORITY_HIGH)
when 'normal', FCM_PRIORITY_NORMAL
super(FCM_PRIORITY_NORMAL)
else
errors.add(:priority, 'must be one of either "normal" or "high"')
end
end

def dry_run=(value)
fail ArgumentError, 'FCM does not support dry run' if value
end

def as_json(options = nil) # rubocop:disable Metrics/PerceivedComplexity
def as_json(_options = nil)
json = {
'data' => data,
'android' => android_config,
Expand Down Expand Up @@ -85,7 +85,7 @@ def apns_config

def notification=(value)
value = value.with_indifferent_access if value.is_a?(Hash)
super(value)
super
end

def root_notification
Expand All @@ -98,13 +98,13 @@ def android_notification
json = notification&.slice(*ANDROID_NOTIFICATION_KEYS) || {}
json['notification_priority'] = priority_for_notification if priority
json['sound'] = sound if sound
json['default_sound'] = sound == 'default' ? true : false
json['default_sound'] = sound == 'default'
json
end

def priority_str
case
when priority <= 5 then 'normal'
if priority <= 5
'normal'
else
'high'
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class PayloadDataSizeValidator < ::ActiveModel::Validator
def validate(record)
limit = options[:limit] || 1024
return unless record.data && record.payload_data_size > limit

record.errors.add :base, "Notification payload data cannot be larger than #{limit} bytes."
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/rpush/client/active_model/pushy/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def self.included(base)

def as_json(_options = nil)
{
'data' => data,
'time_to_live' => time_to_live,
'data' => data,
'time_to_live' => time_to_live,
'registration_ids' => registration_ids
}
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Pushy
class TimeToLiveValidator < ::ActiveModel::Validator
def validate(record)
return if record.time_to_live.blank? || record.time_to_live <= 1.year.seconds

record.errors.add(:time_to_live, 'The maximum value is 1 year')
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class RegistrationIdsCountValidator < ::ActiveModel::Validator
def validate(record)
limit = options[:limit] || 100
return unless record.registration_ids && record.registration_ids.size > limit

record.errors.add :base, "Number of registration_ids cannot be larger than #{limit}."
end
end
Expand Down
11 changes: 4 additions & 7 deletions lib/rpush/client/active_model/webpush/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ module Client
module ActiveModel
module Webpush
module App

class VapidKeypairValidator < ::ActiveModel::Validator
def validate(record)
return if record.vapid_keypair.blank?

keypair = record.vapid
%i[ subject public_key private_key ].each do |key|
unless keypair.key?(key)
record.errors.add(:vapid_keypair, "must have a #{key} entry")
end
%i[subject public_key private_key].each do |key|
record.errors.add(:vapid_keypair, "must have a #{key} entry") unless keypair.key?(key)
end
rescue
rescue StandardError
record.errors.add(:vapid_keypair, 'must be valid JSON')
end
end
Expand All @@ -33,7 +31,6 @@ def service_name
def vapid
@vapid ||= JSON.parse(vapid_keypair).symbolize_keys
end

end
end
end
Expand Down
Loading
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy