Skip to content

Instantly share code, notes, and snippets.

@yoshinari-nomura
Last active June 27, 2018 08:18
Show Gist options
  • Select an option

  • Save yoshinari-nomura/e65c18e0573322c47e9a960b92ea6d7d to your computer and use it in GitHub Desktop.

Select an option

Save yoshinari-nomura/e65c18e0573322c47e9a960b92ea6d7d to your computer and use it in GitHub Desktop.
Sample Event class
#!/usr/bin/env ruby
require "json"
require "date"
class Event
attr_reader :start_time, :end_time, :summary, :description
def initialize(start_time, end_time, summary, description)
@start_time, @end_time, @summary, @description =
start_time, end_time, summary, description
end
def to_s
"start:#{start_time}\nend:#{end_time}\nsummary:#{summary}\ndescription:#{description}\n"
end
def to_json(*arg)
to_hash.to_json
end
def to_hash
{
:start => start_time,
:end => end_time,
:summary => summary,
:description => description
}
end
def self.from_hash(hash)
Event.new(DateTime.parse(hash["start"]),
DateTime.parse(hash["end"]),
hash["summary"],
hash["description"])
end
def self.from_json(json)
Event.from_hash(JSON.parse(json))
end
end
class EventCollection
include Enumerable
def initialize
@events = []
end
def <<(event)
@events << event
end
def each(&block)
@events.each do |ev|
yield ev
end
end
def to_json
@events.to_json
end
def self.from_json(json)
events = EventCollection.new
JSON.parse(json).each do |h|
events << Event.from_hash(h)
end
return events
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment