spbus 0.0.5 → 0.0.6

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: 967e385a3d92d07dd853710b0188e3e3168c769f
4
- data.tar.gz: a8d5298c3d4ca70a90174fc2cc3f55b6727a5bdc
3
+ metadata.gz: 7f63128061498523a2de0d0f08f42d12a6b18a32
4
+ data.tar.gz: 833ec14a34cb6db858fe91c3db826dbb466796ca
5
5
  SHA512:
6
- metadata.gz: 57ce40a910a30b278c57b95ca747d47b0796037a8a70b9b595f63ad7c665f1132251fa4032fa7da8a8c09311c4b4bbad560ac51914c692eaad4f1713fda10cb4
7
- data.tar.gz: 1961eef273d0887811e97fcc292d1f9ddcb19e2ec91ed37fe27b53b3f57b355a5b2851fb1788510b43df96389b5fe037354ed0eca1e231c05c0c5e1acccf4c0d
6
+ metadata.gz: 5f5fc8f1465d8c73d6e9524cd1e43e8ae8f13f048451d9f35b2e2baa28002589a38b4cfa9e4b05b8cf37346a72b0441585287c77bc22ff7ee570a33df2b5a239
7
+ data.tar.gz: 539481e90fb7e8b23909dc6f3866f7f9ded8bba84940ec284225a6bde2f6bf0814c7a76b2707adf544428576c1abddf69d838d1eb37044ea99d3e7a62aeb44bd
data/lib/spbus/request.rb CHANGED
@@ -1,18 +1,12 @@
1
1
  module SpBus
2
2
  class Request
3
3
 
4
- AUTH_URL = "http://olhovivo.sptrans.com.br/"
5
-
6
- attr_writer :authenticated
7
-
8
4
  def initialize(url)
9
5
  @url = url
10
- @authenticated = true
11
6
  end
12
7
 
13
8
  def get
14
9
  tries ||= 3
15
- get_cookies if @authenticated
16
10
  open(@url, request_headers).read
17
11
  rescue Errno::ETIMEDOUT => e
18
12
  raise e if (tries -= 1) == 0
@@ -21,13 +15,9 @@ module SpBus
21
15
 
22
16
  private
23
17
 
24
- def get_cookies
25
- @cookies = open(AUTH_URL).meta["set-cookie"]
26
- end
27
-
28
18
  def request_headers
29
19
  headers = {}
30
- headers["Cookie"] = @cookies if @authenticated
20
+ headers["User-Agent"] = "SpBus/#{SpBus::VERSION}"
31
21
  headers
32
22
  end
33
23
  end
@@ -1,7 +1,7 @@
1
1
  module SpBus::Scrapers
2
2
  class Locations
3
3
 
4
- URL = "http://olhovivo.sptrans.com.br/v0/Posicao"
4
+ URL = "http://200.189.189.54/InternetServices/PosicaoLinha"
5
5
 
6
6
  attr_reader :buses
7
7
 
@@ -11,7 +11,7 @@ module SpBus::Scrapers
11
11
 
12
12
  def fetch
13
13
  doc = SpBus::Request.new(url_with_params).get
14
- @json = JSON.parse(doc, symbolize_names: true)
14
+ @json = JSON.parse(doc, symbolize_names: true)[:PosicaoLinhaResult]
15
15
 
16
16
  validate_response
17
17
 
@@ -1,7 +1,7 @@
1
1
  module SpBus::Scrapers
2
2
  class RouteDetails
3
3
 
4
- URL = "http://olhovivo.sptrans.com.br/v0/Linha/Buscar"
4
+ URL = "http://200.189.189.54/InternetServices/BuscaLinhasSIM"
5
5
 
6
6
  def initialize(route)
7
7
  @route = route
@@ -9,7 +9,7 @@ module SpBus::Scrapers
9
9
 
10
10
  def fetch
11
11
  doc = SpBus::Request.new(url_with_params).get
12
- @json = JSON.parse(doc, symbolize_names: true)
12
+ @json = JSON.parse(doc, symbolize_names: true)[:BuscaLinhasSIMResult]
13
13
 
14
14
  validate_response
15
15
  build_route
@@ -35,7 +35,7 @@ module SpBus::Scrapers
35
35
  @json.size != 2 ||
36
36
  @json[0][:DenominacaoTSTP] != @json[1][:DenominacaoTSTP] ||
37
37
  @json[0][:DenominacaoTPTS] != @json[1][:DenominacaoTPTS]
38
- raise SpBus::UnknownResponse
38
+ raise SpBus::InvalidRoute
39
39
  end
40
40
  end
41
41
 
@@ -26,8 +26,6 @@ module SpBus::Scrapers
26
26
 
27
27
  def make_request
28
28
  request = SpBus::Request.new(ALL_ROUTES_URL)
29
- request.authenticated = false
30
-
31
29
  @doc = Nokogiri::HTML(request.get)
32
30
  end
33
31
 
data/lib/spbus/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module SpBus
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
data/lib/spbus.rb CHANGED
@@ -34,7 +34,11 @@ module SpBus
34
34
 
35
35
  begin
36
36
  Scrapers::RouteDetails.new(route).fetch
37
- true
37
+ if block_given?
38
+ yield route
39
+ else
40
+ true
41
+ end
38
42
  rescue InvalidRoute
39
43
  logger.warn "[INVALID ROUTE] #{route.number}"
40
44
  false