Skip to content

Instantly share code, notes, and snippets.

@binford2k
Created January 13, 2014 18:31
Show Gist options
  • Select an option

  • Save binford2k/8405511 to your computer and use it in GitHub Desktop.

Select an option

Save binford2k/8405511 to your computer and use it in GitHub Desktop.
#! /usr/bin/env ruby
require 'rubygems'
require 'nokogiri'
require 'open-uri'
require 'optparse'
options = {}
optparse = OptionParser.new { |opts|
opts.banner = "Usage : howmuch.rb [-c <city> -s <state>] | [-z <zip>]
Simply outputs the GSA per diem rates for a given city/state or zip code.
"
options[:show]=false
opts.on("-c CITY", "--city CITY", "Set the city for your query") do |city|
options[:city] = city.gsub(' ', '%20')
end
opts.on("-s STATE", "--state STATE", "Set the state for your query") do |state|
options[:state] = state
end
opts.on("-z ZIP", "--zip ZIP", "Set the zip code for your query") do |zip|
options[:zip] = zip
end
opts.separator('')
opts.on("-h", "--help", "Displays this help") do
puts opts
exit
end
}
begin
optparse.parse!
# Get a Nokogiri::HTML::Document for the page we’re interested in...
doc = Nokogiri::HTML(open("http://www.gsa.gov/portal/category/100120?perdiemSearchVO.year=2013&resultName=getPerdiemRatesBySearchVO&currentCategory.categoryId=100120&perdiemSearchVO.city=#{options[:city]}&perdiemSearchVO.state=#{options[:state]}&perdiemSearchVO.zip=#{options[:zip]}"))
# Do funky things with it using Nokogiri::XML::Node methods...
####
# Search for nodes by css
doc.css('table#perdiem_table tr').each do |tr|
if not (row = tr.css('td')).empty? then
city = row[0].content
county = row[1].content
mie = row[14].content
puts "#{city}/#{county}: #{mie}"
end
end
rescue Exception => e
puts e
end
#http://www.gsa.gov/portal/category/100120?perdiemSearchVO.year=2013&resultName=getPerdiemRatesBySearchVO&currentCategory.categoryId=100120&perdiemSearchVO.city=san%20francisco&perdiemSearchVO.state=California
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment