tramtracker 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -7,13 +7,28 @@ A commandline tool for Melbourne's TramTracker
7
7
  Installation
8
8
  ------------
9
9
 
10
- `gem install tramtracker`
10
+ ```
11
+ gem install tramtracker
12
+ ```
11
13
 
12
14
 
13
15
  Basic usage
14
16
  -----------
15
17
 
16
- `tramtracker <stop-id>`
18
+ To simply print the next trams for a given stop:
19
+ ```
20
+ tramtracker <stop-id>
21
+ ```
22
+
23
+ To poll at a regular interval, use the -p option:
24
+ ```
25
+ tramtracker -p <stop-id>
26
+ ```
27
+
28
+ To see the help information, use the -h options:
29
+ ```
30
+ tramtracker -h
31
+ ```
17
32
 
18
33
 
19
34
  Configuration
data/bin/tramtracker CHANGED
@@ -1,14 +1,21 @@
1
1
  #!/usr/bin/env ruby
2
- require 'tramtracker'
2
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
3
+
3
4
  require 'optparse'
5
+ require 'tramtracker'
6
+ require 'formatters'
7
+
8
+
9
+ formatter_class = Formatters::PrintFormatter
10
+ dotfile = "#{ENV['HOME']}/.tramtracker"
11
+
4
12
 
5
13
  optparse = OptionParser.new do |opts|
6
14
  opts.banner = "Usage: tramtracker [options] <stop-id>"
7
15
 
8
- # opts.on('-p', '--poll', "Poll every 15 seconds") do
9
- # options[:interval] = 15
10
- # options[:max_iterations] = 60
11
- # end
16
+ opts.on('-p', '--poll', "Poll every 15 seconds") do
17
+ formatter_class = Formatters::PollingFormatter
18
+ end
12
19
 
13
20
  opts.on('-h', '--help', 'Display this screen') do
14
21
  puts opts
@@ -17,17 +24,18 @@ optparse = OptionParser.new do |opts|
17
24
  end
18
25
  optparse.parse!
19
26
 
27
+
20
28
  id = ARGV[0]
21
- dotfile = "#{ENV['HOME']}/.tramtracker"
22
29
  id ||= File.read(dotfile) if File.exists?(dotfile)
23
-
24
30
  unless id
25
31
  puts optparse
26
32
  exit 1
27
33
  end
28
34
 
29
35
 
30
- puts Time.now
31
- Tramtracker.new(id).get.each do |tram|
32
- printf "Route %3s arriving in %2s minutes\n", tram[:route], tram[:minutes]
36
+ begin
37
+ formatter_class.new(Tramtracker.new(id)).report
38
+ rescue Interrupt
39
+ # Prevent the Exception stack trace from being printed upon ctrl-c
40
+ puts # This just puts the next prompt on its own line
33
41
  end
data/lib/formatters.rb ADDED
@@ -0,0 +1,2 @@
1
+ require "formatters/print_formatter"
2
+ require "formatters/polling_formatter"
@@ -0,0 +1,46 @@
1
+ module Formatters
2
+ class PollingFormatter
3
+
4
+ KILL_LINE_CHAR = "\x1B[K"
5
+ CURSOR_UP_CHAR = "\x1B[A"
6
+
7
+ def initialize(tramtracker)
8
+ @tramtracker = tramtracker
9
+ @delay_interval = 15
10
+ @max_iterations = 60
11
+ end
12
+
13
+ def report
14
+ puts Time.now.strftime "[%H:%M:%S] Stop #{@tramtracker.stop_id}"
15
+ puts "Retrieving tram information..."
16
+ last_results_count = 0
17
+ @max_iterations.times do |iteration|
18
+ results = @tramtracker.get
19
+ clear(last_results_count + 2)
20
+ last_results_count = results.count
21
+ puts Time.now.strftime "[%H:%M:%S] Stop #{@tramtracker.stop_id}"
22
+ results.each do |tram|
23
+ printf "Route %3s arriving in %2s minutes\n",
24
+ tram[:route],
25
+ tram[:minutes]
26
+ end
27
+ delay @delay_interval
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def delay(seconds)
34
+ seconds.times do |i|
35
+ print "\rRetrieving again in #{seconds-i} seconds..."
36
+ sleep 1
37
+ end
38
+ print "\r#{KILL_LINE_CHAR}Retrieving again NOW...\n"
39
+ end
40
+
41
+ def clear(lines)
42
+ lines.times { print "#{CURSOR_UP_CHAR}#{KILL_LINE_CHAR}" }
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,17 @@
1
+ module Formatters
2
+ class PrintFormatter
3
+ def initialize(tramtracker)
4
+ @tramtracker = tramtracker
5
+ end
6
+
7
+ def report
8
+ puts Time.now.strftime "[%H:%M:%S] Stop #{@tramtracker.stop_id}"
9
+ puts "Retrieving tram information..."
10
+ @tramtracker.get.each do |tram|
11
+ printf "Route %3s arriving in %2s minutes\n",
12
+ tram[:route],
13
+ tram[:minutes]
14
+ end
15
+ end
16
+ end
17
+ end
data/lib/tramtracker.rb CHANGED
@@ -1,17 +1,26 @@
1
1
  require 'open-uri'
2
2
 
3
3
  class Tramtracker
4
+ attr_accessor :stop_id
5
+
4
6
  def initialize(stop_id)
5
- @url = "http://www.tramtracker.com.au/?id=#{stop_id}"
7
+ @stop_id = stop_id
6
8
  end
7
9
 
8
10
  def get
9
11
  regex = /\d*\) Rte (\d*)<br>\r\n(\d*)/
10
- open(@url).read.scan(regex).collect do |route, minutes|
12
+ open(url).read.scan(regex).collect do |route, minutes|
11
13
  {
12
- :route => route,
13
- :minutes => minutes
14
+ route: route,
15
+ minutes: minutes
14
16
  }
15
17
  end
16
18
  end
19
+
20
+ private
21
+
22
+ def url
23
+ "http://www.tramtracker.com.au/?id=#{@stop_id}"
24
+ end
25
+
17
26
  end
data/tramtracker.gemspec CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |gem|
6
6
  gem.name = "tramtracker"
7
- gem.version = "0.0.3"
7
+ gem.version = "0.0.4"
8
8
  gem.authors = ["Steven Occhipinti"]
9
9
  gem.email = ["dev@stevenocchipinti.com"]
10
10
  gem.description = "A commandline tool for Melbourne's TramTracker"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tramtracker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-01 00:00:00.000000000 Z
12
+ date: 2013-06-08 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A commandline tool for Melbourne's TramTracker
15
15
  email:
@@ -25,6 +25,9 @@ files:
25
25
  - README.md
26
26
  - Rakefile
27
27
  - bin/tramtracker
28
+ - lib/formatters.rb
29
+ - lib/formatters/polling_formatter.rb
30
+ - lib/formatters/print_formatter.rb
28
31
  - lib/tramtracker.rb
29
32
  - test/lib/tramtracker_test.rb
30
33
  - test/test_helper.rb
@@ -49,7 +52,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
49
52
  version: '0'
50
53
  requirements: []
51
54
  rubyforge_project:
52
- rubygems_version: 1.8.24
55
+ rubygems_version: 1.8.25
53
56
  signing_key:
54
57
  specification_version: 3
55
58
  summary: CLI TramTracker