Skip to content

Commit

Permalink
[ifmeorg#1713] refactored resource method as per rubocop standards
Browse files Browse the repository at this point in the history
  • Loading branch information
S-Warmenhoven committed Apr 10, 2020
1 parent ad42eb9 commit b6f3904
Showing 1 changed file with 40 additions and 32 deletions.
72 changes: 40 additions & 32 deletions app/services/resource_recommendation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit b6f3904

Please sign in to comment.