Sha256: aa5fda7bab23fcf13f7e96e729eaafc54b67ab80a4cfa6da7dfff229fce6ec42
Contents?: true
Size: 1.7 KB
Versions: 1
Compression:
Stored size: 1.7 KB
Contents
require 'json' require 'faraday' module Ec2spec class OfferFile OFFER_FILE_URL = 'https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/AmazonEC2/20180802021534/ap-northeast-1/index.json' OFFER_FILE_NAME = 'price_list.json' OFFER_FILE_DIR = '.ec2spec' def initialize(instance_type) @instance_type = instance_type @offer_file_json = nil end def price_per_unit sku_instance_type = sku on_demand = offer_file_json['terms']['OnDemand'] sku_value = on_demand[sku_instance_type].first[1] price_dimensions = sku_value['priceDimensions'] price_dimensions.first[1]['pricePerUnit']['USD'].to_f end private def offer_file_json download unless File.exist?(offer_file_path) @offer_file_json ||= JSON.parse(File.open(offer_file_path).read) end def offer_file_path price_list_dir = File.join(ENV['HOME'], OFFER_FILE_DIR) Dir.mkdir(price_list_dir) unless Dir.exist?(price_list_dir) File.join(price_list_dir, OFFER_FILE_NAME) end def download http_conn = Faraday.new do |builder| builder.adapter Faraday.default_adapter end response = http_conn.get(OFFER_FILE_URL) File.open(offer_file_path, 'wb') { |fp| fp.write(response.body) } end def sku products = offer_file_json['products'] target_product = products.find do |product| product?(product, @instance_type) end target_product[1]['sku'] end def product?(product, instance_type) attributes = product[1]['attributes'] attributes['instanceType'] == instance_type && attributes['operatingSystem'] == 'Linux' && attributes['preInstalledSw'] == 'NA' end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
ec2spec-0.1.1 | lib/ec2spec/offer_file.rb |