Module: Helper

Included in:
Dubliner
Defined in:
lib/dubliner/helper.rb

Instance Method Summary collapse

Instance Method Details

#distanceCalculator(la1, lo1, la2, lo2) ⇒ Object

input : latitude, longitude of point 1 in rads, latitude, longitude of point 2 in rads output : distance between the two points in kms



35
36
37
38
39
40
41
42
43
44
# File 'lib/dubliner/helper.rb', line 35

def distanceCalculator(la1, lo1, la2, lo2)
  return 0 if la1 == nil || la2 == nil || lo1 == nil || lo2 == nil
  delLambda = (lo2-lo1).to_f
  numerator = Math.sqrt(((Math.cos(la2) * Math.sin(delLambda)) ** 2) +
              ((Math.cos(la1) * Math.sin(la2) - Math.sin(la1) * Math.cos(la2) * Math.cos(delLambda)) ** 2))
  denominator = Math.sin(la1) * Math.sin(la2) + Math.cos(la1) * Math.cos(la2) * Math.cos(delLambda)
  denominator = 0.0000000000000000000001 if denominator == 0.0
  delta = Math.atan(numerator/denominator)
  earthRadius * delta
end

#earthRadiusObject

output : radius of earth in kms



47
48
49
# File 'lib/dubliner/helper.rb', line 47

def earthRadius
  6371.0
end

#fileRead(filename) ⇒ Object

input : filename output : a valid file handle for the filename

Raises:

  • (ArgumentError)


8
9
10
11
12
13
# File 'lib/dubliner/helper.rb', line 8

def fileRead(filename)
  raise ArgumentError.new("File #{filename} does not exist") unless File.exist?(filename)
  raise ArgumentError.new("File #{filename} is not readable. Verify access permissions of the file") unless File
                                                                                                            .readable? (filename)
  File.new(filename)
end

#jsonParse(f) ⇒ Object

input : valid file handle output : array of customer objects



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/dubliner/helper.rb', line 17

def jsonParse(f)
  customers = []
  malformed_line = ''
  begin
    f.each_line do |line|           # read line by line to avoid exhausting memory with large inputs
      malformed_line = line
      data = JSON.parse(line)
      c = Customer.new(data['user_id'], data['name'], data['latitude'], data['longitude'])
      customers.push(c)
    end
  rescue JSON::ParserError
    raise "Malformed JSON in file #{File.basename(f.path)}, line #{malformed_line}"
  end
  customers
end