🐦

[Pixela][FaaStRuby] iPhoneの「ショートカット」で XX した数を記録する

2020/09/27に公開

published_at: 2019-04-05 14:06


諸事情で時間あるので自身のアクティビティ把握も兼ねて
軽く一連の流れを作ってみました。
参考にしていただけるとうれしいです。

概要

このケースでの XX は、トイレに行った回数 になります [1]

実装

  1. Pixela で graph と webhook を作成(webhook の ハッシュは控えておく)
  2. FaaStRuby で handler メソッド に webhook の呼び出しを実装
  3. iPhone ショートカットで ↑でデプロイしたAPIを呼び出す
    • URL (呼び出し)
    • URL の内容を取得 <- これを入れないと期待通りにカウントアップされない

使い方

  1. トイレに行く
  2. トイレでやるべきことをやる
  3. やるべきことが終わったら iPhone のショートカットを実行
  4. Pixela のグラフを必要に応じて確認(カウントアップされたか)

詳細

Pixela で graph と webhook を作成

refs
https://pixe.la/ -> Getting Started あたりから
https://docs.pixe.la/#/post-graph
https://docs.pixe.la/#/invoke-webhook

  • (ユーザーを作成 : 自分は作成済みなので略)

  • グラフを作成

    ❯ curl -X POST https://pixe.la/v1/users/<user_id>/graphs -H 'X-USER-TOKEN:<user_token>' -d '{"id":"toilet","name":"toilet-log","unit":"syo-ben","type":"int","color":"ichou","timezone":"Asia/Tokyo"}'
    {"message":"Success.","isSuccess":true}
    
  • web hook を作成

    ❯ curl -X POST https://pixe.la/v1/users/<user_id>/webhooks -H 'X-USER-TOKEN:<user_token>' -d '{"graphID":"toilet","type":"increment"}'
    {"message":"Success.","isSuccess":true,"webhookHash":"<webhook_hash:これは控えておいてください>"}
    

FaaStRuby で handler メソッド に webhook の呼び出しを実装

FaaStRuby のインストール

gem install faastruby
Fetching: faastruby-rpc-0.2.6.gem (100%)
Successfully installed faastruby-rpc-0.2.6
Fetching: xdg-2.2.3.gem (100%)
Successfully installed xdg-2.2.3
Fetching: tmuxinator-0.15.0.gem (100%)

    __________________________________________________________
    ..........................................................

    Thank you for installing tmuxinator.

    Make sure that you've set these variables in your ENV:

      $EDITOR, $SHELL

    You can run `tmuxinator doctor` to make sure everything is set.
    Happy tmuxing with tmuxinator!

    ..........................................................
    __________________________________________________________

Successfully installed tmuxinator-0.15.0
Fetching: faastruby-0.5.22.gem (100%)
Successfully installed faastruby-0.5.22
Parsing documentation for faastruby-rpc-0.2.6
Installing ri documentation for faastruby-rpc-0.2.6
Parsing documentation for xdg-2.2.3
Installing ri documentation for xdg-2.2.3
Parsing documentation for tmuxinator-0.15.0
Installing ri documentation for tmuxinator-0.15.0
Parsing documentation for faastruby-0.5.22
Installing ri documentation for faastruby-0.5.22
Done installing documentation for faastruby-rpc, xdg, tmuxinator, faastruby after 3 seconds
4 gems installed
faastruby create-workspace toilet-logger
You are not logged in. To login, run: faastruby login

If you don't have an account, run 'faastruby signup' to create one.

おこられたので sign up

❯ faastruby signup

Welcome to FaaStRuby! Please enter your email address:
Email: <your_id@your_mail_domain>

Now type in a password. It must contain 8 to 50 characters and have at least one uppercase letter, one lowercase letter, one number and one special character @ $ ! % * ? &
Password:
✔ Creating your account...

You should have received an email with a confirmation token.
If you didn't receive an email:
- Make sure you sign up with the correct email address
- The system won't send an email if you account status is already 'confirmed'
Confirmation Token: <ここに出るトークンをコンソールに貼り付ける>
✔ Confirming your account...
Login successful!
ATTENTION: I've detected you have workspace credentials saved in '/path/to/Users/<me>/.faastruby....'.
You need to migrate these credentials into your account.
Do you want to do it right now? [Y/n] Y
@@@ WARNING @@@ WARNING @@@ WARNING @@@ WARNING @@@
This is going to migrate all your legacy credentials into your new account. This process is REQUIRED, but irreversible.
You are currently logged in as '<your_id@your_mail_domain>'.
Continue? [y/N] y
✔ Migrating workspace '<以前作った別のworkspace>'...
作業用ディレクトリ `toilet-logger` を作成して移動
mkdir toilet-logger
cd toilet-logger
faastruby new toilet-logger
+ d toilet-logger
+ d toilet-logger/spec
+ f toilet-logger/spec/spec_helper.rb
+ f toilet-logger/spec/handler_spec.rb
+ f toilet-logger/README.md
+ f toilet-logger/Gemfile
+ f toilet-logger/handler.rb
+ f toilet-logger/faastruby.yml
✔ Installing gems...
faastruby deploy-to toilet-logger # いちどデフォルトのままデプロイ
✔ Setting up workspace 'toilet-logger'...
◓ [toilet-logger] Deploying Ruby function 'toilet-logger' to workspace 'toilet-logger'.....

Finished in 0.0737 seconds (files took 1.29 seconds to load)
2 examples, 0 failures

✔ [toilet-logger] Deploying Ruby function 'toilet-logger' to workspace 'toilet-logger'...
* [toilet-logger] Deploy OK
* [toilet-logger] Workspace: toilet-logger
* [toilet-logger] Endpoint: <endpoint_url>

install Pixela (Ruby) Client

cat Gemfile
  source 'https://rubygems.org'

+ gem 'pixela'

  group :test do
    gem 'rspec'
    gem 'faastruby-rpc'
    gem 'faastruby'
  end
bundle
Fetching gem metadata from https://rubygems.org/.............
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies...
Using backports 3.12.0
Using bundler 2.0.1
・・・
Fetching pixela 1.1.0
Installing pixela 1.1.0
Using rspec-support 3.8.0
Using rspec-core 3.8.0
Using rspec-expectations 3.8.2
Using rspec-mocks 3.8.0
Using rspec 3.8.0
Bundle complete! 4 Gemfile dependencies, 57 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
Edit source codes
  • handler.rb
  def handler event
  # ・・・

  + client = Pixela::Client.new(username: "<user_id>", token: "<user_token>")
  + response = client.webhook("<webhook_hash>").invoke

    render text: "Hello, World!\n"
  end
  • handler_spec.rb
+require "pixela"

 require 'spec_helper'
 require 'handler'
 #・・・
  • spec_helper.rb
+require 'pixela'

 require 'faastruby-rpc/test_helper'
 require 'faastruby/spec_helper'
 include FaaStRuby::SpecHelper

Deploy !

faastruby deploy-to toilet-logger
    ✔ Setting up workspace 'toilet-logger'...
    ◐ [toilet-logger] Deploying Ruby function 'toilet-logger' to workspace 'toilet-logger'...No examples found.
    
    Finished in 0.00039 seconds (files took 0.99003 seconds to load)
    0 examples, 0 failures
    
    ✔ [toilet-logger] Deploying Ruby function 'toilet-logger' to workspace 'toilet-logger'...
    * [toilet-logger] Deploy OK
    * [toilet-logger] Workspace: toilet-logger
    * [toilet-logger] Endpoint: <endpoint_url>

iPhone ショートカットで ↑でデプロイしたAPIを呼び出す

[URL(呼び出し)]と [URL の内容を取得] を設定

shortcut-setting.PNG (187.3 kB)

やるべきことが終わったら iPhone のショートカットを実行

操作は2タップのみ
  • ショートカット を呼び出す (まだなければ App Store でインストール)

shortcut.png (118.2 kB)

  • Toilet Log を押す (左側をタップするのがよいみたい)

shortcut_buttons.PNG (1.5 MB)

Pixela のグラフを必要に応じて確認

確認方法の例を2つ
  • Pixela のグラフだと数値が確認できます
    toilet-log-1.png (42.7 kB)

  • esa.io などに埋め込んでる場合、色が濃くなってると思います
    toilet-log-2.png (20.4 kB)

参照情報

脚注
  1. スマホをトイレにも持ち歩くという前提。まあ、忘れたら後で実行すればよいのですが。 ↩︎

Discussion