Sha256: 454bdb62ff0d88a422a1c243932dde18a67cb4dc7be275c4124ce8a263b9dfe9

Contents?: true

Size: 1.59 KB

Versions: 11

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true

module GoogleDistanceMatrix
  # Public: Thin wrapper class for an element in the matrix.
  #
  # The route has the data the element contains, pluss it references
  # it's origin and destination.
  #
  class Route
    STATUSES = %w[ok zero_results not_found].freeze

    ATTRIBUTES = %w[
      origin destination
      status
      distance_text distance_in_meters
      duration_text duration_in_seconds
      duration_in_traffic_text duration_in_traffic_in_seconds
    ].freeze

    attr_reader(*ATTRIBUTES)

    delegate(*(STATUSES.map { |s| s + '?' }), to: :status, allow_nil: true)

    def initialize(attributes = {})
      attributes = attributes.with_indifferent_access

      @origin = attributes[:origin]
      @destination = attributes[:destination]

      @status = ActiveSupport::StringInquirer.new attributes[:status].downcase

      assign attributes if ok?
    end

    def inspect
      attrs = ATTRIBUTES.reject do |a|
        public_send(a).blank?
      end

      inspection = attrs.map { |a| "#{a}: #{public_send(a).inspect}" }.join ', '

      "#<#{self.class} #{inspection}>"
    end

    private

    def assign(attributes)
      @distance_text = attributes[:distance][:text]
      @distance_in_meters = attributes[:distance][:value]
      @duration_text = attributes[:duration][:text]
      @duration_in_seconds = attributes[:duration][:value]

      return unless  attributes.key? :duration_in_traffic

      @duration_in_traffic_text = attributes[:duration_in_traffic][:text]
      @duration_in_traffic_in_seconds = attributes[:duration_in_traffic][:value]
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
google_distance_matrix-0.7.1 lib/google_distance_matrix/route.rb
google_distance_matrix-0.7.0 lib/google_distance_matrix/route.rb
google_distance_matrix-0.6.7 lib/google_distance_matrix/route.rb
google_distance_matrix-0.6.6 lib/google_distance_matrix/route.rb
google_distance_matrix-0.6.5 lib/google_distance_matrix/route.rb
google_distance_matrix-0.6.4 lib/google_distance_matrix/route.rb
google_distance_matrix-0.6.3 lib/google_distance_matrix/route.rb
google_distance_matrix-0.6.2 lib/google_distance_matrix/route.rb
google_distance_matrix-0.6.1 lib/google_distance_matrix/route.rb
google_distance_matrix-0.6.0 lib/google_distance_matrix/route.rb
google_distance_matrix-0.5.0 lib/google_distance_matrix/route.rb