Module: Wik

Defined in:
lib/wik.rb,
lib/wik/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Instance Method Summary collapse

Instance Method Details

#describe(title = nil, id = nil) ⇒ Object

Just give a description of an entry



141
142
143
# File 'lib/wik.rb', line 141

def describe(title=nil, id=nil)
  puts "In development..."
end

#find(phrase, limit = 15, snippet = false, display = true) ⇒ Object

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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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) ⇒ Object

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



12
13
14
15
16
17
18
19
20
# File 'lib/wik.rb', line 12

def get(endpoint, parse=true)
  ua = "Wik-RubyGem (https://github.com/wlib/wik; [email protected]) Command Line Wikipedia"
  body = open(endpoint, "User-Agent" => ua).read
  if parse
    return JSON.parse(body)
  else
    return body
  end
end

#handle(input) ⇒ Object

Automatically handle the process of searching, redirecting, and viewing



146
147
148
# File 'lib/wik.rb', line 146

def handle(input)
  search(input)
end

#info(titles = nil, ids = nil, display = true) ⇒ Object

Get info for titles



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# 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

#search(phrase, limit = 5, description = true, display = true) ⇒ Object

Open Search is a different way to search Wikipedia It returns descriptions automatically and has a more consistent typo-correct



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/wik.rb', line 54

def search(phrase, limit=5, description=true, display=true)
  search = phrase.split.join("_")
  endpoint = "https://en.wikipedia.org/w/api.php?action=opensearch&format=json&search=#{search}&limit=#{limit}"
  hash = get(endpoint)
  results = hash[1]
  descriptions = hash[2]
  if display
    puts "Displaying #{results.length} results:"
    for i in 0...results.length
      if description
        if ! descriptions[i].empty?
          puts "\n'#{results[i]}' : #{descriptions[i]}"
        else
          puts "\n'#{results[i]}' : No Immediate Description Available. Try `wik -d #{results[i]}`"
        end
      else
        puts "\n'#{results[i]}'"
      end
    end
  end
  return hash
end

#view(title = nil, id = nil) ⇒ Object

Get the entire page of an entry



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# 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