|
| 1 | +module UpcomingEvents |
| 2 | + class Aggregation |
| 3 | + def initialize(args) |
| 4 | + @from = Time.zone.today |
| 5 | + @to = @from + 2.months |
| 6 | + @provider = args[:provider] |
| 7 | + dojos = fetch_dojos(@provider) |
| 8 | + # NOTE: 対象は一旦収集可能な connpass, doorkeeper のみにする |
| 9 | + @externals = dojos[:externals] |
| 10 | + # @internals = dojos[:internals] |
| 11 | + end |
| 12 | + |
| 13 | + def run |
| 14 | + puts "UpcomingEvents aggregate" |
| 15 | + with_notifying do |
| 16 | + delete_upcoming_events |
| 17 | + execute |
| 18 | + end |
| 19 | + end |
| 20 | + |
| 21 | + private |
| 22 | + |
| 23 | + def fetch_dojos(provider) |
| 24 | + if provider.blank? |
| 25 | + # 全プロバイダ対象 |
| 26 | + external_services = DojoEventService::EXTERNAL_SERVICES |
| 27 | + internal_services = DojoEventService::INTERNAL_SERVICES |
| 28 | + else |
| 29 | + external_services = [] |
| 30 | + internal_services = [] |
| 31 | + case provider |
| 32 | + when 'connpass', 'doorkeeper', 'facebook' |
| 33 | + external_services = [provider] |
| 34 | + when 'static_yaml' |
| 35 | + internal_services = [provider] |
| 36 | + end |
| 37 | + end |
| 38 | + |
| 39 | + { |
| 40 | + externals: find_dojos_by(external_services), |
| 41 | + internals: find_dojos_by(internal_services) |
| 42 | + } |
| 43 | + end |
| 44 | + |
| 45 | + def find_dojos_by(services) |
| 46 | + services.each.with_object({}) do |name, hash| |
| 47 | + hash[name] = Dojo.eager_load(:dojo_event_services).where(dojo_event_services: { name: name }).to_a |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + def with_notifying |
| 52 | + yield |
| 53 | + Notifier.notify_success(@provider) |
| 54 | + rescue => e |
| 55 | + Notifier.notify_failure(@provider, e) |
| 56 | + end |
| 57 | + |
| 58 | + def delete_upcoming_events |
| 59 | + UpcomingEvent.until(@from).delete_all |
| 60 | + end |
| 61 | + |
| 62 | + def execute |
| 63 | + target_period = @from..@to |
| 64 | + @externals.each do |kind, list| |
| 65 | + if kind == :facebook |
| 66 | + puts "Aggregate of #{kind} --> skip" |
| 67 | + next |
| 68 | + end |
| 69 | + puts "Aggregate of #{kind}" |
| 70 | + "UpcomingEvents::Tasks::#{kind.to_s.camelize}".constantize.new(list, target_period).run |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + class Notifier |
| 75 | + class << self |
| 76 | + def notify_success(provider) |
| 77 | + notify("近日開催イベント情報#{provider_info(provider)}を収集しました") |
| 78 | + end |
| 79 | + |
| 80 | + def notify_failure(provider, exception) |
| 81 | + notify("近日開催イベント情報の収集#{provider_info(provider)}でエラーが発生しました\n#{exception.message}\n#{exception.backtrace.join("\n")}") |
| 82 | + end |
| 83 | + |
| 84 | + private |
| 85 | + |
| 86 | + def provider_info(provider) |
| 87 | + provider ? "(#{provider})" : nil |
| 88 | + end |
| 89 | + |
| 90 | + def idobata_hook_url |
| 91 | + return @idobata_hook_url if defined?(@idobata_hook_url) |
| 92 | + @idobata_hook_url = ENV['IDOBATA_HOOK_URL'] |
| 93 | + end |
| 94 | + |
| 95 | + def notifierable? |
| 96 | + idobata_hook_url.present? |
| 97 | + end |
| 98 | + |
| 99 | + def notify(msg) |
| 100 | + $stdout.puts msg |
| 101 | + puts `curl --data-urlencode "source=#{msg}" -s #{idobata_hook_url} -o /dev/null -w "idobata: %{http_code}"` if notifierable? |
| 102 | + end |
| 103 | + end |
| 104 | + end |
| 105 | + end |
| 106 | +end |
0 commit comments