Skip to content

Instantly share code, notes, and snippets.

@hagiyat
Last active December 29, 2016 06:29
Show Gist options
  • Save hagiyat/59c5e9871bbd05df784fa670763bc7bb to your computer and use it in GitHub Desktop.
Save hagiyat/59c5e9871bbd05df784fa670763bc7bb to your computer and use it in GitHub Desktop.
Record the geolocation record via IFTTT
#!/usr/bin/env ruby
require 'time'
require 'json'
unless ARGV.length == 1
puts "Usage: ruby kintai.rb [input file]"
exit 1
end
lines =
File.new(ARGV.first).
readlines.
map(&:strip).
reject(&:empty?).
map { |v| JSON.parse(v) }.
map { |v|
at = Time.strptime(v['at'], "%B %d, %Y at %I:%M%p")
OpenStruct.new({
'entered?': v['action'] == 'entered',
'exited?': v['action'] == 'exited',
at: at,
date: at.to_date
})
}.
group_by(&:date).
reduce({}) { |acc, (date, list)|
acc.merge({
date.strftime('%Y/%m/%d') => {
entered: list.select(&:entered?).sort_by(&:at).first.at.strftime('%H:%M'),
exited: list.select(&:exited?).sort_by(&:at).last.at.strftime('%H:%M')
}
})
}
puts lines.to_json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment