Class: CombineHashtags::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/combine_hashtags/controller.rb

Overview

controller class to direct user actions

Instance Method Summary collapse

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_postsObject

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.tags.empty? }
end

#listObject

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.tags(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.tags.include?(keywords[:first]) }
  second_set = first_set.select { |post| post.tags.include?(keywords[:second]) }
  third_set = second_set.select { |post| post.tags.include?(keywords[:third]) }
  keywords[:third].empty? ? second_set  : third_set
 
end

#searchObject

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

#setupObject

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