Class: MediaWiki::Query
- Inherits:
-
Object
- Object
- MediaWiki::Query
- Includes:
- HTTParty
- Defined in:
- lib/mediawiki-keiki/query.rb
Instance Attribute Summary collapse
-
#query ⇒ Object
Returns the value of attribute query.
-
#query_result(force = false) ⇒ Object
Unless force is true, uses existing query_result if it has already been cached.
Instance Method Summary collapse
- #find_result_map_match_for_title(result_map, page_title) ⇒ Object
- #get_final_title(result_map_hash) ⇒ Object
-
#get_normalizations_for(result_map, normalized) ⇒ Object
Add :normalized key to the result map if the original search term was normalized.
-
#get_query_map(map_type) ⇒ Object
Get maps (normalization and redirect) from the wiki query result.
-
#get_redirects_for(result_map, redirects) ⇒ Object
Add :redirected key to the result map if the either the original search term or the normalized term were redirected.
-
#initialize(query) ⇒ Query
constructor
A new instance of Query.
-
#initialize_map ⇒ Object
Initially create an array of hashses with the original :search_terms.
-
#map_query_to_results ⇒ Object
Maps original query to results.
-
#pages ⇒ Object
Returns a hash filled with Pages.
Constructor Details
#initialize(query) ⇒ Query
Returns a new instance of Query.
16 17 18 19 20 21 22 23 24 |
# File 'lib/mediawiki-keiki/query.rb', line 16 def initialize(query) # The WikiMedia API requires that requests be limited to 50 sites or less raise ArgumentError, "Query exceeds WikiMedia maximum number of sites (50)" unless query.count("|") < 50 @query = query @page_hash = Hash.new end |
Instance Attribute Details
#query ⇒ Object
Returns the value of attribute query.
5 6 7 |
# File 'lib/mediawiki-keiki/query.rb', line 5 def query @query end |
#query_result(force = false) ⇒ Object
Unless force is true, uses existing query_result if it has already been cached
27 28 29 |
# File 'lib/mediawiki-keiki/query.rb', line 27 def query_result @query_result end |
Instance Method Details
#find_result_map_match_for_title(result_map, page_title) ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/mediawiki-keiki/query.rb', line 47 def find_result_map_match_for_title(result_map, page_title) result_map.each do |h| final_title = get_final_title(h) if page_title == final_title return h[:search_term] end end nil end |
#get_final_title(result_map_hash) ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/mediawiki-keiki/query.rb', line 57 def get_final_title(result_map_hash) if result_map_hash[:redirected] final_title = result_map_hash[:redirected] elsif result_map_hash[:normalized] final_title = result_map_hash[:normalized] else final_title = result_map_hash[:search_term] end final_title end |
#get_normalizations_for(result_map, normalized) ⇒ Object
Add :normalized key to the result map if the original search term was normalized
101 102 103 104 105 106 107 108 |
# File 'lib/mediawiki-keiki/query.rb', line 101 def get_normalizations_for(result_map, normalized) result_map.each do |rm| normalized.each do |n| rm[:normalized] = n["to"] if n["from"] == rm[:search_term] end end result_map end |
#get_query_map(map_type) ⇒ Object
Get maps (normalization and redirect) from the wiki query result
121 122 123 |
# File 'lib/mediawiki-keiki/query.rb', line 121 def get_query_map(map_type) query_result["query"][map_type] # if query_result["query"][map_type] end |
#get_redirects_for(result_map, redirects) ⇒ Object
Add :redirected key to the result map if the either the original search term or the normalized term were redirected
111 112 113 114 115 116 117 118 |
# File 'lib/mediawiki-keiki/query.rb', line 111 def get_redirects_for(result_map, redirects) result_map.each do |rm| redirects.each do |r| rm[:redirected] = r["to"] if r["from"] == rm[:search_term] || r["from"] == rm[:normalized] end end result_map end |
#initialize_map ⇒ Object
Initially create an array of hashses with the original :search_terms
92 93 94 95 96 97 98 |
# File 'lib/mediawiki-keiki/query.rb', line 92 def initialize_map result_map = [] original_terms = @query.split('|') original_terms.each { |k| result_map << {:search_term => k } } result_map end |
#map_query_to_results ⇒ Object
Maps original query to results
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/mediawiki-keiki/query.rb', line 70 def map_query_to_results #Initalize map result_map = initialize_map # Apply the normalization to the result map normalized = get_query_map("normalized") if normalized result_map = get_normalizations_for(result_map, normalized) end # Apply the redirects to the result map redirects = get_query_map("redirects") if redirects result_map = get_redirects_for(result_map, redirects) end result_map end |
#pages ⇒ Object
Returns a hash filled with Pages
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/mediawiki-keiki/query.rb', line 32 def pages result_map = map_query_to_results query_result["query"]["pages"].each do |key, value| page_title = value["title"] original_query = find_result_map_match_for_title(result_map, page_title) @page_hash[original_query] = MediaWiki::Page.new(value) end @page_hash end |