citibike_trips 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4fb47c44deab61a7ecaf6bc39f6ff1c337302989
4
- data.tar.gz: 8811c205511718fad6dce86f0b655faab8cac151
3
+ metadata.gz: 6fba8e04a64f48c175b81ccd0a4f0710d701f9a4
4
+ data.tar.gz: 52337dcffc7f85acc1c5273ae1ea003c8973ee9f
5
5
  SHA512:
6
- metadata.gz: 84fee389d791bfcb30e85fce4b54bf36c789f1d6f523c3ad1602bcff9ecd0faa08e345f16232403d09283432ab4c744d1edfe24f4f94a6fd1f848c0d66bca31e
7
- data.tar.gz: d29e44c8e56a7c5058ea68ddf85c23d27c3c36ec1b69e5a370d4f7db41f57db0f2faffcae84251996f8cfedb6349e97522c684ac73ecd540a0ae84d344088e64
6
+ metadata.gz: 612f2a71a88604036f83728a0f6e3f1a790b23a91e1c45960ad81bc71354455a599464f6c50f88182de57a39d28985dcb561d38ac0822cffeb9546545029aba7
7
+ data.tar.gz: cdd34b158aa712dbe06012ee1adfe9709df4550f6b755e6b44c9ebbf53cd119035c05a62328620893d1a9cc1cabfedc585f9f8b10d600cbefdc497aac09d3eb0
data/README.md CHANGED
@@ -21,13 +21,13 @@ Or install it yourself as:
21
21
  Each trip has a unique id, start station, end station, start timestamp and end timestamp. The stations will be an instance of CitibikeTrips::Station.
22
22
 
23
23
  trips = CitibikeTrips::Trips.new
24
- puts trips.trips.length
25
24
  puts trips.first.start_station.name
26
25
  puts trips.first.end_timestamp
26
+ trips.each{|trip| puts trip.id}
27
27
 
28
28
  stations = CitibikeTrips::Stations.new
29
- puts stations.stations.length
30
29
  puts stations[72].name
30
+ stations.each{|id, station| puts station.name}
31
31
 
32
32
  ## Contributing
33
33
 
@@ -3,15 +3,17 @@ require 'citibike_trips/station'
3
3
  require 'citibike_trips/stations'
4
4
  require 'citibike_trips/trip'
5
5
  require 'citibike_trips/trips'
6
- require 'highline/import'
6
+ require 'highline'
7
7
  require 'mechanize'
8
8
 
9
9
  module CitibikeTrips
10
10
  LOGIN_URL = 'https://www.citibikenyc.com/login'
11
- def self.login(page)
11
+ def self.login(page, options={})
12
+ stdout = options[:stdout] || $stdout
13
+ h = HighLine.new($stdin, stdout)
12
14
  page.form_with(action: LOGIN_URL) do |form|
13
- form.subscriberUsername = ask('Citi Bike username: ')
14
- form.subscriberPassword = ask('Citi Bike password: ') {|q| q.echo = false}
15
+ form.subscriberUsername = h.ask('Citi Bike username: ')
16
+ form.subscriberPassword = h.ask('Citi Bike password: ') {|q| q.echo = false}
15
17
  form.click_button(form.button_with(name: 'login_submit'))
16
18
  end
17
19
  end
@@ -1,3 +1,5 @@
1
+ require 'json'
2
+
1
3
  class CitibikeTrips::Station
2
4
  attr_reader :id, :name,
3
5
  :available_docks, :total_docks, :available_bikes,
@@ -34,4 +36,7 @@ class CitibikeTrips::Station
34
36
  @last_communication_time = data['lastCommunicationTime']
35
37
  @landmark = data['landMark']
36
38
  end
39
+ def to_json(*a)
40
+ Hash[instance_variables.collect{|i| [i[1..-1], instance_variable_get(i)]}].to_json(*a)
41
+ end
37
42
  end
@@ -1,4 +1,7 @@
1
+ require 'json'
2
+
1
3
  class CitibikeTrips::Stations
4
+ include Enumerable
2
5
  STATIONS_URL = 'https://www.citibikenyc.com/stations/json'
3
6
 
4
7
  attr_reader :timestamp, :stations
@@ -18,4 +21,10 @@ class CitibikeTrips::Stations
18
21
  def [](id)
19
22
  @stations[id]
20
23
  end
24
+ def each(&block)
25
+ @stations.each(&block)
26
+ end
27
+ def to_json(*a)
28
+ @stations.to_json(*a)
29
+ end
21
30
  end
@@ -1,12 +1,18 @@
1
+ require 'json'
2
+
1
3
  class CitibikeTrips::Trip
2
4
  attr_reader :id, :start_station, :end_station, :start_timestamp, :end_timestamp
3
5
 
4
- def initialize(id, start_station, end_station, start_timestamp, end_timestmap)
6
+ def initialize(id, start_station, end_station, start_timestamp, end_timestamp)
5
7
  @id = id
6
8
  stations = CitibikeTrips::Stations.new
7
9
  @start_station = stations[start_station.to_i]
8
10
  @end_station = stations[end_station.to_i]
9
11
  @start_timestamp = Time.at(start_timestamp.to_i)
10
- @end_timestamp = Time.at(end_timestmap.to_i)
12
+ @end_timestamp = Time.at(end_timestamp.to_i)
13
+ end
14
+
15
+ def to_json(*a)
16
+ Hash[instance_variables.collect{|i| [i[1..-1], instance_variable_get(i)]}].to_json(*a)
11
17
  end
12
18
  end
@@ -1,9 +1,12 @@
1
+ require 'json'
2
+
1
3
  class CitibikeTrips::Trips
4
+ include Enumerable
2
5
  TRIPS_URL = 'https://www.citibikenyc.com/member/trips'
3
6
 
4
7
  attr_reader :trips
5
8
 
6
- def initialize
9
+ def initialize(options={})
7
10
  agent = Mechanize.new
8
11
  @trips = []
9
12
  url = TRIPS_URL
@@ -24,10 +27,20 @@ class CitibikeTrips::Trips
24
27
  break
25
28
  end
26
29
  elsif page.uri.to_s == CitibikeTrips::LOGIN_URL
27
- CitibikeTrips.login(page)
30
+ CitibikeTrips.login(page, {stdout: options[:stdout]})
28
31
  else
29
32
  raise "Got redirected to unexpected page #{page.uri.to_s}"
30
33
  end
31
34
  end
32
35
  end
36
+
37
+ def [](index)
38
+ @trips[index]
39
+ end
40
+ def each(&block)
41
+ @trips.each(&block)
42
+ end
43
+ def to_json(*a)
44
+ @trips.to_json(*a)
45
+ end
33
46
  end
@@ -1,3 +1,3 @@
1
1
  module CitibikeTrips
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/test/test_station.rb CHANGED
@@ -22,20 +22,32 @@ class StationTests < Test::Unit::TestCase
22
22
  "testStation"=>false,
23
23
  "lastCommunicationTime"=>nil,
24
24
  "landMark"=>""}
25
+ @station = CitibikeTrips::Station.new(@data)
25
26
  end
26
27
 
27
28
  def test_initialize
28
- station = CitibikeTrips::Station.new(@data)
29
29
  # Some regular fields
30
- assert_equal 72, station.id
31
- assert_equal 'W 52 St & 11 Ave', station.name
32
- assert_equal false, station.test_station
30
+ assert_equal 72, @station.id
31
+ assert_equal 'W 52 St & 11 Ave', @station.name
32
+ assert_equal false, @station.test_station
33
33
  # Test conversion of empty string values to nil
34
- assert_nil station.location
34
+ assert_nil @station.location
35
35
  # Test special handling of city
36
- assert_equal 'New York', station.city
36
+ assert_equal 'New York', @station.city
37
37
  # Virtual fields
38
- assert_equal ['W 52 St & 11 Ave'], station.street_address_array
39
- assert_equal 'W 52 St & 11 Ave', station.street_address
38
+ assert_equal ['W 52 St & 11 Ave'], @station.street_address_array
39
+ assert_equal 'W 52 St & 11 Ave', @station.street_address
40
+ end
41
+ def test_to_json
42
+ json = @station.to_json
43
+ assert_kind_of String, json
44
+ data = JSON.parse(json)
45
+ assert_equal 72, data['id']
46
+ assert_equal 'W 52 St & 11 Ave', data['name']
47
+ assert_equal false, data['test_station']
48
+ assert_nil data['location']
49
+ assert_equal 'New York', data['city']
50
+ assert_equal ['W 52 St & 11 Ave'], data['street_address_array']
51
+ assert_equal 'W 52 St & 11 Ave', data['street_address']
40
52
  end
41
53
  end
@@ -22,4 +22,21 @@ class StationsTests < Test::Unit::TestCase
22
22
  assert_equal 'W 52 St & 11 Ave', @stations[72].name
23
23
  assert_equal 20, @stations[3002].available_docks
24
24
  end
25
+ def test_each
26
+ count = 0
27
+ @stations.each{|id, station| count += 1}
28
+ assert_equal 332, count
29
+ end
30
+ def test_includes_enumerable
31
+ assert @stations.any?{|id, station| id == 72 && station.name == 'W 52 St & 11 Ave'}
32
+ assert @stations.all?{|id, station| station.name}
33
+ end
34
+ def test_to_json
35
+ json = @stations.to_json
36
+ assert_kind_of String, json
37
+ data = JSON.parse(json)
38
+ assert_equal 332, data.length
39
+ assert_equal 'W 52 St & 11 Ave', data['72']['name']
40
+ assert_equal 20, data['3002']['available_docks']
41
+ end
25
42
  end
data/test/test_trip.rb CHANGED
@@ -2,19 +2,27 @@ require 'test/unit'
2
2
  require 'citibike_trips'
3
3
 
4
4
  class TripTests < Test::Unit::TestCase
5
- def test_initialize
5
+ def setup
6
6
  page = Mechanize::Page.new
7
7
  Mechanize.any_instance.stubs(:get).with(CitibikeTrips::Stations::STATIONS_URL).returns(page)
8
8
  json = File.read("#{File.dirname(__FILE__)}/samples/stations.json")
9
9
  page.stubs(:body).returns(json)
10
-
11
- trip = CitibikeTrips::Trip.new('trip-13150976', '151', '517', '1406760278', '1406761369')
12
- assert_equal 'trip-13150976', trip.id
13
- assert_kind_of CitibikeTrips::Station, trip.start_station
14
- assert_equal 'Cleveland Pl & Spring St', trip.start_station.name
15
- assert_kind_of CitibikeTrips::Station, trip.end_station
16
- assert_equal 'E 41 St & Madison Ave', trip.end_station.name
17
- assert_equal Time.at(1406760278), trip.start_timestamp
18
- assert_equal Time.at(1406761369), trip.end_timestamp
10
+ @trip = CitibikeTrips::Trip.new('trip-13150976', '151', '517', '1406760278', '1406761369')
11
+ end
12
+ def test_initialize
13
+ assert_equal 'trip-13150976', @trip.id
14
+ assert_kind_of CitibikeTrips::Station, @trip.start_station
15
+ assert_equal 'Cleveland Pl & Spring St', @trip.start_station.name
16
+ assert_kind_of CitibikeTrips::Station, @trip.end_station
17
+ assert_equal 'E 41 St & Madison Ave', @trip.end_station.name
18
+ assert_equal Time.at(1406760278), @trip.start_timestamp
19
+ assert_equal Time.at(1406761369), @trip.end_timestamp
20
+ end
21
+ def test_to_json
22
+ json = @trip.to_json
23
+ assert_kind_of String, json
24
+ data = JSON.parse(json)
25
+ assert_equal 'trip-13150976', data['id']
26
+ assert_equal 'E 41 St & Madison Ave', data['end_station']['name']
19
27
  end
20
28
  end
data/test/test_trips.rb CHANGED
@@ -3,7 +3,7 @@ require 'mocha/test_unit'
3
3
  require 'citibike_trips'
4
4
 
5
5
  class TripsTests < Test::Unit::TestCase
6
- def test_initialize
6
+ def setup
7
7
  agent = Mechanize.new
8
8
  uri1 = 'https://www.citibikenyc.com/member/trips'
9
9
  uri2 = 'https://www.citibikenyc.com/member/trips/2'
@@ -25,14 +25,38 @@ class TripsTests < Test::Unit::TestCase
25
25
  json = File.read("#{File.dirname(__FILE__)}/samples/stations.json")
26
26
  page.stubs(:body).returns(json)
27
27
 
28
- trips = CitibikeTrips::Trips.new
28
+ @trips = CitibikeTrips::Trips.new
29
+ end
30
+
31
+ def test_initialize
29
32
  # 26+20+4
30
- assert_equal 50, trips.trips.length
31
- trips.trips.each do |t|
33
+ assert_equal 50, @trips.trips.length
34
+ @trips.trips.each do |t|
32
35
  assert_kind_of CitibikeTrips::Trip, t
33
36
  end
34
- assert_equal 'trip-13150976', trips.trips.first.id
35
- assert_equal Time.at(1369869537), trips.trips.last.start_timestamp
36
- assert_equal 'Broadway & W 58 St', trips.trips.last.start_station.name
37
+ assert_equal 'trip-13150976', @trips.trips.first.id
38
+ assert_equal Time.at(1369869537), @trips.trips.last.start_timestamp
39
+ assert_equal 'Broadway & W 58 St', @trips.trips.last.start_station.name
40
+ end
41
+ def test_index_access
42
+ assert_equal 'trip-13150976', @trips[0].id
43
+ assert_equal 'Broadway & W 58 St', @trips[-1].start_station.name
44
+ end
45
+ def test_each
46
+ count = 0
47
+ @trips.each{|trip| count += 1}
48
+ assert_equal 50, count
49
+ end
50
+ def test_includes_enumerable
51
+ assert @trips.any?{|trip| trip.id == 'trip-13150976'}
52
+ assert @trips.all?{|trip| trip.id}
53
+ end
54
+ def test_to_json
55
+ json = @trips.to_json
56
+ assert_kind_of String, json
57
+ data = JSON.parse(json)
58
+ assert_equal 50, data.length
59
+ assert_equal 'trip-13150976', data[0]['id']
60
+ assert_equal 'Broadway & W 58 St', data[-1]['start_station']['name']
37
61
  end
38
62
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: citibike_trips
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Heiss
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-01 00:00:00.000000000 Z
11
+ date: 2014-10-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -140,4 +140,3 @@ test_files:
140
140
  - test/test_stations.rb
141
141
  - test/test_trip.rb
142
142
  - test/test_trips.rb
143
- has_rdoc: