grapeのoptionsについての備忘録

2021/09/26 22:41

プログラミング, 備忘録


railsのgemに grapegrape-entityというものがある。

これらを使えば、RESTfulなAPIをそれなりに手軽に返せる機構を作ることが出来る。

以下のようなgrapeのフォーマットに従ったAPIレスポンス用のクラス、及び Entity定義用のクラスがあったとする。

# UserAPIクラス

class Api < Grape::API
  format :json
  content_type :json, 'application/json'

  resource :users do
    desc 'user list'
    get do
      # アクティブ状態のuserを一覧取得するイメージ
      users = User.active

      # UserEntity.represent(users) = UserEntity.new(users)
      UserEntity.represent(users, class: request.headers[:class])
    end
  end
end

# UserEntityクラス
class UserEntity < Grape::Entity

  expose :id
  expose :email
  expose :status
  expose :class_name
  
  private

  def class
    # object: Userの1インスタンスが入る
    object.class_users.find { |class| class.id == @options[:class].id }
  end
end

この時、 UserEntityにて @optionsというインスタンス変数が使われているが、この中には

UserEntity.represent(users, class: request.headers[:class])

で指定された class: request.headers[:class]部分がkey-value形式で格納されている。

参考:

Macでsafariを開き、ユーザーエージェントをipadにして動作確認したい時

2023/05/03 14:33

その他, 備忘録


問題

  • macでsafariを開いてuser agentをipad airに変更した際、ipadの挙動の再現が一部chrome等と異なっている

何が困るか

  • ipadでのみ表示したい箇所を実装した時など、macで動作確認してもうまく表示されないので焦る

事象の詳細

  • Navigator.maxTouchPointsによる判定が 0になっている
    • chrome等では 1と表示される
  • 'ontouchend' in documentによる判定が falseになる
    • chrome等では trueと判定される

対処

  • xcodeのsimulator上でipadを選択し、safariを開く
    • ローカルで起動しているので、 localhost:3000だろうが関係なく動作確認が可能