Class: Rack::Geo::Position

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

Constant Summary collapse

MATCH_LLA =
/\A([+-]?[0-9\.]+);([+-]?[0-9\.]+)(?:;([+-]?[0-9\.]+))?/.freeze
MATCH_EPU =
/\sepu=([0-9\.]+)(?:\s|\z)/i.freeze
MATCH_HDN =
/\shdn=([0-9\.]+)(?:\s|\z)/i.freeze
MATCH_SPD =
/\sspd=([0-9\.]+)(?:\s|\z)/i.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = nil) ⇒ Position

Returns a new instance of Position.



18
19
20
# File 'lib/rack/geo.rb', line 18

def initialize(value=nil)
  parse! value
end

Instance Attribute Details

#altitudeObject

Returns the value of attribute altitude.



16
17
18
# File 'lib/rack/geo.rb', line 16

def altitude
  @altitude
end

#headingObject

Returns the value of attribute heading.



16
17
18
# File 'lib/rack/geo.rb', line 16

def heading
  @heading
end

#latitudeObject

Returns the value of attribute latitude.



16
17
18
# File 'lib/rack/geo.rb', line 16

def latitude
  @latitude
end

#longitudeObject

Returns the value of attribute longitude.



16
17
18
# File 'lib/rack/geo.rb', line 16

def longitude
  @longitude
end

#speedObject

Returns the value of attribute speed.



16
17
18
# File 'lib/rack/geo.rb', line 16

def speed
  @speed
end

#uncertaintyObject

Returns the value of attribute uncertainty.



16
17
18
# File 'lib/rack/geo.rb', line 16

def uncertainty
  @uncertainty
end

Class Method Details

.from_http_header(value) ⇒ Object



83
84
85
# File 'lib/rack/geo.rb', line 83

def self.from_http_header(value)
  self.new(value)
end

Instance Method Details

#attributesObject



63
64
65
66
67
68
69
70
71
72
# File 'lib/rack/geo.rb', line 63

def attributes
  {
    :latitude => latitude,
    :longitude => longitude,
    :altitude => altitude,
    :uncertainty => uncertainty,
    :heading => heading,
    :speed => speed,
  }
end

#parse!(value) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rack/geo.rb', line 29

def parse!(value)
  reset!
  value = value.to_s.strip

  if lla = MATCH_LLA.match(value)
    @latitude = lla[1].to_f
    @longitude = lla[2].to_f
    @altitude = lla[3].to_f if lla[3]
  end

  if epu = MATCH_EPU.match(value)
    @uncertainty = epu[1].to_f
  end

  if hdn = MATCH_HDN.match(value)
    @heading = hdn[1].to_f
  end

  if spd = MATCH_SPD.match(value)
    @speed = spd[1].to_f
  end

  nil
end

#to_http_headerObject



74
75
76
77
78
79
80
81
# File 'lib/rack/geo.rb', line 74

def to_http_header
  value = "%s;%s" % [latitude.to_f.to_s, longitude.to_f.to_s]
  value += ";%f" % altitude.to_f if altitude
  value += " epu=%f" % uncertainty.to_f if uncertainty
  value += " hdn=%f" % heading.to_f if heading
  value += " spd=%f" % speed.to_f if speed
  value
end

#valid?Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
# File 'lib/rack/geo.rb', line 54

def valid?
  (!latitude.nil? && latitude.respond_to?(:to_f) && (-90..90).include?(latitude.to_f)) &&
  (!longitude.nil? && longitude.respond_to?(:to_f) && (-180..180).include?(longitude.to_f)) &&
  (altitude.nil? || altitude.respond_to?(:to_f)) &&
  (uncertainty.nil? || uncertainty.respond_to?(:to_f) && uncertainty.to_f >= 0) &&
  (heading.nil? || heading.respond_to?(:to_f) && (0..360).include?(heading.to_f)) &&
  (speed.nil? || speed.respond_to?(:to_f) && speed.to_f >= 0)
end