Class: Tweetkit::Search

Inherits:
Object
  • Object
show all
Defined in:
lib/tweetkit/search.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(term) ⇒ Search

Returns a new instance of Search.



7
8
9
# File 'lib/tweetkit/search.rb', line 7

def initialize(term)
  @search_term = term + ' '
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(connector, *terms, &block) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/tweetkit/search.rb', line 109

def method_missing(connector, *terms, &block)
  if connector.match?('one_of')
    build_one_of_connector(connector, terms, &block)
  else
    build_connector(connector, terms, &block)
  end
end

Instance Attribute Details

#search_termObject

Returns the value of attribute search_term.



5
6
7
# File 'lib/tweetkit/search.rb', line 5

def search_term
  @search_term
end

Instance Method Details

#add_connector(term, grouped: true) ⇒ Object



35
36
37
# File 'lib/tweetkit/search.rb', line 35

def add_connector(term, grouped: true)
  grouped ? connectors.push("(#{term})") : connectors.push(term)
end

#build_connector(connector, terms, &block) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/tweetkit/search.rb', line 39

def build_connector(connector, terms, &block)
  query = ''
  connector =
    connector
    .to_s
    .delete_prefix('and_')
    .delete_prefix('or_')
    .delete_suffix('_one_of')
    .to_sym
  if connector.match?('contains')
    terms.each do |term|
      term = term.to_s.strip
      if term.split.length > 1
        query += "\"#{term}\" "
      else
        query += "#{term} "
      end
    end
    update_search_term(query)
  elsif connector.match?('without')
    terms.each do |term|
      term = term.to_s.strip
      if term.split.length > 1
        query += "-\"#{term}\" "
      else
        query += "-#{term} "
      end
    end
    add_connector(query.rstrip, grouped: false)
  elsif connector.match?('is_not')
    connector = connector.to_s.delete_suffix('_not').to_sym
    terms.each do |term|
      term = term.to_s.strip
      query += "-#{connector}:#{term} "
    end
    add_connector(query.rstrip)
  else
    terms.each do |term|
      term = term.to_s.strip
      query += "#{connector}:#{term} "
    end
    add_connector(query.rstrip, grouped: false)
  end
  block_given? ? yield : self
end

#build_one_of_connector(connector, terms, &block) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/tweetkit/search.rb', line 85

def build_one_of_connector(connector, terms, &block)
  query = []
  connector =
    connector
    .to_s
    .delete_prefix('and_')
    .delete_prefix('or_')
    .delete_suffix('_one_of')
    .to_sym
  if connector.match?('contains')
    terms.each do |term|
      term = term.to_s.strip
      query << term
    end
  else
    terms.each do |term|
      term = term.to_s.strip
      query << "#{connector}: #{term}"
    end
  end
  add_connector(query.join(' OR '))
  block_given? ? yield : self
end

#combine_connectorsObject



31
32
33
# File 'lib/tweetkit/search.rb', line 31

def combine_connectors
  connectors.join(' ')
end

#combined_queryObject



27
28
29
# File 'lib/tweetkit/search.rb', line 27

def combined_query
  @combined_query = "#{@search_term.strip} #{combine_connectors}"
end

#connectorsObject



15
16
17
# File 'lib/tweetkit/search.rb', line 15

def connectors
  @connectors ||= opts[:connectors] = []
end

#optsObject



11
12
13
# File 'lib/tweetkit/search.rb', line 11

def opts
  @opts ||= {}
end

#respond_to_missing?(method) ⇒ Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/tweetkit/search.rb', line 117

def respond_to_missing?(method)
  method.match?('one_of') || super
end

#setup(&block) ⇒ Object



23
24
25
# File 'lib/tweetkit/search.rb', line 23

def setup(&block)
  instance_eval(&block)
end

#update_search_term(term) ⇒ Object



19
20
21
# File 'lib/tweetkit/search.rb', line 19

def update_search_term(term)
  @search_term += term
end