Class: Citibike::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/citibike/api.rb

Overview

Base class for shared behavior between all types of objects from this api

Direct Known Subclasses

Branch, Helmet, Station

Constant Summary collapse

EARTH_RADIUS =

Radius of the earth in miles

3963.1676

Instance Attribute Summary collapse

Instance Method Summary collapse

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_objectObject (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.

Parameters:

  • lat (Float)
    A latitude position
  • long (Float)
    A longitude position

Returns:

  • (Float)
    Distance from the input postion in miles


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

#latFloat

Shortcut to access latitude

Returns:

  • (Float)
    Object’s latitude position


24
25
26
# File 'lib/citibike/api.rb', line 24

def lat
  self['latitude']
end

#longFloat

Shortcut to access longitude

Returns:

  • (Float)
    Object’s longitude position


32
33
34
# File 'lib/citibike/api.rb', line 32

def long
  self['longitude']
end