Class: WordMatch

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

Class Method Summary collapse

Class Method Details

.containsWord(articles, word) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/wordMatch.rb', line 7

def self.containsWord(articles, word)
    matches_arr = Array.new
    articles.each do |article|

        title = article["title"]
        description = article["description"]
        content = article["content"]

        isTitle = !title.nil?
        isDescription = !description.nil?
        isContent = !content.nil?

        found = false

        if isTitle
            if title.match(word)
                found = true
            end
        end

        if isDescription
            if description.match(word)
                found = true
            end
        end

        if isContent
            if content.match(word)
                found = true
            end
        end

        if found
            matches_arr.push(article)
        end
    end

    if matches_arr.length < 1
        str = "No Matches Found"
        return str

    else
 arr_length = matches_arr.length
 articles_json = JSON.generate(matches_arr)
 articles_obj = JSON.parse(articles_json)
 append_json = {"status":"ok","totalResults":arr_length,"articles":articles_obj}
 return json = JSON.generate(append_json)
    end

end