Class: CitibikeTrips::Trips

Inherits:
Object
  • Object
show all
Defined in:
lib/citibike_trips/trips.rb

Constant Summary collapse

TRIPS_URL =
'https://www.citibikenyc.com/member/trips'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTrips

Returns a new instance of Trips.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/citibike_trips/trips.rb', line 6

def initialize
  agent = Mechanize.new
  @trips = []
  url = TRIPS_URL
  loop do
    page = agent.get(url)
    if page.uri.to_s == url
      page.search('tr.trip').each do |triprow|
        trips << CitibikeTrips::Trip.new(
          triprow.attributes['id'].value,
          triprow.attributes['data-start-station-id'].value,
          triprow.attributes['data-end-station-id'].value,
          triprow.attributes['data-start-timestamp'].value,
          triprow.attributes['data-end-timestamp'].value)
      end
      if nextpagelink = page.link_with(text: '>')
        url = nextpagelink.href
      else
        break
      end
    elsif page.uri.to_s == CitibikeTrips::
      CitibikeTrips.(page)
    else
      raise "Got redirected to unexpected page #{page.uri.to_s}"
    end
  end
end

Instance Attribute Details

#tripsObject (readonly)

Returns the value of attribute trips.



4
5
6
# File 'lib/citibike_trips/trips.rb', line 4

def trips
  @trips
end