Sha256: 94bcfad321bb5ad2c9d7171328137617f09ad1a62aa38ee35b2a280fff247be0

Contents?: true

Size: 1.66 KB

Versions: 1

Compression:

Stored size: 1.66 KB

Contents

module DistanceFinder
  class Calculator

    attr_reader :distance, :duration, :start_address, :end_address, :display_full_response

    def initialize(origin, destination)
      @origin = origin.strip().tr(" ", "+")
      @destination = destination.strip().tr(" ", "+")
      @google_object = parse_response
    end


    # Returns response
    def status
      @google_object["status"]
    end

    # Return distance
    def distance
      @google_object["routes"][0]["legs"][0]["distance"]["value"] / 1000 * 0.621371
    end

    # Return duration
    def duration
      @google_object["routes"][0]["legs"][0]["duration"]["text"]
    end

    # Return origin name
    def start_address
      @google_object["routes"][0]["legs"][0]["start_address"]
    end

    # Returns destination name
    def end_address
      @google_object["routes"][0]["legs"][0]["end_address"]
    end

    # Generate pretty JSON for command line output
    def full_response
      puts JSON.pretty_generate(@google_object)
    end

    def parse_response
      if get_response.kind_of? Net::HTTPSuccess
        JSON.parse(get_response.body)
      elsif !get_response
        JSON.parse('{"status" : "No internet connection!"}')
      else
        JSON.parse('{"status" : "Google API unaviable!"}')
      end
    end

    # Checks for internet connection and gets response from Google API
    def get_response
      begin
        TCPSocket.new 'google.com', 80
        Net::HTTP.get_response(URI(build_url))
      rescue SocketError
        return false
      end
    end

    def build_url
      "https://maps.googleapis.com/maps/api/directions/json?origin=#{@origin}&destination=#{@destination}"
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
distance_finder-1.0.2 lib/distance_finder/calculator.rb