Skip to content

Instantly share code, notes, and snippets.

@jheidt
Created August 28, 2013 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jheidt/1699fc0f753d4d3ddb2a to your computer and use it in GitHub Desktop.
Save jheidt/1699fc0f753d4d3ddb2a to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'uri'
require 'yajl'
require 'json'
require 'json-schema'
require 'net/http'
require 'digest/sha1'
s3_data_transfer_schema = {
"type" => "object",
"required" => ["vers","config"],
"properties" => {
"vers" => { "type" => "number" },
"config" => {
"type" => "object",
"required" => ["currencies","rate","regions"],
"properties" => {
"currencies" => { "type" => "array", "items" => { "type" => "string" } },
"rate" => { "type" => "string" },
"regions" => {
"type" => "array",
"items" => {
"type" => "object",
"properties" => {
"region" => { "type" => "string" },
"types" => {
"type" => "array", "items" => {
"type" => "object",
"properties" => {
"region" => { "type" => "string" }
}
}
}
}
}
}
}
}
}
}
pricing_json = {
:s3 => {
:storage => {
:url => 'http://aws.amazon.com/s3/pricing/pricing-storage.json',
:schema => nil,
:title => 'S3 Storage'
},
:data_transfer => {
:url => 'http://aws.amazon.com/s3/pricing/pricing-data-transfer.json',
:schema => nil,
:title => 'S3 Data Transfer'
},
:requests => {
:url => 'http://aws.amazon.com/s3/pricing/pricing-requests.json',
:schema => nil,
:title => 'S3 Requests'
}
},
:ec2 => {
},
:rds => {
}
}
pricing_values = {}
pricing_json.each_with_object(pricing_values) do |service,rv|
service_sym = service[0]
service_sections = service[1]
rv[service_sym] ||= {}
service_sections.each do |sect|
section_sym = sect[0]
section_url = sect[1][:url]
section_schema = sect[1][:schema]
request_content = Net::HTTP.get(URI.parse(section_url))
rv[service_sym][section_sym] = {
:sha1 => Digest::SHA1.hexdigest(request_content),
:json => JSON.parse(request_content),
:length => request_content.length
}
end
end
valid = JSON::Validator.validate(s3_data_transfer_schema, pricing_values[:s3][:data_transfer][:json])
puts "#{ pricing_json[:s3][:data_transfer][:title] } JSON"
puts "\t-content length: #{ pricing_values[:s3][:data_transfer][:length] }"
puts "\t- SHA-1: #{ pricing_values[:s3][:data_transfer][:sha1] }"
puts "\t- version value: #{ pricing_values[:s3][:data_transfer][:json]['vers'] }"
puts "\t- schema valid?: #{ valid }"
puts ""
###################################################################
# Output
###################################################################
#
# S3 Data Transfer JSON
# -content length: 26543
# - SHA-1: fea25c963bebaa229636f4edc57a2b716a074249
# - version value: 0.01
# - schema valid?: true
#
# [Finished in 0.3s]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment