Created
February 26, 2014 08:42
-
-
Save shigeya/9225918 to your computer and use it in GitHub Desktop.
Minimum code for EPCIS repository simple query interface
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
#!/usr/bin/env ruby | |
require 'nokogiri' | |
require 'optparse' | |
require 'net/http' | |
require 'uri' | |
@opts = { query_url: "http://127.0.0.1:3000/epcis/query", | |
query_type: "SimpleEventQuery" } | |
ARGV.options do |o| | |
o.banner = "#{$0} [options] [type:param]" | |
o.separator "Options:" | |
o.on("-Q", "--query_url url", "Destination URL for query") {|url| @opts[:query_url] = url } | |
o.parse! | |
end | |
query_params = {} | |
ARGV.each {|a| query_params[$1] = $2 if a =~ /^([^:]+):(.*)/ } | |
def body_build(xml, query_type, query_params) | |
xml.queryName query_type | |
xml.params { | |
query_params.map do |type, value| | |
xml.param { | |
xml.name type | |
xml.value value | |
} | |
end | |
} | |
end | |
QUERY_NAMESPACES = { | |
'xmlns:epcisq' => 'urn:epcglobal:epcis-query:xsd:1' | |
} | |
builder = Nokogiri::XML::Builder.new do |xml| | |
xml['epcisq'].Poll(QUERY_NAMESPACES) { | |
body_build(xml, @opts[:query_type], query_params) | |
} | |
end | |
body = builder.to_xml | |
uri = URI.parse(@opts[:query_url]) | |
http = Net::HTTP.new(uri.host, uri.port) | |
request = Net::HTTP::Post.new(uri.request_uri) | |
request["Content-Type"] = 'text/xml' | |
request["Accept"] = '*/*' | |
request.body = body | |
response = http.request(request) | |
if response.code.to_i == 200 | |
puts response.body | |
else | |
puts response.code | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment