Class: Infoboxer::MediaWiki
- Inherits:
-
Object
- Object
- Infoboxer::MediaWiki
- Defined in:
- lib/infoboxer/media_wiki.rb,
lib/infoboxer/media_wiki/page.rb,
lib/infoboxer/media_wiki/traits.rb
Overview
Defined Under Namespace
Constant Summary collapse
- UA =
Default Infoboxer User-Agent header.
You can set yours as an option to Infoboxer.wiki and its shortcuts, or to #initialize
"Infoboxer/#{Infoboxer::VERSION} (https://github.com/molybdenum-99/infoboxer; [email protected])"
Class Attribute Summary collapse
-
.user_agent ⇒ Object
User agent getter/setter.
Instance Attribute Summary collapse
-
#api_base_url ⇒ Object
readonly
Returns the value of attribute api_base_url.
Instance Method Summary collapse
-
#get(*titles) ⇒ Tree::Nodes<Page>
Receive list of parsed wikipedia pages for list of titles provided.
-
#initialize(api_base_url, options = {}) ⇒ MediaWiki
constructor
Creating new MediaWiki client.
-
#raw(*titles) ⇒ Array<Hash>
Receive "raw" data from Wikipedia (without parsing or wrapping in classes).
Constructor Details
#initialize(api_base_url, options = {}) ⇒ MediaWiki
Creating new MediaWiki client. Infoboxer.wiki provides shortcut for it, as well as shortcuts for some well-known wikis, like Infoboxer.wikipedia.
50 51 52 53 |
# File 'lib/infoboxer/media_wiki.rb', line 50 def initialize(api_base_url, = {}) @api_base_url = Addressable::URI.parse(api_base_url) @resource = RestClient::Resource.new(api_base_url, headers: headers()) end |
Class Attribute Details
.user_agent ⇒ Object
User agent getter/setter.
Default value is UA.
You can also use per-instance option, see #initialize
36 37 38 |
# File 'lib/infoboxer/media_wiki.rb', line 36 def user_agent @user_agent end |
Instance Attribute Details
#api_base_url ⇒ Object (readonly)
Returns the value of attribute api_base_url.
39 40 41 |
# File 'lib/infoboxer/media_wiki.rb', line 39 def api_base_url @api_base_url end |
Instance Method Details
#get(*titles) ⇒ Tree::Nodes<Page>
Receive list of parsed wikipedia pages for list of titles provided. All pages are received with single query to MediaWiki API.
NB: currently, if you are requesting more than 50 titles at once (MediaWiki limitation for single request), Infoboxer will not try to get other pages with subsequent queries. This will be fixed in future.
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/infoboxer/media_wiki.rb', line 89 def get(*titles) pages = raw(*titles).reject{|raw| raw[:content].nil?}. map{|raw| traits = Traits.get(@api_base_url.host, extract_traits(raw)) Page.new(self, Parser.paragraphs(raw[:content], traits), raw.merge(traits: traits)) } titles.count == 1 ? pages.first : Tree::Nodes[*pages] end |
#raw(*titles) ⇒ Array<Hash>
Receive "raw" data from Wikipedia (without parsing or wrapping in classes).
59 60 61 62 63 |
# File 'lib/infoboxer/media_wiki.rb', line 59 def raw(*titles) postprocess @resource.get( params: DEFAULT_PARAMS.merge(titles: titles.join('|')) ) end |