class Wikipedia::Client

Constants

BASE_URL_OPTIONS
BASE_URL_TEMPLATE

see en.wikipedia.org/w/api.php

Attributes

follow_redirects[RW]

Public Class Methods

new(configuration = Wikipedia::Configuration.new) click to toggle source
# File lib/wikipedia/client.rb, line 14
def initialize(configuration = Wikipedia::Configuration.new)
  @configuration = configuration
  self.follow_redirects = true
end

Public Instance Methods

find( title, options = {} ) click to toggle source
# File lib/wikipedia/client.rb, line 19
def find( title, options = {} )
  title = Url.new(title).title rescue title
  page = Page.new( request_page( title, options ) )
  while follow_redirects && page.redirect?
    page = Page.new( request_page( page.redirect_title, options ) )
  end
  page
end
find_image( title, options = {} ) click to toggle source
# File lib/wikipedia/client.rb, line 28
def find_image( title, options = {} )
  title = Url.new(title).title rescue title
  Page.new( request_image( title, options ) )
end
find_random( options = {} ) click to toggle source
# File lib/wikipedia/client.rb, line 33
def find_random( options = {} )
  require 'json'
  data = JSON.parse( request_random( options ) )
  title = data['query']['pages'].values[0]['title']
  find( title, options )
end
request( options ) click to toggle source
# File lib/wikipedia/client.rb, line 75
def request( options )
  URI.parse( url_for( options ) ).read( headers )
end
request_image( title, options = {} ) click to toggle source

# File lib/wikipedia/client.rb, line 55
def request_image( title, options = {} )
  request( {
    action: 'query',
    prop: 'imageinfo',
    iiprop: 'url',
    iiurlwidth: options && options[:iiurlwidth] ? options[:iiurlwidth] : 200,
    titles: title
  }.merge( options ) )
end
request_page( title, options = {} ) click to toggle source

en.wikipedia.org/w/api.php?action=query&format=json&prop=revisions%7Clinks%7Cimages%7Ccategories&rvprop=content&titles=Flower%20(video%20game)

# File lib/wikipedia/client.rb, line 41
def request_page( title, options = {} )
  request( {
    action: 'query',
    prop: %w[info revisions links extlinks images categories coordinates templates extracts pageimages langlinks],
    rvprop: 'content',
    inprop: 'url',
    pithumbsize: 200,
    explaintext: '',
    lllimit: 500,
    titles: title
  }.merge( options ) )
end
request_random( options = {} ) click to toggle source

en.wikipedia.org/w/api.php?action=query&generator=random&grnnamespace=0&prop=info

# File lib/wikipedia/client.rb, line 66
def request_random( options = {} )
  request( {
    action: 'query',
    generator: 'random',
    grnnamespace: '0',
    prop: 'info'
  }.merge( options ) )
end

Protected Instance Methods

configuration_options() click to toggle source
# File lib/wikipedia/client.rb, line 81
def configuration_options
  {
    protocol: @configuration[:protocol],
    domain:   @configuration[:domain],
    path:     @configuration[:path]
  }
end
headers() click to toggle source
# File lib/wikipedia/client.rb, line 118
def headers
  {
    'User-Agent' => @configuration[:user_agent]
  }.merge( @configuration[:headers] )
end
normalize_value( val ) click to toggle source
# File lib/wikipedia/client.rb, line 100
def normalize_value( val )
  case val
  when Array
    val.flatten.join( '|' )
  else
    val
  end
end
split_hash(hash, keys) click to toggle source
# File lib/wikipedia/client.rb, line 109
def split_hash(hash, keys)
  h1 = {}
  h2 = {}
  hash.each do |k, v|
    (keys.include?(k) ? h1 : h2).store(k, v)
  end
  [h1, h2]
end
url_for(options) click to toggle source
# File lib/wikipedia/client.rb, line 89
def url_for(options)
  options = configuration_options.merge( options )

  url_options, query_options = split_hash(options, BASE_URL_OPTIONS)
  normalized_query_options = query_options.map { |k, v| [k, normalize_value(v)] }

  base_url = BASE_URL_TEMPLATE % url_options
  query_string = Addressable::URI.form_encode(normalized_query_options)
  base_url + '&' + query_string
end