Class: Infoboxer::MediaWiki

Inherits:
Object
  • Object
show all
Defined in:
lib/infoboxer/media_wiki.rb,
lib/infoboxer/media_wiki/page.rb,
lib/infoboxer/media_wiki/traits.rb

Overview

MediaWiki client class.

Usage:

client = Infoboxer::MediaWiki.new('http://en.wikipedia.org/w/api.php', user_agent: 'My Own Project')
page = client.get('Argentina')

Consider using shortcuts like wiki, wikipedia, wp and so on instead of direct instation of this class (although you can if you want to!)

Defined Under Namespace

Classes: Page, Traits

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • api_base_url

    URL of api.php file in your MediaWiki installation. Typically, its <domain>/w/api.php, but can vary in different wikis.

  • options (defaults to: {})

    Only one option is currently supported:

    • :user_agent (also aliased as :ua) -- custom User-Agent header.


50
51
52
53
# File 'lib/infoboxer/media_wiki.rb', line 50

def initialize(api_base_url, options = {})
  @api_base_url = Addressable::URI.parse(api_base_url)
  @resource = RestClient::Resource.new(api_base_url, headers: headers(options))
end

Class Attribute Details

.user_agentObject

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_urlObject (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.

Returns:

  • (Tree::Nodes<Page>)

    array of parsed pages. Notes:

    • if you call get with only one title, one page will be returned instead of an array
    • if some of pages are not in wiki, they will not be returned, therefore resulting array can be shorter than titles array; you can always check pages.map(&:title) to see what you've really received; this approach allows you to write absent-minded code like this:
      Infoboxer.wp.get('Argentina', 'Chile', 'Something non-existing').
         infobox.fetch('some value')
    

    and obtain meaningful results instead of NoMethodError or some NotFound.



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).

Returns:

  • (Array<Hash>)


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