krk-timetables 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .rvmrc
6
+ .DS_Store
7
+ coverage/
data/.rspec ADDED
File without changes
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in krk-timetables.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new('spec')
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "krk-timetables/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "krk-timetables"
7
+ s.version = KrkTimetables::VERSION
8
+ s.authors = ["Marek Nowak"]
9
+ s.email = ["mareknowakk@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{A gem parsing Kraków MPK timetables.}
12
+ s.description = %q{This is a gem parsing Kraków MPK timetables from the mpk.krakow.pl website and returning them in a form of nice Ruby hashes.}
13
+
14
+ s.rubyforge_project = "krk-timetables"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency "rspec", "~> 2.7"
22
+ s.add_development_dependency "autotest"
23
+ s.add_development_dependency "simplecov"
24
+ s.add_runtime_dependency "nokogiri"
25
+ end
@@ -0,0 +1,25 @@
1
+ require "nokogiri"
2
+ require "open-uri"
3
+
4
+ require "krk-timetables/version"
5
+ require "krk-timetables_stop"
6
+ require "krk-timetables_line"
7
+ require "krk-timetables_departure"
8
+
9
+ module KrkTimetables
10
+
11
+ URL_PREFIX = "http://rozklady.mpk.krakow.pl/aktualne/"
12
+ STOPS_PATH = "przystan.htm"
13
+
14
+ def KrkTimetables.stops
15
+ doc = Nokogiri::HTML(open(URL_PREFIX + STOPS_PATH))
16
+ stops = []
17
+
18
+ doc.css('table li a').each do |stop_link|
19
+ stops << Stop.new(stop_link.content, stop_link.attributes["href"].value)
20
+ end
21
+
22
+ stops
23
+ end
24
+
25
+ end
@@ -0,0 +1,3 @@
1
+ module KrkTimetables
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,17 @@
1
+ class KrkTimetables::Departure
2
+
3
+ def initialize(hour_part, minute_part)
4
+ @hour_part, @minute_part = hour_part, minute_part
5
+ end
6
+
7
+ def time
8
+ "#{@hour_part}:#{@minute_part.to_s.rjust(2, "0")}"
9
+ end
10
+
11
+ def time_today
12
+ now = Time.now
13
+
14
+ Time.new(now.year, now.month, now.day, @hour_part, @minute_part)
15
+ end
16
+
17
+ end
@@ -0,0 +1,44 @@
1
+ class KrkTimetables::Line
2
+
3
+ attr_reader :number, :destination
4
+
5
+ def initialize(number, destination, url)
6
+ @number, @destination, @departures = number, destination, {}
7
+
8
+ # the following "r" to "t" substitution is needed to get the right page without any frames
9
+ @url = url.gsub(/r|\.\.\//, "r" => "t", "../" => "")
10
+ end
11
+
12
+ def departures
13
+ return @departures unless @departures.empty?
14
+
15
+ departure_types = ["work_weekdays", "saturdays", "sundays"]
16
+ departure_types.each do |type|
17
+ @departures[type] = []
18
+ end
19
+
20
+ doc = Nokogiri::HTML(open(KrkTimetables::URL_PREFIX + @url))
21
+
22
+ doc.css("table table tr").each do |row|
23
+ next if (cells = row.css("td")).size == 3
24
+
25
+ hour = nil
26
+
27
+ cells.each_with_index do |cell, cell_index|
28
+ if (hour_cell = cell.css("b").first)
29
+ hour = hour_cell.content
30
+ elsif not (minutes = cell.content.split).empty?
31
+ minutes.each do |minute|
32
+ next if minute == "-"
33
+
34
+ type = departure_types[cell_index/2]
35
+
36
+ @departures[type] << KrkTimetables::Departure.new(hour.to_i, minute.to_i)
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ @departures
43
+ end
44
+ end
@@ -0,0 +1,32 @@
1
+ class KrkTimetables::Stop
2
+
3
+ attr_reader :name
4
+
5
+ def initialize(name, url)
6
+ @name, @lines, @url = name, [], url
7
+ end
8
+
9
+ def self.find_by_name(name)
10
+ KrkTimetables.stops.select { |stop| stop.name == name }.first
11
+ end
12
+
13
+ def lines
14
+ return @lines unless @lines.empty?
15
+
16
+ doc = Nokogiri::HTML(open(KrkTimetables::URL_PREFIX + @url))
17
+
18
+ doc.css("table li a").each do |line_link|
19
+ line_link_data = line_link.content.split(" - > ")
20
+ next unless line_link_data.size == 2
21
+
22
+ number = line_link_data.first.to_i
23
+ destination = line_link_data.last
24
+ url = line_link.attributes["href"].value
25
+
26
+ @lines << KrkTimetables::Line.new(number, destination, url)
27
+ end
28
+
29
+ @lines
30
+ end
31
+
32
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe KrkTimetables::Departure do
4
+
5
+ before(:each) do
6
+ @departure = KrkTimetables::Stop.find_by_name("Cracovia").lines.first.departures["work_weekdays"].first
7
+ end
8
+
9
+ describe "#time" do
10
+
11
+ it "has a proper time attribute" do
12
+ @departure.time.should match(/^\d?\d:\d\d$/)
13
+ end
14
+
15
+ end
16
+
17
+ describe "#time_today" do
18
+
19
+ it "is a Time object" do
20
+ @departure.time_today.should be_a(Time)
21
+ end
22
+
23
+ end
24
+
25
+ end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ describe KrkTimetables::Line do
4
+
5
+ before(:each) do
6
+ @line = KrkTimetables::Stop.find_by_name("Cracovia").lines.first
7
+ end
8
+
9
+ describe "#number" do
10
+
11
+ it "is an Integer" do
12
+ @line.number.should be_a(Integer)
13
+ end
14
+
15
+ it "is bigger than zero" do
16
+ @line.number.should > 0
17
+ end
18
+
19
+ end
20
+
21
+ describe "#destination" do
22
+
23
+ it "is a String" do
24
+ @line.destination.should be_a(String)
25
+ end
26
+
27
+ it "should not be blank" do
28
+ @line.destination.should_not be_empty
29
+ end
30
+
31
+ end
32
+
33
+ describe "#departures" do
34
+
35
+ it "should be a Hash" do
36
+ @line.departures.should be_a(Hash)
37
+ end
38
+
39
+ it "should not be empty" do
40
+ @line.departures.should_not be_empty
41
+ end
42
+
43
+ it "should contain categorized departures" do
44
+ expected_keys = ["work_weekdays", "saturdays", "sundays"]
45
+
46
+ departures = @line.departures
47
+ departures.size.should == 3
48
+ (departures.keys & expected_keys).size.should == 3
49
+ expected_keys.each do |key|
50
+ @line.departures[key].should be_a(Array)
51
+ @line.departures[key].first.should be_a(KrkTimetables::Departure)
52
+ @line.departures[key].last.should be_a(KrkTimetables::Departure)
53
+ end
54
+ end
55
+
56
+ end
57
+
58
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe KrkTimetables do
4
+
5
+ describe "#stops" do
6
+
7
+ before(:each) do
8
+ @stops = KrkTimetables.stops
9
+ end
10
+
11
+ it "is not empty" do
12
+ @stops.should_not be_empty
13
+ end
14
+
15
+ it "should be an Array" do
16
+ @stops.should be_a(Array)
17
+ end
18
+
19
+ it "should contain stops" do
20
+ @stops.first.should be_a(KrkTimetables::Stop)
21
+ @stops.last.should be_a(KrkTimetables::Stop)
22
+ end
23
+
24
+ it "should contain Cracovia and Teatr Bagatela stops" do
25
+ stops_names = @stops.map {|stop| stop.name }
26
+ stops_names.should include("Cracovia")
27
+ stops_names.should include("Teatr Bagatela")
28
+ end
29
+
30
+ end
31
+
32
+ end
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+
3
+ describe KrkTimetables::Stop do
4
+
5
+ describe "#find_by_name" do
6
+
7
+ it "can find Cracovia and Teatr Bagatela stops" do
8
+ cracovia = KrkTimetables::Stop.find_by_name("Cracovia")
9
+ cracovia.should_not be_nil
10
+ cracovia.should be_a(KrkTimetables::Stop)
11
+ cracovia.name.should == "Cracovia"
12
+
13
+ teatr_bagatela = KrkTimetables::Stop.find_by_name("Teatr Bagatela")
14
+ teatr_bagatela.should_not be_nil
15
+ teatr_bagatela.should be_a(KrkTimetables::Stop)
16
+ teatr_bagatela.name.should == "Teatr Bagatela"
17
+ end
18
+
19
+ it "can't find The Stop That Does Not Exist" do
20
+ KrkTimetables::Stop.find_by_name("The Stop That Does Not Exist").should be_nil
21
+ end
22
+
23
+ end
24
+
25
+ describe "#lines" do
26
+
27
+ before(:each) do
28
+ @stop = KrkTimetables::Stop.find_by_name("Cracovia")
29
+ @lines = @stop.lines
30
+ end
31
+
32
+ it "is an Array" do
33
+ @lines.should be_a(Array)
34
+ end
35
+
36
+ it "is not empty" do
37
+ @lines.should_not be_empty
38
+ end
39
+
40
+ it "should contain lines" do
41
+ @lines.first.should be_a(KrkTimetables::Line)
42
+ @lines.last.should be_a(KrkTimetables::Line)
43
+ end
44
+
45
+ it "should contain a 164 line" do
46
+ @lines.select { |line| line.number == 164 }.should_not be_empty
47
+ end
48
+
49
+ end
50
+
51
+ end
@@ -0,0 +1,10 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+
4
+ require 'rspec'
5
+ require 'krk-timetables'
6
+
7
+ RSpec.configure do |config|
8
+ config.color_enabled = true
9
+ config.formatter = 'documentation'
10
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: krk-timetables
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Marek Nowak
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-04 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70162864160160 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.7'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70162864160160
25
+ - !ruby/object:Gem::Dependency
26
+ name: autotest
27
+ requirement: &70162864159740 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70162864159740
36
+ - !ruby/object:Gem::Dependency
37
+ name: simplecov
38
+ requirement: &70162864159280 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70162864159280
47
+ - !ruby/object:Gem::Dependency
48
+ name: nokogiri
49
+ requirement: &70162864158860 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *70162864158860
58
+ description: This is a gem parsing Kraków MPK timetables from the mpk.krakow.pl website
59
+ and returning them in a form of nice Ruby hashes.
60
+ email:
61
+ - mareknowakk@gmail.com
62
+ executables: []
63
+ extensions: []
64
+ extra_rdoc_files: []
65
+ files:
66
+ - .gitignore
67
+ - .rspec
68
+ - Gemfile
69
+ - Rakefile
70
+ - krk-timetables.gemspec
71
+ - lib/krk-timetables.rb
72
+ - lib/krk-timetables/version.rb
73
+ - lib/krk-timetables_departure.rb
74
+ - lib/krk-timetables_line.rb
75
+ - lib/krk-timetables_stop.rb
76
+ - spec/krk-timetables_departure_spec.rb
77
+ - spec/krk-timetables_line_spec.rb
78
+ - spec/krk-timetables_spec.rb
79
+ - spec/krk-timetables_stop_spec.rb
80
+ - spec/spec_helper.rb
81
+ homepage: ''
82
+ licenses: []
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project: krk-timetables
101
+ rubygems_version: 1.8.10
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: A gem parsing Kraków MPK timetables.
105
+ test_files:
106
+ - spec/krk-timetables_departure_spec.rb
107
+ - spec/krk-timetables_line_spec.rb
108
+ - spec/krk-timetables_spec.rb
109
+ - spec/krk-timetables_stop_spec.rb
110
+ - spec/spec_helper.rb