Class: WhatIs
- Inherits:
-
Object
- Object
- WhatIs
- Defined in:
- lib/whatis.rb,
lib/whatis/cli.rb,
lib/whatis/thisis.rb,
lib/whatis/formatter.rb,
lib/whatis/refinements.rb,
lib/whatis/thisis/link.rb,
lib/whatis/thisis/ambigous.rb,
lib/whatis/thisis/notfound.rb
Overview
‘WhatIs` is a simple entity resolver through Wikipedia.
See #this and #these methods docs for details on call sequence and options, and response classes:
-
ThisIs – normal response object;
-
ThisIs::Ambigous – response object representing disambiguation page;
-
ThisIs::NotFound – response object for not found page, includes search for term service.
Defined Under Namespace
Modules: Refinements Classes: CLI, Description, Formatter, ThisIs
Constant Summary collapse
- AMBIGOUS_CATEGORIES =
This constant lists Wikipedia ambiguity categories per Wikipedia language. For ThisIs::Ambigous feature to work for your language, this list should include it.
{ be: ['Катэгорыя:Неадназначнасці'], en: ['Category:All disambiguation pages', 'Category:All set index articles'], ru: ['Категория:Страницы значений по алфавиту'], uk: ['Категорія:Всі статті визначеного індексу', 'Категорія:Всі сторінки неоднозначності статей'] }.freeze
Instance Attribute Summary collapse
- #language ⇒ Object readonly
Class Method Summary collapse
- .[](lang) ⇒ WhatIs
- .these(*titles, **options) ⇒ Hash{String => ThisIs, ThisIs::Ambigous, ThisIs::NotFound}
- .this(title, **options) ⇒ ThisIs, ...
Instance Method Summary collapse
- #ambigous_categories ⇒ Object
-
#initialize(language = :en) ⇒ WhatIs
constructor
A new instance of WhatIs.
- #inspect ⇒ String
-
#search(title, limit = 5, **options) ⇒ Object
Used by ThisIs::NotFound#search.
-
#these(*titles, **options) ⇒ Hash{String => ThisIs, ThisIs::Ambigous, ThisIs::NotFound}
Batch resolving of several entities.
-
#this(title, **options) ⇒ ThisIs, ...
Resolves one entity through Wikipedia API.
Constructor Details
#initialize(language = :en) ⇒ WhatIs
Returns a new instance of WhatIs.
99 100 101 102 |
# File 'lib/whatis.rb', line 99 def initialize(language = :en) @language = language @infoboxer = Infoboxer.wikipedia(language) end |
Instance Attribute Details
#language ⇒ Object (readonly)
96 97 98 |
# File 'lib/whatis.rb', line 96 def language @language end |
Class Method Details
.these(*titles, **options) ⇒ Hash{String => ThisIs, ThisIs::Ambigous, ThisIs::NotFound}
71 72 73 |
# File 'lib/whatis.rb', line 71 def these(*titles, **) self[:en].these(*titles, **) end |
Instance Method Details
#ambigous_categories ⇒ Object
173 174 175 |
# File 'lib/whatis.rb', line 173 def ambigous_categories @ambigous_categories = AMBIGOUS_CATEGORIES.fetch(language.to_sym, []) end |
#inspect ⇒ String
160 161 162 |
# File 'lib/whatis.rb', line 160 def inspect "#<WhatIs(#{language}). Usage: .this(*pages, **options)>" end |
#search(title, limit = 5, **options) ⇒ Object
Used by WhatIs::ThisIs::NotFound#search
166 167 168 169 170 |
# File 'lib/whatis.rb', line 166 def search(title, limit = 5, **) @infoboxer .search(title, limit: limit) { |req| setup_request(req, **) } .map { |page| ThisIs.create(self, page.title, page) } end |
#these(*titles, **options) ⇒ Hash{String => ThisIs, ThisIs::Ambigous, ThisIs::NotFound}
Batch resolving of several entities. Wikipedia API allows batch fetching of pages, so there would be roughly 1 API call for each 50 entities. Number of API calls could be larger if additional information (languages, categories) is requested.
127 128 129 130 131 132 133 |
# File 'lib/whatis.rb', line 127 def these(*titles, **) titles.any? or fail(ArgumentError, "Usage: `these('Title 1', 'Title 2', ..., **options). At least one title is required.") @infoboxer .get_h(*titles) { |req| setup_request(req, **) } .map { |title, page| [title, ThisIs.create(self, title, page)] }.to_h end |
#this(title, **options) ⇒ ThisIs, ...
Special WhatIs::ThisIs::Ambigous wrapper for disambiguation pages can be created only for those language Wikipedias which ‘WhatIs` knows “disambiguation” category name, see AMBIGOUS_CATEGORIES.
Resolves one entity through Wikipedia API.
155 156 157 |
# File 'lib/whatis.rb', line 155 def this(title, **) these(title, **).values.first end |