Skip to content

Commit

Permalink
yaml_dataをメモ化
Browse files Browse the repository at this point in the history
  • Loading branch information
ysk91 committed Apr 22, 2023
1 parent 9fad181 commit 9286a10
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ class Api::V1::CostsController < ApplicationController
require 'yaml'

def index
yaml_data = YAML.load_file(yaml_path)
render json: {
status: 'SUCCESS',
message: '電力会社とコスト一覧の取得に成功しました',
data: yaml_data
}, status: 200
end

def calculate_rate
def calculate_rate # 10行で収める その他はモデルに移動
contract_ampere = params[:contract_ampere].to_i
usage = params[:usage].to_i unless params[:usage].nil?
rates = YAML.load_file(yaml_path)
rates = yaml_data

if contract_ampere.zero? || usage.nil?
render json: { error: 'Invalid input: contract_ampere and usage are required' }, status: 400
Expand Down Expand Up @@ -51,8 +50,8 @@ def calculate_rate

private

def yaml_path
@yaml_path ||= Rails.root.join('config', 'rates.yml')
def yaml_data
yaml_data ||= YAML.load_file(Rails.root.join('config', 'rates.yml'))
end

end
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def output
end
data = JSON.parse(response.body)
@results = data
if @results.empty?
if @results.empty? # TODO モデルでバリデーションする ヒント: オブジェクト指向
render :input
end
end
Expand Down

0 comments on commit 9286a10

Please sign in to comment.