Last active
December 17, 2019 18:03
-
-
Save letsbreelhere/384c640fe1677e76a3a0de73076abe95 to your computer and use it in GitHub Desktop.
Hoogle Alfred flow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'nokogiri' | |
require 'json' | |
require 'net/http' | |
require 'cgi' | |
def sanitize(text) | |
CGI.unescapeHTML(Nokogiri::HTML(text).xpath('//text()').to_s) | |
end | |
query_str = URI.escape("{query}") | |
query_uri = URI("https://hoogle.haskell.org?mode=json&hoogle=#{query_str}") | |
res = Net::HTTP.get_response(query_uri) | |
builder = Nokogiri::XML::Builder.new do |x| | |
x.items do | |
if res.is_a?(Net::HTTPSuccess) | |
search_results = JSON.parse(res.body) | |
if search_results.empty? | |
x.item do | |
x.title 'No results' | |
x.valid 'no' | |
end | |
break | |
end | |
search_results.each do |result| | |
x.item do | |
x.title sanitize(result['item']) | |
if result['type'] != 'package' | |
x.subtitle "#{result['package']['name']}: #{sanitize(result['docs'])}" | |
else | |
x.subtitle sanitize(result['docs']) | |
end | |
x.arg result['url'] | |
end | |
end | |
else | |
x.item do | |
x.title 'Error' | |
x.subtitle 'Search query returned an error.' | |
x.valid 'no' | |
end | |
end | |
end | |
end | |
puts builder.to_xml |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment