spbus 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 58eec47fb8c6377374cede176ce2613b1639f928
4
+ data.tar.gz: 60981bfd93bd384d157024f95f3b96e390f6e125
5
+ SHA512:
6
+ metadata.gz: c573ccd0ecccec2804867de1440fba932d961c751297f1ee2c1c71ef4c9a90338cbf3d530d2da25b6411a9231667d5b4c1a65ad9bb36d8a90d211987e964dee9
7
+ data.tar.gz: b73ba4b11c0e8eb8ce5a141a02f5929841eedb01eec3927dc1428a1500e3447af309cda6eef45cb86fa1091fe8d90cf0d7e0120f7cc59cc13293d50cea1ce330
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ Gemfile.lock
2
+
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Lenon Marcel
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # SpBus
2
+
3
+ A gem to easily retrieve information about São Paulo bus routes, numbers,
4
+ location, etc using SPTrans web services.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'spbus'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install spbus
19
+
20
+ ## Usage
21
+
22
+ Please take a look at `example.rb` file.
23
+
24
+ ## Contributing
25
+
26
+ 1. Fork it
27
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
28
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
29
+ 4. Push to the branch (`git push origin my-new-feature`)
30
+ 5. Create new Pull Request
31
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/example.rb ADDED
@@ -0,0 +1,25 @@
1
+ require "spbus"
2
+
3
+ crawler = SpBus::Crawler.new
4
+ crawler.fetch_routes
5
+
6
+ puts "Found #{crawler.routes.size} routes"
7
+
8
+ crawler.routes.each do |route|
9
+ puts "Fetching details for route #{route.number}"
10
+
11
+ begin
12
+ route.fetch_details
13
+
14
+ puts <<-EOF
15
+ Origin: #{route.origin}
16
+ Destination: #{route.destination}
17
+ Code for origin: #{route.code_for_origin}
18
+ Code for destination: #{route.code_for_destination}
19
+ EOF
20
+
21
+ rescue SpBus::InconsistentResponse
22
+ puts "Can't fetch details for #{route.number}"
23
+ end
24
+ end
25
+
@@ -0,0 +1,52 @@
1
+ # encoding: utf-8
2
+
3
+ module SpBus
4
+ class Crawler
5
+
6
+ BUS_ROUTES_URL = "http://200.99.150.170/PlanOperWeb/linhaselecionada.asp"
7
+ RESPONSE_VALIDATOR = /Foram encontrados vários resultados para a sua busca/
8
+ ROUTE_NUMBER_MATCHER = /Linha: (?<number>[a-z0-9]+[-][0-9a-z]+)/iu
9
+ ROUTE_NUMBER_PATH = "#contentScroll ul li .linkLinha:first-child"
10
+
11
+ attr_reader :routes
12
+
13
+ def initialize
14
+ @routes = []
15
+ end
16
+
17
+ def fetch_routes
18
+ sptrans_request
19
+
20
+ raise InconsistentResponse unless valid_response?
21
+
22
+ @response.css(ROUTE_NUMBER_PATH).each do |el|
23
+ create_route_from_raw(el.content)
24
+ end
25
+
26
+ true
27
+ end
28
+
29
+ private
30
+
31
+ def sptrans_request
32
+ @response = Nokogiri::HTML(RestClient.get(BUS_ROUTES_URL))
33
+ end
34
+
35
+ def valid_response?
36
+ @response.text =~ RESPONSE_VALIDATOR
37
+ end
38
+
39
+ def clean_raw_route(raw)
40
+ raw.strip.gsub("\u00A0", " ")
41
+ end
42
+
43
+ def create_route_from_raw(str)
44
+ match = ROUTE_NUMBER_MATCHER.match(clean_raw_route(str))
45
+
46
+ if match
47
+ @routes << Route.new(match[:number])
48
+ end
49
+ end
50
+ end
51
+ end
52
+
@@ -0,0 +1,44 @@
1
+ # encoding: utf-8
2
+
3
+ module SpBus
4
+ class Route
5
+
6
+ BUS_ROUTE_DETAILS = "http://200.189.189.54/InternetServices/BuscaLinhasSIM"
7
+
8
+ attr_reader :number, :origin, :destination, :code_for_origin,
9
+ :code_for_destination
10
+
11
+ def initialize(number)
12
+ @number = number
13
+ end
14
+
15
+ def fetch_details
16
+ sptrans_request
17
+
18
+ raise InconsistentResponse unless valid_response?
19
+
20
+ @origin = @response[0][:DenominacaoTSTP]
21
+ @destination = @response[1][:DenominacaoTPTS]
22
+ @code_for_origin = @response[0][:CodigoLinha]
23
+ @code_for_destination = @response[1][:CodigoLinha]
24
+
25
+ true
26
+ end
27
+
28
+ private
29
+
30
+ def sptrans_request
31
+ @response = JSON.parse(
32
+ RestClient.get(BUS_ROUTE_DETAILS, params: { termosBusca: number }),
33
+ symbolize_names: true
34
+ )[:BuscaLinhasSIMResult]
35
+ end
36
+
37
+ def valid_response?
38
+ !@response.nil? && @response.size == 2 &&
39
+ @response[0][:DenominacaoTSTP] == @response[1][:DenominacaoTSTP] &&
40
+ @response[0][:DenominacaoTPTS] == @response[1][:DenominacaoTPTS]
41
+ end
42
+ end
43
+ end
44
+
@@ -0,0 +1,3 @@
1
+ module SpBus
2
+ VERSION = "0.0.1"
3
+ end
data/lib/spbus.rb ADDED
@@ -0,0 +1,11 @@
1
+ require "rest_client"
2
+ require "nokogiri"
3
+ require "json"
4
+ require "spbus/version"
5
+ require "spbus/crawler"
6
+ require "spbus/route"
7
+
8
+ module SpBus
9
+ class InconsistentResponse < StandardError ; end
10
+ end
11
+
data/spbus.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # encoding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'spbus/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "spbus"
8
+ spec.version = SpBus::VERSION
9
+ spec.authors = ["Lenon Marcel"]
10
+ spec.email = ["lenon.marcel@gmail.com"]
11
+ spec.description = %q{Easily retrieve information about São Paulo bus routes}
12
+ spec.summary = %q{A gem to easily retrieve information about São Paulo bus routes, numbers, location, etc using SPTrans web services.}
13
+ spec.homepage = "http://github.com/lenon/spbus"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "webmock"
25
+ spec.add_development_dependency "vcr"
26
+
27
+ spec.add_dependency "nokogiri"
28
+ spec.add_dependency "rest-client"
29
+ end
30
+