1. open markdown document
2. select a word, say “multivariate”
2. Select Lookup Word / Selection on Wikipedia and link

EXPECTED: link to wikipedia wrapped about the selection
OBTAINED: Command error


/Users/tim/Library/Application Support/TextMate/Ruby/1.8.7/lib/ruby/1.8/net/http.rb:586:in `connect': SSL_connect returned=1 errno=0 state=SSLv2/v3 read server hello A: tlsv1 alert protocol version (OpenSSL::SSL::SSLError)
from /Users/tim/Library/Application Support/TextMate/Ruby/1.8.7/lib/ruby/1.8/net/http.rb:586:in `connect'
from /Users/tim/Library/Application Support/TextMate/Ruby/1.8.7/lib/ruby/1.8/net/http.rb:553:in `do_start'
from /Users/tim/Library/Application Support/TextMate/Ruby/1.8.7/lib/ruby/1.8/net/http.rb:542:in `start'
from /Users/tim/Library/Application Support/TextMate/Ruby/1.8.7/lib/ruby/1.8/open-uri.rb:242:in `open_http'
from /Users/tim/Library/Application Support/TextMate/Ruby/1.8.7/lib/ruby/1.8/open-uri.rb:616:in `buffer_open'
from /Users/tim/Library/Application Support/TextMate/Ruby/1.8.7/lib/ruby/1.8/open-uri.rb:164:in `open_loop'
from /Users/tim/Library/Application Support/TextMate/Ruby/1.8.7/lib/ruby/1.8/open-uri.rb:162:in `catch'
from /Users/tim/Library/Application Support/TextMate/Ruby/1.8.7/lib/ruby/1.8/open-uri.rb:162:in `open_loop'
from /Users/tim/Library/Application Support/TextMate/Ruby/1.8.7/lib/ruby/1.8/open-uri.rb:132:in `open_uri'
from /Users/tim/Library/Application Support/TextMate/Ruby/1.8.7/lib/ruby/1.8/open-uri.rb:518:in `open'
from /Users/tim/Library/Application Support/TextMate/Ruby/1.8.7/lib/ruby/1.8/open-uri.rb:30:in `open'
from Lookup Selection on Wikipedia and link:19:in `getWikiEntries'
from Lookup Selection on Wikipedia and link:31:in `getWikiDef'
from Lookup Selection on Wikipedia and link:37



The command is:

#!/usr/bin/env ruby18
require "#{ENV['TM_SUPPORT_PATH']}/lib/progress.rb"
require "#{ENV['TM_SUPPORT_PATH']}/lib/exit_codes.rb"
require "#{ENV['TM_SUPPORT_PATH']}/lib/ui.rb"
require 'cgi'
require 'open-uri'
require 'erb'
require 'rexml/document'

def select_link(links)
  linklist = links.map { |p| p['Text'] }
  item = TextMate::UI.request_item :title => "Multiple Definitions", :prompt => "Please select a definition", :items => linklist
  TextMate.exit_discard if item.nil?
  return links[linklist.index(item)]
end # select_link

def getWikiEntries(phrase)
  res = []
  response = open("https://en.wikipedia.org/w/api.php?format=xml&action=opensearch&search=#{CGI.escape(phrase)}", "User-Agent" => "TextMate 2.0")
  doc = REXML::Document.new(response.read)

  doc.elements.each('SearchSuggestion/Section/Item') do |item|
    hash = { }
    %w[ Text Url Description ].each { |tag| item.elements.each(tag) { |inner| hash[tag] = inner.text } }
    res << hash
  end
  res.sort { |a, b| a['Text'].downcase <=> b['Text'].downcase }
end

def getWikiDef(phrase)
  entries = getWikiEntries(phrase)
  TextMate.exit_show_tool_tip "No definition found" if entries.empty?
  entry = entries.size > 1 ? select_link(entries) : entries.first
  return entry['Url'], "Wikipedia Entry: #{entry['Text']}", phrase
end

url, title, input = getWikiDef(STDIN.read)
print ERB.new(ENV['TM_LINK_FORMAT']).result