tfl_journey_planner 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.7
1
+ 0.1.8
@@ -23,17 +23,43 @@ class TflJourneyPlanner
23
23
 
24
24
  def get_journeys(from, to)
25
25
  output = Nokogiri::HTML.parse(submit_form(from, to)).css('.un_space_top_bottom')
26
- '[' + output.map do |o|
27
- '{' + o.css('div').map do |d|
28
- if d.css('img').empty?
29
- "'" + d.text.strip.sub(": ", "': '") + "'"
30
- else
31
- d.text.strip + d.css('img').map{ |i| CGI.unescape(i.attr('src')).split(/\=|\&/)[1] }.inspect
32
- end
33
- end.join(", ") + '}'
34
- end.join(", ") + ']'
26
+
27
+ routes = output.map do |route|
28
+ process_route(route)
29
+ end.reject(&:empty?)
30
+
31
+ to_json(routes)
32
+ end
33
+
34
+ def to_json(routes)
35
+ "[" + routes.map{ |r| "{" + r.join(", ") + "}" }.join(", ") + "]"
35
36
  end
36
37
 
38
+ def process_route(route)
39
+ route.css('div').map do |attribute|
40
+ has_image?(attribute) ? process_interchanges(attribute) : process_attribute(attribute)
41
+ end.compact
42
+ end
43
+
44
+ def process_interchanges(attribute)
45
+ attribute.text.strip + images_urls(attribute).inspect
46
+ end
47
+
48
+ def images_urls(attribute)
49
+ attribute.css('img').map{ |i| CGI.unescape(i.attr('src')).split(/\=|\&/)[1] }
50
+ end
51
+
52
+ def process_attribute(attribute)
53
+ "'" + attribute.text.strip.sub(": ", "': '") + "'" if has_data?(attribute)
54
+ end
55
+
56
+ def has_data?(attribute)
57
+ !attribute.text.empty? && attribute.text.split(":").length > 1
58
+ end
59
+
60
+ def has_image?(attribute)
61
+ !attribute.css('img').empty?
62
+ end
37
63
  end
38
64
 
39
65
  end
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{tfl_journey_planner}
8
- s.version = "0.1.7"
8
+ s.version = "0.1.8"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jordi Noguera"]
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tfl_journey_planner
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 11
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 7
10
- version: 0.1.7
9
+ - 8
10
+ version: 0.1.8
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jordi Noguera