Skip to content

Commit

Permalink
Merge pull request #19 from pyama86/fix-ruby-3
Browse files Browse the repository at this point in the history
support ruby3
  • Loading branch information
Kazuhiko Yamashita authored Oct 3, 2022
2 parents b5b9708 + ff71b8b commit 36c68a3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 59 deletions.
79 changes: 38 additions & 41 deletions lib/popper/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
module Popper
class Config
attr_reader :default, :accounts, :interval

def initialize(config_path)
raise "configure not fond #{config_path}" unless File.exist?(config_path)

config = read_file(config_path)

@interval = config.key?("interval") ? config["interval"].to_i : 60
@default = config["default"] if config["default"]
@accounts = config.select {|k,v| v.is_a?(Hash) && v.key?("login") }.map do |account|
@interval = config.key?('interval') ? config['interval'].to_i : 60
@default = config['default'] if config['default']
@accounts = config.select { |_k, v| v.is_a?(Hash) && v.key?('login') }.map do |account|
_account = AccountAttributes.new(account[1])
_account.name = account[0]
_account
Expand All @@ -19,90 +21,85 @@ def initialize(config_path)

def read_file(file)
config = TOML.load_file(file)
if config.key?("include")
content = config["include"].map {|p| Dir.glob(p).map {|f|File.read(f)}}.join("\n")
if config.key?('include')
content = config['include'].map { |p| Dir.glob(p).map { |f| File.read(f) } }.join("\n")
config.deep_merge!(TOML::Parser.new(content).parsed)
end
config
end

%w(
%w[
condition
action
).each do |name|
define_method("default_#{name}") {
begin
default[name]
rescue
{}
end
}
].each do |name|
define_method("default_#{name}") do
default[name]
rescue StandardError
{}
end
end
end

class AccountAttributes < OpenStruct
def initialize(hash=nil)
def initialize(hash = nil)
super
@table = {}
@hash = hash

hash.each do |k,v|
@table[k.to_sym] = (v.is_a?(Hash) ? self.class.new(v) : v)
new_ostruct_member(k)
end if hash
if hash
hash.each do |k, v|
@table[k.to_sym] = (v.is_a?(Hash) ? self.class.new(v) : v)
end
end
end

def to_h
@hash
end

[
%w(select all?),
%w(each each),
%w[select all?],
%w[each each]
].each do |arr|
define_method("rule_with_conditions_#{arr[0]}") do |&blk|
@hash["rules"].keys.send(arr[0]) do |rule|
self.condition_by_rule(rule).to_h.send(arr[1]) do |mail_header,conditions|
@hash['rules'].keys.send(arr[0]) do |rule|
condition_by_rule(rule).to_h.send(arr[1]) do |mail_header, conditions|
blk.call(rule, mail_header, conditions)
end
end
end
end

%w(
%w[
condition
action
).each do |name|
define_method("account_default_#{name}") {
begin
@hash["default"][name]
rescue
{}
end
}
].each do |name|
define_method("account_default_#{name}") do
@hash['default'][name]
rescue StandardError
{}
end

# merge default and account default
define_method("#{name}_by_rule") do |rule|
hash = Popper.configure.send("default_#{name}")
hash = hash.deep_merge(self.send("account_default_#{name}")) if self.send("account_default_#{name}")
hash = hash.deep_merge(send("account_default_#{name}")) if send("account_default_#{name}")
hash = hash.deep_merge(rule_by_name(rule)[name]) if rule_by_name(rule).key?(name)

# replace body to utf_body
AccountAttributes.new(Hash[hash.map {|k,v| [k.to_s.gsub(/^body$/, "utf_body").to_sym, v]}])
AccountAttributes.new(Hash[hash.map { |k, v| [k.to_s.gsub(/^body$/, 'utf_body').to_sym, v] }])
end
end

def rule_by_name(name)
begin
@hash["rules"][name]
rescue
{}
end
@hash['rules'][name]
rescue StandardError
{}
end

end

def self.load_config(options)
config_path = options[:config] || "/etc/popper.conf"
config_path = options[:config] || '/etc/popper.conf'
@_config = Config.new(config_path)
end

Expand Down
37 changes: 19 additions & 18 deletions popper.gemspec
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
lib = File.expand_path('lib', __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'popper/version'

Gem::Specification.new do |spec|
spec.name = "popper"
spec.name = 'popper'
spec.version = Popper::VERSION
spec.authors = ["pyama86"]
spec.email = ["[email protected]"]
spec.authors = ['pyama86']
spec.email = ['[email protected]']

spec.summary = %q{email notification tool}
spec.description = %q{email notification tool}
spec.homepage = "http://ten-snapon.com"
spec.license = "MIT"
spec.summary = 'email notification tool'
spec.description = 'email notification tool'
spec.homepage = 'http://ten-snapon.com'
spec.license = 'MIT'

spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
spec.bindir = "exe"
spec.bindir = 'exe'
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.require_paths = ['lib']

spec.add_dependency 'thor'
spec.add_dependency 'slack-notifier'
spec.add_dependency 'toml'
spec.add_dependency 'bundler'
spec.add_dependency 'faraday'
spec.add_dependency 'mail'
spec.add_dependency 'net-pop'
spec.add_dependency 'net-smtp'
spec.add_dependency 'octokit'
spec.add_dependency 'faraday'
spec.add_dependency "bundler"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec"
spec.add_dependency 'slack-notifier'
spec.add_dependency 'thor'
spec.add_dependency 'toml'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rspec'
end

0 comments on commit 36c68a3

Please sign in to comment.