Skip to content

Instantly share code, notes, and snippets.

@iberianpig
Created December 18, 2016 07:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iberianpig/b0ae7a7b2fd75ad1c11f218d00619d67 to your computer and use it in GitHub Desktop.
Save iberianpig/b0ae7a7b2fd75ad1c11f218d00619d67 to your computer and use it in GitHub Desktop.
Amazon Dash Buttonを押すとIFTTT経由で出社時刻を報告する
require 'dashed'
require 'net/https'
# main class
class Yamadash
USERNAME = 'yamada'
ICONURL = ''
MACADDRESS = '12:34:56:78:90:ab'
DEVICENAME = 'wlp2s0'
ENDPOINT = 'https://maker.ifttt.com/trigger/press_button/with/key/'
PREPTIME = 1.5
def ifttt_key
key = ARGV[0]
if key.nil?
puts "require IFTTT keycode as an argument:\
#{ENDPOINT}/KEYCODE"
end
key
end
def arrive_at
Time.now + PREPTIME * 60 * 60
end
def roughly_arrive_at
time = arrive_at
"#{hour(time.hour)}#{round_min(time.min)}"
end
def hour(hour)
"#{hour}時"
end
def round_min(min)
rounded = min.round(-1)
if (rounded == 60) || rounded.zero?
nil
else
"#{rounded}分"
end
end
def message
"おはようございます。\n#{roughly_arrive_at}ごろ出社します。"
end
def wait_press_button(key)
Dashed::Button.new(MACADDRESS, DEVICENAME).on_press do
uri = URI.parse(ENDPOINT + key)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
req = Net::HTTP::Post.new(uri.path)
req.set_form_data(value1: message, value2: USERNAME, value3: ICONURL)
http.request(req)
end
end
def run
key = ifttt_key
return if key.nil?
wait_press_button(key)
end
new.run if __FILE__ == $PROGRAM_NAME
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment