Class: QuickSearch::WikipediaSearcher

Inherits:
Searcher
  • Object
show all
Defined in:
app/searchers/quick_search/wikipedia_searcher.rb

Instance Method Summary collapse

Instance Method Details

#base_urlObject



29
30
31
# File 'app/searchers/quick_search/wikipedia_searcher.rb', line 29

def base_url
  "https://en.wikipedia.org/w/api.php"
end


57
58
59
# File 'app/searchers/quick_search/wikipedia_searcher.rb', line 57

def build_link(value)
  "https://en.wikipedia.org/wiki/" + value['title']
end


53
54
55
# File 'app/searchers/quick_search/wikipedia_searcher.rb', line 53

def loaded_link
  "https://en.wikipedia.org/wiki/Special:Search?fulltext=Search&search=" + http_request_queries['uri_escaped']
end

#parametersObject



33
34
35
36
37
38
39
40
41
42
43
# File 'app/searchers/quick_search/wikipedia_searcher.rb', line 33

def parameters
  {
    'action' => 'query',
    'list' => 'search',
    'srsearch' => http_request_queries['not_escaped'],
    'sroffset' => @offset,
    'srlimit' => @per_page,
    'utf8' => true,
    'format' => 'json'
  }
end

#resultsObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/searchers/quick_search/wikipedia_searcher.rb', line 9

def results
  if results_list
    results_list

  else
    @results_list = []

    @response['query']['search'].each do |value|
      result = OpenStruct.new
      result.title = value['title']
      result.link = build_link(value)
      result.description = value['snippet']
      @results_list << result
    end

    @results_list
  end

end

#searchObject



4
5
6
7
# File 'app/searchers/quick_search/wikipedia_searcher.rb', line 4

def search
  resp = @http.get(base_url, parameters)
  @response = JSON.parse(resp.body)
end

#spelling_suggestionObject



49
50
51
# File 'app/searchers/quick_search/wikipedia_searcher.rb', line 49

def spelling_suggestion
  @response['query']['searchinfo']['suggestion']
end

#totalObject



45
46
47
# File 'app/searchers/quick_search/wikipedia_searcher.rb', line 45

def total
  @response['query']['searchinfo']['totalhits']
end