]> nos-oignons.net Git - gestion-adh.git/blob - lib/nos_oignons/subscriptions.rb
ca5cafe5265f745b80d6e1fcf261803f8fa8fbfa
[gestion-adh.git] / lib / nos_oignons / subscriptions.rb
1 require 'safe_yaml'
2 SafeYAML::OPTIONS[:default_mode] = :safe
3
4 SUBSCRIPTIONS_ROOT = 'Membres'
5
6 def is_valid_subscription?(content)
7   return false if content.length == 0
8   return false unless content.start_with?("---\n")
9   begin
10     data = YAML.load(content)
11   rescue ArgumentError
12     # Parse error
13     return false
14   end
15   ['name', 'email'].each do |key|
16     return false unless data.include?(key)
17   end
18   if data.include?('membership_fee_paid_on')
19     return false unless data['membership_fee_paid_on'].is_a?(Date)
20   end
21   true
22 end
23
24 def is_valid_subscription_file?(ref, file)
25   IO.popen(['git', 'show', "#{ref}:#{file}"]) do |f|
26     is_valid_subscription?(f.read)
27   end
28 end
29
30 def read_subscriptions
31   subscriptions = {}
32   Dir.glob("#{SUBSCRIPTIONS_ROOT}/*.mdwn") do |file|
33     subscription_id = File.basename(file).gsub(/\.mdwn$/, '')
34     subscriptions[subscription_id] = YAML.load_file(file)
35   end
36   subscriptions
37 end