Skip to content

Instantly share code, notes, and snippets.

@houshuang
Created April 17, 2012 17:30
Show Gist options
  • Save houshuang/2407649 to your computer and use it in GitHub Desktop.
Save houshuang/2407649 to your computer and use it in GitHub Desktop.
Quick API to check if a URL has downloadable PDF - live at http://reganmian.net/check-oa/URL
#!/usr/bin/ruby
require 'net/http'
require "cgi"
# this runs on my server, and is used to check if a certain url links to a publicly available PDF
def checkOA(url)
url = url.gsub("http:/",'').gsub("http://",'')
uri, *path = url.split("/")
path = "/" + path.join("/")
response = nil
Net::HTTP.start(uri, 80) { |http| response = http.head(path) }
possible_ctypes = [
"application/pdf",
"application/x-pdf",
"application/vnd.pdf",
"application/text.pdf"]
return possible_ctypes.index( response['content-type'] )
end
arg = CGI.new['redir'].to_s
puts "Content-Type: text/html; charset=utf-8\n\n"
puts checkOA(arg) ? 'true' : 'false'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment