2024-03-02 14:50:16 +00:00
|
|
|
|
require "carddav"
|
|
|
|
|
require "thor"
|
|
|
|
|
require "yaml"
|
|
|
|
|
|
|
|
|
|
class Birthdaze < Thor
|
|
|
|
|
desc "generate", "Generate calendars"
|
|
|
|
|
def generate
|
|
|
|
|
puts "Generate calendars"
|
2024-03-03 10:33:24 +00:00
|
|
|
|
birthdays
|
2024-03-02 14:50:16 +00:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
2024-03-03 10:33:24 +00:00
|
|
|
|
def config(config_file = "#{ENV["HOME"]}/.config/birthdaze.yaml")
|
2024-03-02 14:50:16 +00:00
|
|
|
|
unless File.file?(config_file)
|
|
|
|
|
puts "Please add a configuration file"
|
|
|
|
|
return
|
|
|
|
|
end
|
2024-03-03 10:33:24 +00:00
|
|
|
|
|
2024-03-02 14:50:16 +00:00
|
|
|
|
@config ||= YAML.load_file(config_file)
|
|
|
|
|
end
|
|
|
|
|
|
2024-03-03 10:33:24 +00:00
|
|
|
|
def client(url: config["url"], username: config["username"], password: config["password"])
|
|
|
|
|
@client ||= Carddav::Client.new(url, username, password)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def birthdays
|
|
|
|
|
birthdays = []
|
|
|
|
|
client.cards.each do |card|
|
|
|
|
|
card = card.parsed.to_s
|
|
|
|
|
birthday = birthday_regex.match(card)[1] if birthday_regex.match?(card)
|
|
|
|
|
name = name_regex.match(card)[1] if name_regex.match?(card)
|
|
|
|
|
birthdays << [ name, birthday ] if name && birthday
|
|
|
|
|
end
|
|
|
|
|
birthdays
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def birthday_regex
|
|
|
|
|
# We need the dash for dates which don’t specify a year
|
|
|
|
|
/.*BDAY.*:([\d-]*).*/
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def name_regex
|
|
|
|
|
/FN.*:(.*)/
|
2024-03-02 14:50:16 +00:00
|
|
|
|
end
|
|
|
|
|
end
|