Skip to content

FrozenError (can't modify frozen String: ""): distance_to #14

Description

@rmcsharry

First of all thanks for making an awesome gem. It was so simple to get it working.

My code was working fine, but recently i started getting a frozen string error.

This is calling the distance matrix api. Implemented as a concern on a model called 'Address':

The error is thrown when calling the query:

Address.distance.query(origins: origin, destinations: dest, mode: 'driving', units: units)

Full concern code is this:

require 'googlemaps/services/client'
require 'googlemaps/services/distancematrix'

module Mappable
  extend ActiveSupport::Concern
  include GoogleMaps::Services

  # https://maps.googleapis.com/maps/api/distancematrix/json?&key=

  included do
    client = GoogleClient.new(key: Rails.application.secrets.google_api_key, response_format: :json)
    self.distance = DistanceMatrix.new(client) # creates a class instance variable (so this will be a singleton)
  end
  
  module ClassMethods
    attr_accessor :distance
  end

  def distance_to(destination, units)
    dest = []
    origin = []
    dest << {lat:"#{destination.latitude}", lng:"#{destination.longitude}"}
    origin << {lat:"#{self.latitude}", lng:"#{self.longitude}"}
    result = Address.distance.query(origins: origin, destinations: dest, mode: 'driving', units: units)
    puts 'result is ****', result
    return result['rows'][0]['elements'][0]['distance']
  end

end

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions