Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cook command #133

Merged
merged 1 commit into from
Aug 8, 2024
Merged

Conversation

otsuki-at
Copy link
Contributor

@otsuki-at otsuki-at commented May 7, 2024

概要

cook コマンドの追加

コマンドの概要

機能

指定された食材や料理に適したレシピの情報を表示する.

使用方法

swimmy cook <キーワード> と入力するとキーワードに適したレシピを表示する.
swimmy cook と入力するとランダムにレシピを表示する.

特徴

楽天レシピカテゴリ一覧API楽天レシピカテゴリ別ランキングAPIを使用している.

@otsuki-at otsuki-at force-pushed the add-cook-command branch 2 times, most recently from c541840 to a7caa30 Compare May 13, 2024 05:28
@otsuki-at
Copy link
Contributor Author

変更点

  • API キーはコマンドで env から取得するように変更
  • cook.rbの6行目のfoodに代入する方法をif文を用いた方法から||に変更
  • API仕様のページに関するコメントを追加
  • serviceのファイル名およびクラス名をRecipeInformationに変更
  • CookCategoryクラスをRecipeInformationと同じファイル内に記述
  • エラーが起こった際にエラー名を表示するように変更
  • resourceにファイルを追加し,get_recipeinfoの返り値をValueObjectに変更

コードレビューで頂いたアドバイスを基に,コードを修正しました

Copy link
Member

@yoshinari-nomura yoshinari-nomura left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

レビューコメントを付けました.

class Cook < Swimmy::Command::Base

command "cook" do|client,data,match|
food = match[:expression]||""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

food = match[:expression] || "" のようにスペースを入れてください.

rakuten_app_id = ENV['RAKUTEN_COOK_ID']
recipe_info = Service::RecipeInfomation.new.get_recipeinfo(rakuten_app_id, food)

if recipe_info == nil then message = "レシピが見つかりませんでした.別のキーワードで再検索してください."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

message = if recipe_info then recipe_info.make_mesage else "レシピが…" end と書くか,
message = recipe_info&.make_message || "レシピ…" と書けます.

@name, @description, @url = name, description, url
end

def make_message()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ruby では,引数がない場合の () は不要です.
引数のコンマに空白が必要です

#{@url}
EOS

return message
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ruby では,途中から抜ける必要のない return は,そのまま message と書くだけでよいです


class HttpException < StandardError; end

def get_recipeinfo(api_id, food)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

api_id は,new の引数 (つまり initialize 関数の引数) で与えて,オブジェクト中にインスタンス変数として保持します.

# API仕様 : https://webservice.rakuten.co.jp/documentation/recipe-category-list
result = JSON.parse(URI.open(category_url, &:read))
rescue
puts "HttpException\n"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これでは,情報量がありません.もともと rescue した例外の詳細を puts して log に吐くべきです.

# API仕様 : https://webservice.rakuten.co.jp/documentation/recipe-category-ranking
recipe_ranking = JSON.parse(URI.open(ranking_url, &:read))
rescue
puts "HttpException\n"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これでは,情報量がありません.もともと rescue した例外の詳細を puts して log に吐くべきです.

module Command
class Cook < Swimmy::Command::Base

command "cook" do|client,data,match|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

以降同様ですが,コンマや演算子の前後にスペースを入れてください.


if recipe_info == nil then message = "レシピが見つかりませんでした.別のキーワードで再検索してください."
else
message = recipe_info.make_message()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

() は不要です

Comment on lines 71 to 132
result["result"]["large"].each do |list|
category_id_list[list["categoryName"]] = list["categoryId"].to_s
end
result["result"]["medium"].each do |list|
category_id_list[list["categoryName"]] = list["parentCategoryId"].to_s + "-" + list["categoryId"].to_s
parent_id_list[list["categoryId"]] = list["parentCategoryId"]
end
result["result"]["small"].each do |list|
parent_id = parent_id_list[list["parentCategoryId"].to_i]
category_id_list[list["categoryName"]] = parent_id + "-" + list["parentCategoryId"].to_s + "-" + list["categoryId"].to_s
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これらが何をやっているのかコメントを付けてください.

@otsuki-at
Copy link
Contributor Author

頂いたレビューコメントを基に,コードを修正しました.
以下,変更点です.

共通の修正点

  • 演算子の前後やコンマの後ろに空白を挿入
  • 引数のないメソッドから()を削除
  • rescue 内で例外の詳細をputsするように変更

cook.rb の修正点

  • cook.rb の14行の message への代入の方法を変更

cook_resource.rb の修正点

  • make_message メソッドから return 文を削除

recipe_information の修正点

  • CookInformation および CookCategory クラスにおいて api_id はインスタンス変数として保持するように変更
  • get_reciperanking メソッドの引数に food を追加
  • recipe_choices という変数名から recipe_candidates に変更
  • category_id_list と get_categorylist という変数名を category_id_hash と get_category_id_hash に変更
  • recipe_title, recipe_description, recipe_url という変数名をそれぞれ title, description, url に変更
  • top_ranking という変数を作成し title, description, urlへの代入に top_ranking を用いる方法に変更
  • random_recipe という変数名を random_recipe_id に変更
  • 配列が空か調べる際に empty? メソッドを使用するように変更
  • 引数のないメソッドから()を削除

Copy link
Member

@yoshinari-nomura yoshinari-nomura left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

レビューしました.

end

private
def get_reciperanking(food, category_id_hash)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_receiperanking → get_receipe_ranking がいいと思います.

p e
raise HttpException.new
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

以下が何をやっているのかの説明が必要です.

client.say(channel: data.channel, text: "レシピ情報を取得しています……")

begin
rakuten_app_id = ENV['RAKUTEN_COOK_ID']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

環境変数 RAKUTEN_COOK_ID が設定されていなかった場合の分岐を考えてください.
また,food の設定と RAKUTEN_COOK_ID の設定が離れているので,順序やタイミングを調整したほうがいいです.begin ... rescue に入れるべきかどうかの検討も含めて.


private
def get_reciperanking(food, category_id_hash)
recipe_candidates = category_id_hash.find_all{|key,value| key.include?(food)}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

key,value → key, value

@otsuki-at
Copy link
Contributor Author

頂いたレビューコメントを基に,コードを修正しました.
以下,変更点です.

cook.rb の修正点

  • envファイルにAPIのIDが登録されていなかった場合の分岐を追加
  • food と rakuten_app_id の設定を近づけるために food の設定をbegin … rescue 間に変更

recipe_information の修正点

  • get_receiperanking のメソッド名を get_receipe_ranking に変更
  • 77行目以降にコメントを追加
  • コンマの後に空白を追加

@otsuki-at
Copy link
Contributor Author

検討打合せでいただいたレビューを基にPRを更新しました.
以下,変更点です.

  • make_message メソッドの名前を to_s に変更
  • recipe_information.rb の61-92行にコメントを追加
  • recipe_information.rb に get_my_id メソッドを作成し,カテゴリ名と ID のハッシュの作成方法を変更
  • cook.rb で env ファイルに API の ID が設定されていなかった場合の分岐方法を if 文を用いた方法に変更

@otsuki-at
Copy link
Contributor Author

談話会でいただいたレビューを基にPRを更新しました.
以下変更点です.

  • recipe_information.rb のコメント内の例をツリーを辿ることができるような例に変更
  • recipe_information.rb のコメントにランキング取得に必要な ID の例を追加
  • 親のカテゴリ ID を取得するための get_parent_id メソッドを追加

@ShukiSasakura ShukiSasakura merged commit b623d11 into nomlab:master Aug 8, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants