class Data_Load

Attributes

coordinateArrayFromFile[RW]
coordinateArrayFromServer[RW]
postcodeArrayFromFile[RW]

Public Class Methods

new() click to toggle source
# File lib/test/data_load.rb, line 6
def initialize
        # Will there be an instance where @postcodeArrayFromFile is nil?
        if @postcodeArrayFromFile.nil?
                        @postcodeArrayFromFile = []
        end

        if @coordinateArrayFromServer.nil?
                @coordinateArrayFromServer = []
        end

        if @coordinateArrayFromFile.nil?
                @coordinateArrayFromFile = []
        end
end

Public Instance Methods

getCoordinateFromFile(convertToNumbers = nil) click to toggle source
# File lib/test/data_load.rb, line 73
def getCoordinateFromFile(convertToNumbers = nil)
        puts "Getting coordinates from file"
        if convertToNumbers.nil?
                convertToNumbers = false
        end

        File.open("lib/test/sample_data/coordinates", "r") do |file|
                while line = file.gets
                        coordinate = line.strip.split("-")
                        if convertToNumbers == true
                                if coordinate[0].is_number? && coordinate[1].is_number?
                                        coordinate[0] = coordinate[0].to_f
                                        coordinate[1] = coordinate[1].to_f
                                else
                                        raise "Error! #{coordinate[0]} & #{coordinate[1]} is not a number!"
                                end
                        end
                        @coordinateArrayFromFile.push(coordinate)
                end
        end

        puts "Done getting coordinates from file"
        puts
end
getCoordinateFromMapSynq() click to toggle source
# File lib/test/data_load.rb, line 21
def getCoordinateFromMapSynq
        puts "Getting postcodes from file..."

        getPostcodeFromFile()

        print "Requesting coordinates from server..."

        @postcodeArrayFromFile.each do |postcode|
                print "."

                mapsynqUrl = "http://www.mapsynq.com/home/search1?q=#{postcode}&lat=&lon=&page=1"

                begin
                        url = URI.parse(mapsynqUrl)
                        req = Net::HTTP::Get.new(url.to_s)
                        res = Net::HTTP.start(url.host, url.port) {|http|
                          http.request(req)
                        }

                        result = JSON.parse(res.body)["result"]
                        
                        unless result.empty?
                                yCoordinate = result[0]["true_latitude"]
                                xCoordinate = result[0]["true_longitude"]
                                coordinate = "#{xCoordinate}-#{yCoordinate}"

                                coordinateArrayFromServer.push(coordinate)
                        end
                rescue
                        puts $!, $@
                        puts
                        next
                end
        end
        print "\n"
        puts "Done requesting coordinates from server"
        puts
end
getPostcodeFromFile() click to toggle source
# File lib/test/data_load.rb, line 60
def getPostcodeFromFile
        puts "Getting postcodes from file"

        File.open("lib/test/sample_data/postcodes", "r") do |file|
                while line = file.gets
                        @postcodeArrayFromFile.push(line.strip)
                end
        end

        puts "Done getting postcodes from file"
        puts
end
writeCoordinateToFile() click to toggle source
# File lib/test/data_load.rb, line 98
def writeCoordinateToFile
        puts "Writing coordinates to file"

        File.open("lib/test/sample_data/coordinates", "w") do |file|
                @coordinateArrayFromServer.each do |coordinate|
                        file.puts coordinate
                end
        end

        puts "Done writing coordinates to file"
        puts
end