Class: Citibike::Api
- Inherits:
-
Object
- Object
- Citibike::Api
- Defined in:
- lib/citibike/api.rb
Overview
Base class for shared behavior between all types of objects from this api
Constant Summary collapse
- EARTH_RADIUS =
Radius of the earth in miles
3963.1676
Instance Attribute Summary collapse
-
#internal_object ⇒ Object
readonly
Returns the value of attribute internal_object.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Allow access to the hash through the object.
-
#distance_from(lat, long) ⇒ Float
Returns the distance this object is from the given latitude and longitude.
-
#initialize(data) ⇒ Api
constructor
A new instance of Api.
-
#lat ⇒ Float
Shortcut to access latitude.
-
#long ⇒ Float
Shortcut to access longitude.
-
#method_missing(sym, *args, &block) ⇒ Object
Allow hash keys to be used as methods.
Constructor Details
#initialize(data) ⇒ Api
Returns a new instance of Api.
16 17 18 |
# File 'lib/citibike/api.rb', line 16 def initialize(data) @internal_object = data end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
Allow hash keys to be used as methods
66 67 68 69 70 71 72 |
# File 'lib/citibike/api.rb', line 66 def method_missing(sym, *args, &block) if self.internal_object.key?(sym.to_s) return self.internal_object[sym.to_s] end super end |
Instance Attribute Details
#internal_object ⇒ Object (readonly)
Returns the value of attribute internal_object.
14 15 16 |
# File 'lib/citibike/api.rb', line 14 def internal_object @internal_object end |
Instance Method Details
#[](key) ⇒ Object
Allow access to the hash through the object
61 62 63 |
# File 'lib/citibike/api.rb', line 61 def [](key) self.internal_object[key.to_s] end |
#distance_from(lat, long) ⇒ Float
Returns the distance this object is from the given latitude and longitude. Distance is as the crow flies.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/citibike/api.rb', line 44 def distance_from(lat, long) dLat = self.degrees_to_radians(lat - self.latitude) dLon = self.degrees_to_radians(long - self.longitude) lat1 = self.degrees_to_radians(lat) lat2 = self.degrees_to_radians(self.latitude) a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2) c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)) EARTH_RADIUS * c end |
#lat ⇒ Float
Shortcut to access latitude
24 25 26 |
# File 'lib/citibike/api.rb', line 24 def lat self['latitude'] end |
#long ⇒ Float
Shortcut to access longitude
32 33 34 |
# File 'lib/citibike/api.rb', line 32 def long self['longitude'] end |