Class: SpeedtestNet::Geo

Inherits:
Object
  • Object
show all
Defined in:
lib/speedtest_net/geo.rb

Constant Summary collapse

RADIAN_PER_DEGREE =
Math::PI / 180
EARTH_RADIUS =
6_371_000

Instance Method Summary collapse

Constructor Details

#initialize(lat, long) ⇒ Geo

Returns a new instance of Geo.



8
9
10
11
# File 'lib/speedtest_net/geo.rb', line 8

def initialize(lat, long)
  @lat = lat
  @long = long
end

Instance Method Details

#distance(other) ⇒ Object

rubocop:disable Metrics/AbcSize



13
14
15
16
17
18
# File 'lib/speedtest_net/geo.rb', line 13

def distance(other) # rubocop:disable Metrics/AbcSize
  a = (Math.sin(radian_lat) * Math.sin(other.radian_lat)) +
      (Math.cos(radian_lat) * Math.cos(other.radian_lat) * Math.cos(radian_long - other.radian_long))

  Math.acos(a) * EARTH_RADIUS
end

#radian_latObject



20
21
22
# File 'lib/speedtest_net/geo.rb', line 20

def radian_lat
  @lat * RADIAN_PER_DEGREE
end

#radian_longObject



24
25
26
# File 'lib/speedtest_net/geo.rb', line 24

def radian_long
  @long * RADIAN_PER_DEGREE
end