-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
188 additions
and
16 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,103 @@ | ||
=begin | ||
-------------------------------------------------------------------------------- | ||
execute LISTRDF requests, like this: | ||
http://localhost:8080/vivo/listrdf?vclass=http%3A%2F%2Fvivoweb.org%2Fontology%2Fcore%23FacultyMember | ||
write the results to the file, one URI per line. | ||
-------------------------------------------------------------------------------- | ||
=end | ||
|
||
class CmdPrepareUriList | ||
USAGE = 'prepare uri-list [class_list_file] [VIVO_homepage_URL] {uri_list_file {REPLACE}}' | ||
def initialize(args) | ||
bogus "CmdPrepareUriList.initialize NOT IMPLEMENTED" | ||
@args = args | ||
complain("usage: #{USAGE}") unless (2..4).include? args.size | ||
|
||
@class_list_file = args[0] | ||
complain("'#{@class_list_file}' does not exist.") unless File.exist?(@class_list_file) | ||
|
||
@vivo_home_url = args[1] | ||
begin | ||
HttpRequest.new(@vivo_home_url).exec do |response| | ||
complain("Response from '#{@vivo_home_url}' does not look like VIVO's home page.") unless response.body =~ /body class="home"/ | ||
end | ||
rescue | ||
puts "#{$!}\n#{$!.backtrace.join("\n")}" | ||
complain("Can't contact VIVO at '#{@vivo_home_url}': #{$!}") | ||
end | ||
|
||
@uri_list_file = args[2] | ||
|
||
if @uri_list_file | ||
complain("'#{@uri_list_file}' already exists. Specify REPLACE to replace it.") if File.exist?(@uri_list_file) unless 'REPLACE' == args[3] | ||
@output = File.open(@uri_list_file, 'w') | ||
else | ||
@output = $stdout | ||
end | ||
end | ||
|
||
def run() | ||
bogus "CmdPrepareUriList.run NOT IMPLEMENTED" | ||
write_heading | ||
@stats = [] | ||
|
||
File.open(@class_list_file) do |f| | ||
f.each_line do |line| | ||
line.strip! | ||
perform_listrdf(line) unless line.empty? || line.start_with?('#') | ||
end | ||
end | ||
|
||
write_report | ||
end | ||
|
||
def perform_listrdf(class_uri) | ||
HttpRequest.new(listrdf_url).parameters('vclass' => class_uri).headers('Accept' => 'text/plain').exec do |response| | ||
process_response(class_uri, response.body.lines) | ||
end | ||
end | ||
|
||
def process_response(class_uri, lines) | ||
@stats << [class_uri, lines.size] | ||
@output.puts | ||
@output.puts "# class = #{class_uri}" | ||
@output.puts | ||
|
||
lines.each do |line| | ||
if line =~ /<([^>]+)>/ | ||
@output.puts $1 | ||
else | ||
warning("Unexpected line in response '#{line}'") | ||
end | ||
end | ||
end | ||
|
||
def listrdf_url() | ||
if @vivo_home_url.end_with?('/') | ||
@vivo_home_url + 'listrdf' | ||
else | ||
@vivo_home_url + '/listrdf' | ||
end | ||
end | ||
|
||
def write_heading() | ||
@output.puts "#" | ||
@output.puts "# prepare uri-list #{@args.join(' ')}" | ||
@output.puts "# #{Time.now.strftime('%Y-%m-%d %H:%M:%S')}" | ||
@output.puts "#" | ||
end | ||
|
||
def write_report() | ||
width = @stats.map { |s| s[0].size }.max | ||
|
||
report = "\n" | ||
@stats.each do |s| | ||
report += "# %-#{width}s %5d \n" % s | ||
end | ||
report += "# %-#{width}s %5d \n" % ['TOTAL', @stats.map{|s| s[1]}.inject(:+)] | ||
|
||
@output.print(report) | ||
print(report) if @output != $stdout | ||
end | ||
end |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
require 'net/http' | ||
require 'uri' | ||
require 'cgi' | ||
|
||
class HttpRequest | ||
def initialize(url, method = :GET) | ||
@url = url | ||
@method = method | ||
@headers = {} | ||
@parameters = {} | ||
end | ||
|
||
def headers(heads) | ||
@headers = heads | ||
self | ||
end | ||
|
||
def parameters(params) | ||
@parameters = params | ||
self | ||
end | ||
|
||
def exec() | ||
uri = URI.parse(@url) | ||
|
||
Net::HTTP.start(uri.host, uri.port) do |http| | ||
if @method == :GET | ||
full_url = add_parameters(uri.request_uri) | ||
request = Net::HTTP::Get.new(full_url) | ||
elsif @method == :POST | ||
request = Net::HTTP::Post.new(uri.request_uri) | ||
request.set_form_data(@parameters) | ||
else | ||
raise 'Method must be :GET or :POST' | ||
end | ||
|
||
@headers.each_pair { |k, v| request[k] = v } | ||
|
||
begin | ||
http.request(request) do |response| | ||
response.value | ||
yield response if block_given? | ||
end | ||
rescue Net::HTTPServerException => e | ||
raise e.exception(e.message << "\nProblem request: \n#{inspect_request(request, @url)}") | ||
rescue IOError => e | ||
raise e.exception(e.message << "\nProblem request: \n#{inspect_request(request, @url)}") | ||
end | ||
end | ||
end | ||
|
||
def add_parameters(request_uri) | ||
if @parameters.empty? | ||
request_uri | ||
elsif request_uri.include?("?") | ||
request_uri + "&" + URI.encode_www_form(@parameters) | ||
else | ||
request_uri + "?" + URI.encode_www_form(@parameters) | ||
end | ||
|
||
end | ||
|
||
def inspect_request(r, url) | ||
headers = r.to_hash.to_a.map{|h| " #{h[0]} ==> #{h[1]}"}.join("\n") | ||
body = body ? CGI.unescape(r.body) : 'NO BODY' | ||
"#{r.method} #{url}\n#{headers}\n#{body}" | ||
end | ||
|
||
end |
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