From b6f39046a2bc499d1d5884b8837ae43b18f1a7c6 Mon Sep 17 00:00:00 2001 From: stephanie-warmenhoven Date: Thu, 9 Apr 2020 18:16:12 -0700 Subject: [PATCH] [#1713] refactored resource method as per rubocop standards --- app/services/resource_recommendation.rb | 72 ++++++++++++++----------- 1 file changed, 40 insertions(+), 32 deletions(-) diff --git a/app/services/resource_recommendation.rb b/app/services/resource_recommendation.rb index 711498fc38..09528cac14 100644 --- a/app/services/resource_recommendation.rb +++ b/app/services/resource_recommendation.rb @@ -2,8 +2,48 @@ class ResourceRecommendation def initialize(moment) @moment = moment + @moment_keywords = [] + @matched_resources = [] end + def get_keywords(array) + array.each do |item| + @moment_keywords.push(item['name'].split, + html_clean(item['description']).split) + end + end + + def modify_keywords + @moment_keywords = @moment_keywords.flatten + @moment_keywords.each do |keyword| + keyword.gsub!(%r{([_@#!%()\-=;><,{}\~\[\]\./\?\"\*\^\$\+\-]+)}, '') + end + @moment_keywords = @moment_keywords.map(&:downcase) + end + + def match_keywords + all_resources.each do |resource| + resource_tags = resource['tags'].map do |tag| + tag.split('_') + end + unless (resource_tags.flatten & @moment_keywords).empty? + @matched_resources.push(resource) + end + end + end + + def resources + get_keywords(@moment.categories) + get_keywords(@moment.moods) + get_keywords(@moment.strategies) + @moment_keywords.push(moment_name, moment_why, moment_fix) + modify_keywords + match_keywords + @matched_resources + end + + private + def all_resources JSON.parse(File.read(Rails.root.join('doc', 'pages', 'resources.json'))) end @@ -23,36 +63,4 @@ def moment_why def moment_fix html_clean(@moment.fix).split end - - def resources - matched_resources = [] - moment_keywords = [] - @moment.categories.each do |category| - moment_keywords.push(category['name'].split, - html_clean(category['description']).split) - end - @moment.moods.each do |mood| - moment_keywords.push(mood['name'].split, - html_clean(mood['description']).split) - end - @moment.strategies.each do |strategy| - moment_keywords.push(strategy['name'].split, - html_clean(strategy['description']).split) - end - moment_keywords.push(moment_name, moment_why, moment_fix) - moment_keywords = moment_keywords.flatten - moment_keywords.each do |keyword| - keyword.gsub!(%r{([_@#!%()\-=;><,{}\~\[\]\./\?\"\*\^\$\+\-]+)}, '') - end - moment_keywords = moment_keywords.map(&:downcase) - all_resources.each do |resource| - resource_tags = resource['tags'].map do |tag| - tag.split('_') - end - unless (resource_tags.flatten & moment_keywords).empty? - matched_resources.push(resource) - end - end - matched_resources - end end