Class: Data_Load

Inherits:
Object
  • Object
show all
Defined in:
lib/test/data_load.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeData_Load

Returns a new instance of Data_Load.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 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

Instance Attribute Details

#coordinateArrayFromFileObject

Returns the value of attribute coordinateArrayFromFile.



4
5
6
# File 'lib/test/data_load.rb', line 4

def coordinateArrayFromFile
  @coordinateArrayFromFile
end

#coordinateArrayFromServerObject

Returns the value of attribute coordinateArrayFromServer.



4
5
6
# File 'lib/test/data_load.rb', line 4

def coordinateArrayFromServer
  @coordinateArrayFromServer
end

#postcodeArrayFromFileObject

Returns the value of attribute postcodeArrayFromFile.



4
5
6
# File 'lib/test/data_load.rb', line 4

def postcodeArrayFromFile
  @postcodeArrayFromFile
end

Instance Method Details

#getCoordinateFromFile(convertToNumbers = nil) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# 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

#getCoordinateFromMapSynqObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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

#getPostcodeFromFileObject



60
61
62
63
64
65
66
67
68
69
70
71
# 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

#writeCoordinateToFileObject



98
99
100
101
102
103
104
105
106
107
108
109
# 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