WikipediaWrapper
WikipediaWrapper is a ruby gem that extracts information from Wikipedia and makes the information available in an easy-to-use API.
All information is extracted using the Wikipedia/MediaWiki API.
This gem is inspired by the Python wrapper for the Wikipedia API by Jonathan Goldsmith.
Installation
Install WikipediaWrapper like any other Ruby gem:
gem install wikipedia_wrapper
Or, if you're using Rails/Bundler, add this to your Gemfile:
gem 'wikipedia_wrapper'
and run at the command prompt:
bundle install
Usage
Before you can use the gem, you need to require it in your file:
require 'wikipedia_wrapper'
The following are just some example usages. For detailed information, check the documentation.
Configuration
To use this gem you do not need to specify configuration options, but you can. The values specified in the example below are the default values. It is however highly recommended that you choose a caching client and set that before making a lot of requests. The caching functionality is provided by the cache gem. See it's documentation for more information on possible caching clients.
WikipediaWrapper.configure do |config|
config.lang = 'en'
config.user_agent = 'WikipediaWrapper/0.0.1 (http://sykaeh.github.com/wikipedia_wrapper/) Ruby/2.2.1'
config.default_ttl = 604800
end
WikipediaWrapper.cache(Memcached.new('127.0.0.1:11211', :binary_protocol => true))
WikipediaWrapper.cache(Dalli::Client.new)
WikipediaWrapper.cache(Redis.new)
WikipediaWrapper.cache(Rails.cache)
Autocomplete suggestions
Autocomplete suggestions are the suggestions you get when you start typing a word in the search box on the page. You get the complete title for each page and a short summary. You can specify a limit with the keyword argument limit
(default is 10).
WikipediaWrapper.autocomplete('api', limit: 3)
Search
Search does a full-text search of the term you provided, and returns the list of page titles that match and short snippets containing the search term.
WikipediaWrapper.search('api', limit: 3)
Summary
Summary is a convenience function to get the first couple of character, sentences of all of the text before the first section (introductory text) either in plain text or as basic HTML markup.
WikipediaWrapper.summary('Ruby')
WikipediaWrapper.summary('Ruby', chars: 100)
WikipediaWrapper.summary('Ruby', sentences: 10)
WikipediaWrapper.summary('Ruby', html: true)
Complete page
To get all of the information on a particular page, use the page
convenience wrapper or use the Page
class directly.
page = WikipediaWrapper.page('Ruby')
page = WikipediaWrapper::Page.new('Ruby')
page.title
page.url
page.extract
Tests
WikipediaWrapper comes with a test suite (just run rake test).
License
Copyright (c) 2015 Sybil Ehrensberger, released under the MIT license