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

Resource recommendation #1

Open
wants to merge 70 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
7ffef32
Render resources in view to test show action
AlineRibeiro Apr 6, 2020
02540f2
Link each resource to their webpage and render a list
AlineRibeiro Apr 6, 2020
ade6417
Parse resources from json file
AlineRibeiro Apr 6, 2020
4dd3c88
Create link to resources index
AlineRibeiro Apr 6, 2020
0b51d51
Limit resourse each iteration to display maximmun of 3 items
AlineRibeiro Apr 6, 2020
c837408
Create ResourceRecommendation class
AlineRibeiro Apr 7, 2020
082e87a
Create method to parse resources from json file
AlineRibeiro Apr 7, 2020
2fb6310
Rename method parse_json to resources
AlineRibeiro Apr 7, 2020
295e44f
Match a moment name with resources according to tags
AlineRibeiro Apr 7, 2020
a65d353
Match tag with why and fix
AlineRibeiro Apr 7, 2020
19d2a2f
Code cleanup
AlineRibeiro Apr 7, 2020
831d748
Downcase every word in the moment_keyword array
AlineRibeiro Apr 7, 2020
811591d
Code clean up
AlineRibeiro Apr 7, 2020
35eb7cb
Extract category names and compare to tags
AlineRibeiro Apr 8, 2020
52004c6
Extract strategy names and compare to tags
AlineRibeiro Apr 8, 2020
0195ada
Fix iteration that loops through moods
AlineRibeiro Apr 8, 2020
7553127
Fix category name loops so it works for multi word names
AlineRibeiro Apr 8, 2020
2ea93b8
Split words in category and strategy descriptions before pushing to t…
AlineRibeiro Apr 8, 2020
959172c
Refactor strategy, mood and category description, so it removec html …
AlineRibeiro Apr 8, 2020
901ee37
Remove html tags from moment why and fix
AlineRibeiro Apr 8, 2020
f578aa8
Remove all special charachters from moment keywords
AlineRibeiro Apr 8, 2020
b4914b0
Split multi-word tags with '_' into two separated words
AlineRibeiro Apr 8, 2020
dc7b0d7
Refactor ActionController::Base.helpers.strip_tags usages into method…
AlineRibeiro Apr 8, 2020
5db0a22
Refacotor moment_name into a method
AlineRibeiro Apr 8, 2020
389ee81
Refacotor moment_why into a method
AlineRibeiro Apr 9, 2020
4c730d1
Refacotor moment_fix into a method
AlineRibeiro Apr 9, 2020
153a098
Code clean up
AlineRibeiro Apr 9, 2020
64388b7
Refactor all resources into a method
AlineRibeiro Apr 9, 2020
fee8f81
Remove unused variables from iteration loops
AlineRibeiro Apr 9, 2020
576e08a
Move description and name loops into the same iteration
AlineRibeiro Apr 9, 2020
129dd71
Refactor category keywords loop into a method
AlineRibeiro Apr 9, 2020
785d0ce
Refactor mood keywords loop into a method
AlineRibeiro Apr 9, 2020
aff8e5e
Refactor strategy keywords loop into a method
AlineRibeiro Apr 9, 2020
f7f39a1
Make extracted methods private
AlineRibeiro Apr 9, 2020
0de3cfc
Extract downcase_keyword method
AlineRibeiro Apr 9, 2020
e656b88
Rename category, mood and strategy related methods
AlineRibeiro Apr 9, 2020
86d57d4
Refactor and rename moment_name, moment_fix, moment_why
AlineRibeiro Apr 9, 2020
70519ad
Create method extract_moment_keywords
AlineRibeiro Apr 9, 2020
703e9e6
Create remove_special_chars method to clean strings with gsub
AlineRibeiro Apr 9, 2020
a6a6dad
Substitute map for flat_map
AlineRibeiro Apr 9, 2020
5183f90
Match any character that is not a letter
AlineRibeiro Apr 13, 2020
e5c4843
Select matched resources
AlineRibeiro Apr 13, 2020
dcb965e
Remove yarn.lock file from PR
AlineRibeiro Apr 13, 2020
a424065
Create MomentKeyword class and move keyword extraction methods inside it
AlineRibeiro Apr 13, 2020
9f1f143
Rename class and file
AlineRibeiro Apr 13, 2020
be5f3fe
Get moment keywords from a MomentKeywords instance
AlineRibeiro Apr 13, 2020
1c7f3ac
Refactor array extraction methods
AlineRibeiro Apr 13, 2020
fdb9de4
Match international characters
AlineRibeiro Apr 14, 2020
a98570e
Move remove_special_chars method to the moment_keywords class
AlineRibeiro Apr 14, 2020
3f5862d
Remove reassignment from remove_special_chars method
AlineRibeiro Apr 14, 2020
0a16b3b
Move downcase_keywords method to moment_keywords class
AlineRibeiro Apr 14, 2020
0b407e7
Code clean up
AlineRibeiro Apr 14, 2020
7e95790
Code clean up
AlineRibeiro Apr 14, 2020
5584b17
Build a moment factory with customized content
AlineRibeiro Apr 14, 2020
ada08a6
Test if extract_moment_keywords downcases words properly
AlineRibeiro Apr 14, 2020
d917431
Refactor test to check if array contains a term
AlineRibeiro Apr 14, 2020
fde3041
Test is special chars are eliminated and hyphenated words are separated
AlineRibeiro Apr 14, 2020
2d93d59
Turn keywords into a let symbol to avoi repetition
AlineRibeiro Apr 15, 2020
950a472
Test if correct array of words is returned
AlineRibeiro Apr 15, 2020
b9b6448
Change gsub and splits to match self-care input with self_care tag
AlineRibeiro Apr 15, 2020
bb75e8a
Associate moment factory with categories factory
AlineRibeiro Apr 15, 2020
8bbf9ac
Associate moment factory with moods and strategy factory and code cle…
AlineRibeiro Apr 15, 2020
4be1a55
Create stub and test if resources method return the right resources
AlineRibeiro Apr 15, 2020
70d44ed
Test if resources method wont return any resources if none of the tag…
AlineRibeiro Apr 15, 2020
3c1a8a7
Code clean up
AlineRibeiro Apr 15, 2020
33d2108
Refacot test to account for special chars and hyphen cases
AlineRibeiro Apr 15, 2020
93eee5e
Test clean up
AlineRibeiro Apr 16, 2020
85e4a00
Revert client yarn.lock
AlineRibeiro Apr 16, 2020
eaf70b4
Change subject to let
AlineRibeiro Apr 16, 2020
babf5f6
Change let to subject and code clean up
AlineRibeiro Apr 16, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/controllers/moments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def index
# GET /moments/1.json
def show
show_with_comments(@moment)
@resources = ResourceRecommendation.new(@moment).resources
end

# GET /moments/new
Expand Down
55 changes: 55 additions & 0 deletions app/services/moment_keywords.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# frozen_string_literal: true

class MomentKeywords
def initialize(moment)
@moment = moment
@moment_keywords = []
end

def extract_moment_keywords
extract_keywords(@moment.categories)
extract_keywords(@moment.moods)
extract_keywords(@moment.strategies)
extract_moment_name
extract_moment_why
extract_moment_fix
remove_special_chars
downcase_keywords
end

private

def strip_tags(str)
ActionController::Base.helpers.strip_tags(str)
end

def extract_keywords(array)
array.each do |item|
@moment_keywords.push(item['name'].split,
strip_tags(item['description']).split)
end
end

def extract_moment_name
@moment_keywords.push(@moment.name.split)
end

def extract_moment_why
@moment_keywords.push(strip_tags(@moment.why).split)
end

def extract_moment_fix
@moment_keywords.push(strip_tags(@moment.fix).split)
end

def remove_special_chars
@moment_keywords = @moment_keywords.flatten.each do |keyword|
keyword.tr!('-', ' ')
keyword.gsub!(/[^\p{Alpha} -]/, '')
end
end

def downcase_keywords
@moment_keywords = @moment_keywords.map(&:downcase)
end
end
24 changes: 24 additions & 0 deletions app/services/resource_recommendation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

class ResourceRecommendation
def initialize(moment)
@moment = moment
@moment_keywords = []
end

def resources
@moment_keywords = MomentKeywords.new(@moment).extract_moment_keywords
all_resources.select do |resource|
tags = resource['tags'].flat_map do |tag|
tag.tr('_', ' ')
end
(tags & @moment_keywords).any?
end
end

private

def all_resources
JSON.parse(File.read(Rails.root.join('doc', 'pages', 'resources.json')))
end
end
10 changes: 10 additions & 0 deletions app/views/moments/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@
</div>
<% end %>

<div class="smallMarginTop">
<div class="label"><%= label_tag "What resources could help?" %> </div>
<ul>
<% @resources.take(3).each do |item| %>
<li><%= link_to item['name'], item['link'] %></li>
<%end %>
<li><%= link_to 'More...', resources_path %></li>
</ul>
</div>

<% if @moment.owned_by?(current_user) && @moment.shared? %>
<div class="smallMarginTop">
<%= react_component('Form', props: secret_share_props(@moment)) %>
Expand Down
38 changes: 38 additions & 0 deletions spec/services/moment_keywords_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
describe MomentKeywords do
let(:moment) do
FactoryBot.build(
:moment,
categories: [build(:category, name: 'free', description: 'Description')],
moods: [build(:mood, name: 'Name', description: 'Blog^^')],
strategies: [build(:strategy, name: 'Name', description: 'books@!##.')],
name: "ADDICTION",
why: "self-care.",
fix: "@Teachers!!"
)
end

subject(:keywords) { MomentKeywords.new(moment).extract_moment_keywords }

it 'downcases words' do
expect(keywords).to include("addiction")
end

it 'removes hyphen from compound words' do
expect(keywords).to include("self care")
end

it 'removes special characters' do
expect(keywords).to include("teachers")
end

it 'returns an array of all the key words' do
expect(keywords).to eq(
["free", "description","name", "blog", "name", "books",
"addiction", "self care", "teachers"]
)
end
end




Comment on lines +34 to +38

Choose a reason for hiding this comment

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

These extra empty lines must be removed

22 changes: 22 additions & 0 deletions spec/services/resource_recommendation_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
describe ResourceRecommendation do
subject(:moment) {FactoryBot.build(:moment,

Choose a reason for hiding this comment

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

Suggested change
subject(:moment) {FactoryBot.build(:moment,
let(:moment) {FactoryBot.build(:moment,

name: 'SELF-INJURY@',
why: 'text',
fix: 'text' )}

let(:resources) { ResourceRecommendation.new(moment).resources}

Choose a reason for hiding this comment

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

Suggested change
let(:resources) { ResourceRecommendation.new(moment).resources}
subject(:resources) { ResourceRecommendation.new(moment).resources}


describe 'A test for resources method from ResourceRecommendation class' do
it 'returns recommended resources based on moment keywords' do
available_resource = [{'tags'=> ["self_injury"]}]
allow(JSON).to receive(:parse) { available_resource }
expect(resources).to eq(available_resource)
end

it 'does not return any matched resources' do
available_resource2 = [{'tags'=> ['self_care', 'anonymous', 'ios', 'communities']}]
allow(JSON).to receive(:parse) { available_resource2 }
expect(resources).not_to eq(available_resource2)
end
end
end