class CoureonBindings::Rates
  attr_reader :raw_rates, :shipment_params, :rates

  def initialize(content, client, shipment_params)
    @raw_rates = content['content']['rates']
    @client = client
    @shipment_params = shipment_params

    @rates = []
    @raw_rates.each do |rate|
      @rates << CoureonBindings::Rate.new(rate, client, shipment_params)
    end
  end

  def get_cheapest_rates
    min_filter_by(:amount)
  end

  def get_fastest_rates
    min_filter_by(:days_max)
  end

  def min_filter_by(key)
    sorted = @rates.sort_by { |rate| rate[key.to_s] }
    sorted.keep_if { |rate| rate[key.to_s] == sorted.first[key.to_s] }
  end
end