From 1fc429485178dd7984b44f659dabc7d77ff1b4c7 Mon Sep 17 00:00:00 2001 From: Eiji Yamamoto Date: Fri, 15 Mar 2019 12:54:11 +0900 Subject: [PATCH 1/2] Update post_event command to deal with Date and DateTime --- lib/goohub/command/post_event.rb | 34 +++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/lib/goohub/command/post_event.rb b/lib/goohub/command/post_event.rb index 6a59cd3..24ae522 100644 --- a/lib/goohub/command/post_event.rb +++ b/lib/goohub/command/post_event.rb @@ -5,15 +5,31 @@ class GoohubCLI < Clian::Cli desc "post_event CALENDAR_ID TITLE START END", "Create an event to CALENDAR_ID" def post_event(calendar_id, title, start_time, end_time) - event = Google::Apis::CalendarV3::Event.new({ - summary: title, - start: { - date_time: start_time, - }, - end: { - date_time: end_time, - } - }) + if start_time.class == Icalendar::Values::Date then + event = Google::Apis::CalendarV3::Event.new({ + summary: title, + start: { + date: start_time, + }, + end: { + date: end_time, + } + }) + + elsif start_time.class == Icalendar::Values::DateTime then + event = Google::Apis::CalendarV3::Event.new({ + summary: title, + start: { + date_time: start_time, + }, + end: { + date_time: end_time, + } + }) + else + puts "Event is not created, because event format is wrong." + return + end result = client.insert_event(calendar_id, event) puts "Event created: #{result.html_link}" end From 8d79db40b815046854d149cc7c8aa044c744bbee Mon Sep 17 00:00:00 2001 From: Eiji Yamamoto Date: Fri, 15 Mar 2019 12:55:24 +0900 Subject: [PATCH 2/2] Add post_ics command --- Gemfile | 1 + lib/goohub/command.rb | 1 + lib/goohub/command/post_ics.rb | 28 ++++++++++++++++++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 lib/goohub/command/post_ics.rb diff --git a/Gemfile b/Gemfile index 265bc00..1075bec 100644 --- a/Gemfile +++ b/Gemfile @@ -2,3 +2,4 @@ source 'https://rubygems.org' # Specify your gem's dependencies in goohub.gemspec gemspec +gem 'icalendar' diff --git a/lib/goohub/command.rb b/lib/goohub/command.rb index 42767cd..e51b290 100644 --- a/lib/goohub/command.rb +++ b/lib/goohub/command.rb @@ -11,3 +11,4 @@ require "#{dir}/get_event.rb" require "#{dir}/events.rb" require "#{dir}/post_event.rb" +require "#{dir}/post_ics.rb" diff --git a/lib/goohub/command/post_ics.rb b/lib/goohub/command/post_ics.rb new file mode 100644 index 0000000..01e3a83 --- /dev/null +++ b/lib/goohub/command/post_ics.rb @@ -0,0 +1,28 @@ +require 'icalendar' + +class GoohubCLI < Clian::Cli + ################################################################ + # Command: post_ics + ################################################################ + desc "post_ics CALENDAR_ID ICS_FILE", "Create ics file events to CALENDAR_ID" + + def post_ics(calendar_id, ics_file) + ics_file = File.open(ics_file) + events = Icalendar::Event.parse(ics_file) + count = 0 + + puts "Start #{events.size} events post." + events.each { |e| + begin + count = count + 1 + post_event(calendar_id, e.summary, e.dtstart, e.dtend) + rescue => error + puts "#########################" + puts "#{error}" + puts "#{e.summary}, #{e.dtstart}, #{e.dtend}" + puts "#########################" + end + } + puts "Done #{count}/#{events.size} events post." + end +end