lib/infoboxer/media_wiki.rb in infoboxer-0.2.2 vs lib/infoboxer/media_wiki.rb in infoboxer-0.2.3

- old
+ new

@@ -2,10 +2,11 @@ #require 'rest-client' #require 'json' require 'mediawiktory' require 'addressable/uri' +require_relative 'media_wiki/mediawiktory_patch' require_relative 'media_wiki/traits' require_relative 'media_wiki/page' module Infoboxer # MediaWiki client class. @@ -63,11 +64,12 @@ @client.query. titles(*part). prop(revisions: {prop: :content}, info: {prop: :url}). redirects(true). # FIXME: should be done transparently by MediaWiktory? perform.pages - }.inject(:concat) # somehow flatten(1) fails! + }.inject(:concat). # somehow flatten(1) fails! + sort_by{|page| titles.index(page.queried_title) || 1_000} end # Receive list of parsed MediaWiki pages for list of titles provided. # All pages are received with single query to MediaWiki API. # @@ -100,9 +102,30 @@ Page.new(self, Parser.paragraphs(raw.content, traits), raw) } titles.count == 1 ? pages.first : Tree::Nodes[*pages] + end + + # Same as {#get}, but returns hash of {requested title => page}. + # + # Useful quirks: + # * when requested page not existing, key will be still present in + # resulting hash (value will be `nil`); + # * when requested page redirects to another, key will still be the + # requested title. For ex., `get_h('Einstein')` will return hash + # with key 'Einstein' and page titled 'Albert Einstein'. + # + # This allows you to be in full control of what pages of large list + # you've received. + # + # @return [Hash<String, Page>] + # + def get_h(*titles) + pages = [*get(*titles)] + titles.map{|t| + [t, pages.detect{|p| p.source.queried_title == t}] + }.to_h end # Receive list of parsed MediaWiki pages from specified category. # # **NB**: currently, this API **always** fetches all pages from