module Wik

Constants

VERSION

Public Instance Methods

describe(title=nil, id=nil) click to toggle source

Just give a description of an entry

# File lib/wik.rb, line 141
def describe(title=nil, id=nil)
  puts "In development..."
end
find(phrase, limit=15, snippet=false, display=true) click to toggle source

Do a search by phrase, returns a hash with full search data, but first prints the parsed search data

# File lib/wik.rb, line 23
def find(phrase, limit=15, snippet=false, display=true)
  search = phrase.split.join("_")
  if snippet
    endpoint = "https://en.wikipedia.org/w/api.php?action=query&format=json&list=search&srsearch=#{search}&srlimit=#{limit}"
  else
    endpoint = "https://en.wikipedia.org/w/api.php?action=query&format=json&list=search&srsearch=#{search}&srlimit=#{limit}&srprop"
  end
  hash = get(endpoint)
  info = hash["query"]["searchinfo"]
  results = hash["query"]["search"]
  if display
    puts "Total hits : #{ info["totalhits"] }"
    if info["suggestion"]
      puts "Suggestion : #{ info["suggestion"] }"
    end
    puts "Displaying #{results.length} results:"
    results.each do |result|
      # https://stackoverflow.com/a/1732454
      if snippet
        snip = result["snippet"].gsub( /<.*?>/, "" ).gsub( /&\w+;/, "" ).split.join(" ")
        puts "\n'#{result["title"]}' : #{snip}..."
      else
        puts "\n'#{result["title"]}'"
      end
    end
  end
  return hash
end
get(endpoint, parse=true) click to toggle source

Do a get request and, by default, return the response as parsed json

# File lib/wik.rb, line 12
def get(endpoint, parse=true)
  ua = "Wik-RubyGem (https://github.com/wlib/wik; danielethridge@icloud.com) Command Line Wikipedia"
  body = open(endpoint, "User-Agent" => ua).read
  if parse
    return JSON.parse(body)
  else
    return body
  end
end
handle(input) click to toggle source

Automatically handle the process of searching, redirecting, and viewing

# File lib/wik.rb, line 146
def handle(input)
  search(input)
end
info(titles=nil, ids=nil, display=true) click to toggle source

Get info for titles

# File lib/wik.rb, line 78
def info(titles=nil, ids=nil, display=true)
  if titles
    if titles.is_a?(Array)
      encoded = titles.join("|").split.join("_")
    elsif titles.is_a?(String)
      encoded = titles
    else
      puts "Titles should be a string or array"
      return false
    end
    endpoint = "https://en.wikipedia.org/w/api.php?action=query&format=json&prop=info&inprop=url&titles=#{encoded}&redirects"
  elsif ids
    if ids.is_a?(Array)
      encoded = ids.join("|").split.join("_")
    elsif ids.is_a?(String)
      encoded = ids
    else
      puts "ID's should be a string or array"
      return false
    end
    endpoint = "https://en.wikipedia.org/w/api.php?action=query&format=json&prop=info&inprop=url&pageids=#{encoded}&redirects"
  end
  hash = get(endpoint)
  pages = hash["query"]["pages"]
  redirects = hash["query"]["redirects"]
  if display
    if redirects
      redirects.each do |redirect|
        puts "Redirected from '#{redirect["from"]}' to '#{redirect["to"]}'"
      end
    end
    pages.keys.each do |id|
      page = pages[id]
      if page["missing"]
        puts "The page '#{page["title"]}' is missing"
      else
        puts "The page '#{page["title"]}' has the ID: #{page["pageid"]}, and is written in #{page["pagelanguage"]}"
      end
    end
  end
  return hash
end
view(title=nil, id=nil) click to toggle source

Get the entire page of an entry

# File lib/wik.rb, line 122
def view(title=nil, id=nil)
  if title
    encoded = title.split.join("_")
    endpoint = "https://en.wikipedia.org/w/api.php?action=parse&format=json&prop=wikitext&page=#{encoded}&redirects"
  elsif id
    encoded = title.split.join("_")
    endpoint = "https://en.wikipedia.org/w/api.php?action=parse&format=json&prop=wikitext&pageid=#{encoded}"
  end
  hash = get(endpoint)
  page = hash["parse"]["wikitext"].to_s.gsub( '\n', "\n" )
  tmp = Tempfile.new("wik-#{encoded}-")
  tmp.puts page
  system "less #{tmp.path}"
  tmp.close
  tmp.unlink
  return hash
end