Class: CombineHashtags::Controller
- Inherits:
-
Object
- Object
- CombineHashtags::Controller
- Defined in:
- lib/combine_hashtags/controller.rb
Overview
controller class to direct user actions
Instance Method Summary collapse
-
#clean_posts ⇒ Object
removes posts w/o hashtags.
-
#initialize(profile) ⇒ Controller
constructor
A new instance of Controller.
-
#list ⇒ Object
returns 10 most popular/used hashtags.
-
#match(keywords) ⇒ Object
checks if keywords match any of the tags included in each post.
-
#search ⇒ Object
displays popular hashtags, gets search keywords from user, returns matching posts.
-
#setup ⇒ Object
fetches data for a new profile and stores it locally.
Constructor Details
#initialize(profile) ⇒ Controller
Returns a new instance of Controller.
6 7 8 9 10 11 |
# File 'lib/combine_hashtags/controller.rb', line 6 def initialize(profile) @profile = profile @posts = clean_posts @view = CombineHashtags::View.new @query = CombineHashtags::Query.new end |
Instance Method Details
#clean_posts ⇒ Object
removes posts w/o hashtags
22 23 24 |
# File 'lib/combine_hashtags/controller.rb', line 22 def clean_posts @profile.posts.select { |post| post unless post..empty? } end |
#list ⇒ Object
returns 10 most popular/used hashtags
27 28 29 30 |
# File 'lib/combine_hashtags/controller.rb', line 27 def list content = CombineHashtags::Post.popular.sort_by { |_, value| -value } @view.(content.first(10)) end |
#match(keywords) ⇒ Object
checks if keywords match any of the tags included in each post
41 42 43 44 45 46 47 |
# File 'lib/combine_hashtags/controller.rb', line 41 def match(keywords) first_set = @posts.select { |post| post..include?(keywords[:first]) } second_set = first_set.select { |post| post..include?(keywords[:second]) } third_set = second_set.select { |post| post..include?(keywords[:third]) } keywords[:third].empty? ? second_set : third_set end |
#search ⇒ Object
displays popular hashtags, gets search keywords from user, returns matching posts
33 34 35 36 37 38 |
# File 'lib/combine_hashtags/controller.rb', line 33 def search list keywords = @view.search results = match(keywords) @view.results(results) end |
#setup ⇒ Object
fetches data for a new profile and stores it locally
14 15 16 17 18 19 |
# File 'lib/combine_hashtags/controller.rb', line 14 def setup # removing previous profile @profile.destroy puts "loading..." @query.call_api end |