lib/lookup.rb in lookup-1.1.1 vs lib/lookup.rb in lookup-1.1.3
- old
+ new
@@ -17,11 +17,11 @@
end
module Lookup
APIS = []
- class APINotFound < StandardError; end
+ class APINotFound < Exception; end
class << self
def home
Pathname.new(ENV["HOME"]) + ".lookup"
@@ -132,28 +132,30 @@
end
methods
end
def search(msg, options={})
- options[:api] ||= msg.split.first
+ first_word = msg.split.first
+ options[:api] ||= first_word if apis.keys.include?(first_word)
api_check = lambda { |options| (!apis.keys.map(&:to_s).include?(options[:api]) && !options[:api].is_a?(Api)) }
# to_s because yaml interprets "1.8" as a literal 1.8
# And because I'm super, super lazy
- if !@attempted_ruby && api_check.call(options)
+ if api_check.call(options)
# Attempt a current Ruby lookup
- @attempted_ruby = true
api = case RUBY_VERSION
when /^1.8/
"1.8"
when /^1.9/
"1.9"
end
search(msg, options.merge!(:api => api)) if api
end
- options[:api] = Api.find_by_name!(apis[options[:api]]["name"]) unless options[:api].is_a?(Api)
+ options[:api] = Api.find_by_name!(apis[options[:api]]["name"]) if (options[:api] && apis[options[:api]].try(:[], "name")) && !options[:api].is_a?(Api)
+ raise Lookup::APINotFound, "Could not determine what API the lookup is for" unless options[:api]
+
# We want to retain message.
msg = msg.gsub(/^(.*?)\s/, "")
splitter = options[:splitter] || "#"
parts = msg.split(" ")[0..-1].flatten.map { |a| a.split(splitter) }.flatten!
@@ -191,6 +193,6 @@
end
end
end
-require 'lookup/models'
+require File.expand_path('../lookup/models', __FILE__)