Class: Rack::Geo::Position
- Inherits:
-
Object
- Object
- Rack::Geo::Position
- 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
-
#altitude ⇒ Object
Returns the value of attribute altitude.
-
#heading ⇒ Object
Returns the value of attribute heading.
-
#latitude ⇒ Object
Returns the value of attribute latitude.
-
#longitude ⇒ Object
Returns the value of attribute longitude.
-
#speed ⇒ Object
Returns the value of attribute speed.
-
#uncertainty ⇒ Object
Returns the value of attribute uncertainty.
Class Method Summary collapse
Instance Method Summary collapse
- #attributes ⇒ Object
-
#initialize(value = nil) ⇒ Position
constructor
A new instance of Position.
-
#parse!(value) ⇒ Object
Parse Geo-Position header: tools.ietf.org/html/draft-daviel-http-geo-header-05.
- #to_http_header ⇒ Object
- #valid? ⇒ Boolean
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
#altitude ⇒ Object
Returns the value of attribute altitude.
16 17 18 |
# File 'lib/rack/geo.rb', line 16 def altitude @altitude end |
#heading ⇒ Object
Returns the value of attribute heading.
16 17 18 |
# File 'lib/rack/geo.rb', line 16 def heading @heading end |
#latitude ⇒ Object
Returns the value of attribute latitude.
16 17 18 |
# File 'lib/rack/geo.rb', line 16 def latitude @latitude end |
#longitude ⇒ Object
Returns the value of attribute longitude.
16 17 18 |
# File 'lib/rack/geo.rb', line 16 def longitude @longitude end |
#speed ⇒ Object
Returns the value of attribute speed.
16 17 18 |
# File 'lib/rack/geo.rb', line 16 def speed @speed end |
#uncertainty ⇒ Object
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
#attributes ⇒ Object
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
Parse Geo-Position header: tools.ietf.org/html/draft-daviel-http-geo-header-05
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_header ⇒ Object
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
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 |