lib/malkist/distances.rb
# calculate_distance will hit Distance Matrix API and wrap it inside class Distance.
calculate_distance will hit Distance Matrix API and wrap it inside class Distance.
calculate_distance will hit Distance Matrix API and wrap it inside class Distance .
def calculate_distance
      uri = URI('https://maps.googleapis.com/maps/api/distancematrix/json')
      params = { origins: @origins.join('|'), destinations: @destinations.join('|') }
      uri.query = URI.encode_www_form(params)
      res = Net::HTTP.get_response(uri)
      return [] unless res.is_a?(Net::HTTPSuccess)
      body = JSON.parse(res.body)
      result = []
      body['rows'].each_with_index do |row, row_index|
        row['elements'].each_with_index do |element, index|
          dist = Distance.new(
            body['origin_addresses'][row_index],
            body['destination_addresses'][index],
            element['distance']['value'],
            element['duration']['value']
          )
          result.push(dist)
        end
      end
      result
    end
def calculate_distance uri = URI ( 'https://maps.googleapis.com/maps/api/distancematrix/json' ) params = { origins : @origins . join ( '|' ) , destinations : @destinations . join ( '|' ) } uri . query = URI . encode_www_form ( params ) res = Net :: HTTP . get_response ( uri ) return [ ] unless res . is_a? ( Net :: HTTPSuccess ) body = JSON . parse ( res . body ) result = [ ] body [ 'rows' ] . each_with_index do | row , row_index | row [ 'elements' ] . each_with_index do | element , index | dist = Distance . new ( body [ 'origin_addresses' ] [ row_index ] , body [ 'destination_addresses' ] [ index ] , element [ 'distance' ] [ 'value' ] , element [ 'duration' ] [ 'value' ] ) result . push ( dist ) end end result end
calculate_distance will hit Distance Matrix API and wrap it inside class Distance.
calculate_distance will hit Distance Matrix API and wrap it inside class Distance .
# puts uri