Sha256: f7d4c68effc44a3a5c2d58b6b4aa17fc958145b1a69b36d1132d60e388adbb10

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

module Ec2spec
  class HostResult
    MONTH_OF_DAYS = 31
    NA_VALUE = 'N/A'

    LABEL_WITH_METHODS = {
      'instance_type' => :instance_type,
      'instance_id'   => :instance_id,
      'vCPU'          => :vcpu,
      'memory'        => :memory,
      'price (USD/H)' => :price_per_unit,
      'price (USD/M)' => :price_per_month,
    }

    attr_accessor :host, :backend, :instance_id, :cpu
    attr_reader :instance_type
    attr_writer :price_per_unit

    def initialize(region, host, days = nil)
      @region = region
      @host = host
      @backend = nil
      @days = days || MONTH_OF_DAYS
    end

    def na_values
      @instance_type = NA_VALUE
      @instance_id = NA_VALUE
      @memory = NA_VALUE
      @cpu = NA_VALUE
      @price_per_unit = NA_VALUE
    end

    def instance_type=(value)
      @instance_type = value

      return if value == NA_VALUE
      price_per_unit
    end

    def vcpu
      @vcpu ||=
        Ec2spec::OfferFile.instance.vcpu(@instance_type)
    end

    def memory
      @memory ||=
        Ec2spec::OfferFile.instance.memory(@instance_type)
    end

    def price_per_unit
      @price_per_unit ||=
        Ec2spec::OfferFile.instance.price_per_unit(@instance_type)
    end

    def price_per_month
      return NA_VALUE if @price_per_unit == NA_VALUE
      @price_per_unit * 24 * @days
    end

    def to_hash
      host_values
    end

    private

    def host_values
      LABEL_WITH_METHODS.each_with_object({}) do |(k, v), hash|
        hash[k] = public_send(v)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ec2spec-0.1.2 lib/ec2spec/host_result.rb