uk_buses 0.0.2 → 0.5

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: d4805f4887607804abe20dae87c22fe79acbf950
4
- data.tar.gz: 32626cdee3fb531cf1e505a3b2f9bb974c9d2639
3
+ metadata.gz: 6cfa62662695c6e4e22febb44997d22ae276ee9e
4
+ data.tar.gz: cf1c691507e203b60423084b8a2f13f7fd4c33d0
5
5
  SHA512:
6
- metadata.gz: 0bbdde79b78c7f60fb47c3fd636493423b4c177fe2f600a4f0fb0e8301f3390b7332e1c90bbb092300b32c1d6ca4e39c08d420fd347986a7c2b407e7f8f1bcc9
7
- data.tar.gz: 63c850cb25bed477247fdcfe2c9e53bee213fa831042d61f1437064cf76e9b9d14f8e4951bd520eb4174900416643975f2632d7ebd8d5112b2c628208290c68b
6
+ metadata.gz: beb02f3754d626c50379fe5e3d4d1a45a6a1725da9d98e178d7015d92b6cb59cdb82684daa4d9b91e1d37fe3bb569d20326371b364bc9f44524a07dab1389137
7
+ data.tar.gz: 0585de01e573357570ebea9a6805e3f0532365f1f70ef1529f20b42bcee3f3325886c4a8b486617393c3e891c01840674ca3728fe5393635bb1703b87a60b7c7
data/Guardfile CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
  guard 'rspec', :cli => "--color --format nested" do
5
5
  watch(%r{^spec/.+_spec\.rb$})
6
- watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
6
+ watch(%r{^lib/uk_buses/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
7
7
  watch('spec/spec_helper.rb') { "spec" }
8
8
  end
9
9
 
data/README.md CHANGED
@@ -7,41 +7,30 @@ Grab *real-time* bus information across the entire United Kingdom.
7
7
 
8
8
  ## Installation
9
9
 
10
- Add this line to your application's Gemfile:
10
+ Add it to your Gemfile:
11
11
 
12
- gem 'uk_buses'
12
+ `gem 'uk_buses'`
13
13
 
14
- And then execute:
15
-
16
- $ bundle
17
-
18
- Or install it yourself as:
19
-
20
- $ gem install uk_buses
21
-
22
- ## Usage
23
-
24
- Using UKBuses couldn't be simpler.
25
-
26
- Firstly, require it at the top of your file:
14
+ ## Use it
27
15
 
28
16
  require 'uk_buses'
17
+ ...
18
+ UkBuses::Query.new('cditmpa').fetch_buses
29
19
 
30
- Create a `Query` object containing the bus stop code. This is normally the code written on bus stops that you text to a number to get real-time updates. They usually begin with a 2 letter code that represents your area, `cd` for Cardiff, `bn` for Brighton & Hove etc.
31
-
32
- q = UkBuses::Query.new('cdijtgm')
20
+ #=>
21
+ [#<UkBuses::Bus:0x007f8fe31aa2f0 @route_number="39", @destination="Heath Hospital", @arrives="4 mins">, #<UkBuses::Bus:0x007f8fe31a9aa8 @route_number="38", @destination="Heath, Childrens Unit", @arrives="20:45">, #<UkBuses::Bus:0x007f8fe31a9260 @route_number="39", @destination="Heath Hospital", @arrives="34 mins">, #<UkBuses::Bus:0x007f8fe31a8a18 @route_number="38", @destination="Heath, Childrens Unit", @arrives="21:15">, #<UkBuses::Bus:0x007f8fe31a81d0 @route_number="39", @destination="Heath Hospital", @arrives="21:30">, #<UkBuses::Bus:0x007f8fe38af780 @route_number="38", @destination="Heath, Childrens Unit", @arrives="21:45">, #<UkBuses::Bus:0x007f8fe38aef10 @route_number="39", @destination="Heath Hospital", @arrives="22:00">, #<UkBuses::Bus:0x007f8fe38ade58 @route_number="38", @destination="Heath, Childrens Unit", @arrives="22:15">, #<UkBuses::Bus:0x007f8fe38acf58 @route_number="39", @destination="Heath Hospital", @arrives="22:30">, #<UkBuses::Bus:0x007f8fe38ac710 @route_number="38", @destination="Heath, Childrens Unit", @arrives="22:45">]
33
22
 
34
- Then execute the query to scrape the data:
35
- This happens automatically.
36
- **This can take a little while!**
23
+ **That's it.**
37
24
 
38
- It will return an array of `UkBuses::Bus` objects.
25
+ ## Is this ok?
39
26
 
40
- That's it!
27
+ I don't know. This was a weekend hack project; there's no intention here to commercialise anything or commit any crime. This data is available in the **public domain**, on publically accessible websites, this gem just displays the data in a more geek-friendly format. But I'm aware that some companies aren't too keen on this.
28
+ Use of the MIT licence implies that you use this gem at your own risk.
41
29
 
42
- UkBuses doesn't implement any caching, but it is designed to pull real-time data so it won't be required unless you're servicing thousands of requests.
30
+ My views mirror that of ScraperWiki:
43
31
 
44
- This is a 0.0.1 release currently so Pull Requests are greatfully received.
32
+ *It is our view that, where a web server responds to an unauthenticated HTTP request, there is an implied licence to use the HTML that is returned for reading and automatically extracting that information. This, in our view, is how the web is designed to operate. If the proprietor of a web host wishes (for example) to charge for use of their site, HTTP provides mechanisms to require payment or authentication for use. They may also make use of the robots exclusion protocol to prevent scraping and spidering of any kind.
33
+ Of course we may be wrong about this. The question has not been tested in any UK court and, we understand, there is not much more clarity world-wide. If you are in doubt about whether what you are doing is lawful, you should seek your own legal advice, rather than relying on our best guess.*
45
34
 
46
35
  ## Contributing
47
36
 
@@ -0,0 +1,131 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: http://nextbuses.mobi/WebView/BusStopSearch/BusStopSearchResults/cditmpa
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ Accept-Encoding:
11
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
12
+ Accept:
13
+ - '*/*'
14
+ User-Agent:
15
+ - Ruby
16
+ response:
17
+ status:
18
+ code: 200
19
+ message: OK
20
+ headers:
21
+ Cache-Control:
22
+ - private
23
+ Content-Type:
24
+ - text/html; charset=utf-8
25
+ Vary:
26
+ - Accept-Encoding
27
+ Server:
28
+ - Microsoft-IIS/8.0
29
+ X-Aspnetmvc-Version:
30
+ - '2.0'
31
+ X-Aspnet-Version:
32
+ - 2.0.50727
33
+ X-Powered-By:
34
+ - ASP.NET
35
+ Date:
36
+ - Mon, 20 May 2013 19:09:09 GMT
37
+ Content-Length:
38
+ - '1404'
39
+ body:
40
+ encoding: UTF-8
41
+ string: "\r\n\r\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
42
+ \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\r\n<html xmlns=\"http://www.w3.org/1999/xhtml\"
43
+ xml:lang=\"en\">\r\n<head id=\"ctl00_Head1\">\r\n <link href=\"/Assets/site.css\"
44
+ rel=\"stylesheet\" type=\"text/css\" />\r\n <meta name=\"viewport\"
45
+ content=\"width=device-width, initial-scale=1.0, target-densitydpi=device-dpi\"
46
+ />\r\n <link rel=\"shortcut icon\" href=\"/Assets/Images/favicon.ico\"
47
+ type=\"image/x-icon\" />\r\n <meta name=\"HandheldFriendly\" content=\"true\"
48
+ />\r\n \r\n<title>\r\n\tNextBuses | Bus Stop Search Results\r\n</title></head>\r\n<body
49
+ id=\"ctl00_nextBuses\">\r\n <div id=\"wrapper\">\r\n <div id=\"nbHeader\"
50
+ class=\"header\">\r\n <div class=\"nbLogo\"></div>\r\n <div
51
+ class=\"tlLogo\"></div>\r\n <div class=\"clear\"></div>\r\n </div>\r\n
52
+ \ <div class=\"top-menu\">\r\n <a class=\"active\" href=\"/WebView/BusStopSearch/Index\">Home</a>\r\n
53
+ \ <a href=\"/WebView/Feedback/Feedback\">Feedback</a>\r\n <a
54
+ href=\"/WebView/Help/Help\">Info</a>\r\n </div>\r\n <div class=\"top-menu-ds\"></div>\r\n
55
+ \ <div class=\"content\">\r\n \r\n \r\n <!-- Departure
56
+ Board Results -->\r\n <h2>Departures</h2> \r\n <h5>\r\n
57
+ \ 20/05/2013 20:09:09<br />\r\n Churchill Way
58
+ (Stop HQ), Cardiff\r\n </h5>\r\n \r\n <p
59
+ class=\"Stops\"><a href=\"/WebView/BusStopSearch/BusStopSearchResults/cditmpa\">[Refresh
60
+ Departures]</a></p>\r\n \r\n <table class=\"BusStops\"
61
+ style=\"width:794px;\">\r\n \r\n <tr>\r\n
62
+ \ <td class=\"Number\">\r\n <p
63
+ class=\"Stops\"><a href=\"/WebView/BusStopSearch/BusStopSearchResults/cditmpa?searchType=route&amp;searchFilter=38\">38</a></p>\r\n
64
+ \ </td>\r\n <td>\r\n <p
65
+ class=\"Stops\">Heath Hospital&nbsp;in 5 mins</p>\r\n </td>\r\n
66
+ \ </tr>\r\n \r\n <tr>\r\n
67
+ \ <td class=\"Number\">\r\n <p
68
+ class=\"Stops\"><a href=\"/WebView/BusStopSearch/BusStopSearchResults/cditmpa?searchType=route&amp;searchFilter=39\">39</a></p>\r\n
69
+ \ </td>\r\n <td>\r\n <p
70
+ class=\"Stops\">Heath Hospital&nbsp;in 20 mins</p>\r\n </td>\r\n
71
+ \ </tr>\r\n \r\n <tr>\r\n
72
+ \ <td class=\"Number\">\r\n <p
73
+ class=\"Stops\"><a href=\"/WebView/BusStopSearch/BusStopSearchResults/cditmpa?searchType=route&amp;searchFilter=38\">38</a></p>\r\n
74
+ \ </td>\r\n <td>\r\n <p
75
+ class=\"Stops\">Heath Hospital&nbsp;at 20:45</p>\r\n </td>\r\n
76
+ \ </tr>\r\n \r\n <tr>\r\n
77
+ \ <td class=\"Number\">\r\n <p
78
+ class=\"Stops\"><a href=\"/WebView/BusStopSearch/BusStopSearchResults/cditmpa?searchType=route&amp;searchFilter=39\">39</a></p>\r\n
79
+ \ </td>\r\n <td>\r\n <p
80
+ class=\"Stops\">Heath Hospital&nbsp;at 21:00</p>\r\n </td>\r\n
81
+ \ </tr>\r\n \r\n <tr>\r\n
82
+ \ <td class=\"Number\">\r\n <p
83
+ class=\"Stops\"><a href=\"/WebView/BusStopSearch/BusStopSearchResults/cditmpa?searchType=route&amp;searchFilter=38\">38</a></p>\r\n
84
+ \ </td>\r\n <td>\r\n <p
85
+ class=\"Stops\">Heath Hospital&nbsp;at 21:15</p>\r\n </td>\r\n
86
+ \ </tr>\r\n \r\n <tr>\r\n
87
+ \ <td class=\"Number\">\r\n <p
88
+ class=\"Stops\"><a href=\"/WebView/BusStopSearch/BusStopSearchResults/cditmpa?searchType=route&amp;searchFilter=39\">39</a></p>\r\n
89
+ \ </td>\r\n <td>\r\n <p
90
+ class=\"Stops\">Heath Hospital&nbsp;at 21:30</p>\r\n </td>\r\n
91
+ \ </tr>\r\n \r\n <tr>\r\n
92
+ \ <td class=\"Number\">\r\n <p
93
+ class=\"Stops\"><a href=\"/WebView/BusStopSearch/BusStopSearchResults/cditmpa?searchType=route&amp;searchFilter=38\">38</a></p>\r\n
94
+ \ </td>\r\n <td>\r\n <p
95
+ class=\"Stops\">Heath Hospital&nbsp;at 21:45</p>\r\n </td>\r\n
96
+ \ </tr>\r\n \r\n <tr>\r\n
97
+ \ <td class=\"Number\">\r\n <p
98
+ class=\"Stops\"><a href=\"/WebView/BusStopSearch/BusStopSearchResults/cditmpa?searchType=route&amp;searchFilter=39\">39</a></p>\r\n
99
+ \ </td>\r\n <td>\r\n <p
100
+ class=\"Stops\">Heath Hospital&nbsp;at 22:00</p>\r\n </td>\r\n
101
+ \ </tr>\r\n \r\n <tr>\r\n
102
+ \ <td class=\"Number\">\r\n <p
103
+ class=\"Stops\"><a href=\"/WebView/BusStopSearch/BusStopSearchResults/cditmpa?searchType=route&amp;searchFilter=38\">38</a></p>\r\n
104
+ \ </td>\r\n <td>\r\n <p
105
+ class=\"Stops\">Heath Hospital&nbsp;at 22:15</p>\r\n </td>\r\n
106
+ \ </tr>\r\n \r\n <tr>\r\n
107
+ \ <td class=\"Number\">\r\n <p
108
+ class=\"Stops\"><a href=\"/WebView/BusStopSearch/BusStopSearchResults/cditmpa?searchType=route&amp;searchFilter=39\">39</a></p>\r\n
109
+ \ </td>\r\n <td>\r\n <p
110
+ class=\"Stops\">Heath Hospital&nbsp;at 22:30</p>\r\n </td>\r\n
111
+ \ </tr>\r\n \r\n </table>\r\n
112
+ \ \r\n <div class=\"clear\"></div>\r\n \r\n
113
+ \ <!-- MAP -->\r\n <p class=\"Stops\">\r\n <a
114
+ href=\"/WebView/BusStopSearch/BusStopSearchResults/cditmpa?searchMap=true\">[Show
115
+ Stop on Map]</a>\r\n </p> \r\n \r\n <p
116
+ class=\"Stops\">\r\n <a href=\"/WebView/BusStopSearch/BusStopSearchResults/ll_51.4814,-3.17261~Churchill%20Way,%20Cardiff\">[Show
117
+ Nearby Stops]</a>\r\n </p>\r\n \r\n <table
118
+ id=\"tblPaging\" class=\"Paging\" style=\"width: 794px;\">\r\n \r\n
119
+ \ <tr>\r\n <td class=\"Top\" colspan=\"3\"></td>\r\n
120
+ \ </tr>\r\n <tr>\r\n <td
121
+ class=\"Side\"><a class=\"Zoom Left invisible\" href=\"/WebView/BusStopSearch/BusStopSearchResults/cditmpa?currentPage=-1\">Previous</a></td>\r\n
122
+ \ <td></td>\r\n <td class=\"Side\"><a
123
+ class=\"Zoom Right visible\" href=\"/WebView/BusStopSearch/BusStopSearchResults/cditmpa?currentPage=1\">Next</a></td>\r\n
124
+ \ </tr>\r\n <tr>\r\n <td
125
+ class=\"Top\" colspan=\"3\"></td>\r\n </tr>\r\n </table>
126
+ \r\n <div class=\"clear\"></div>\r\n \r\n <div
127
+ class=\"clear\"></div>\r\n \r\n </div>\r\n <div class=\"footer\"></div>\r\n
128
+ \ </div>\r\n</body>\r\n</html>\r\n"
129
+ http_version:
130
+ recorded_at: Mon, 20 May 2013 19:09:03 GMT
131
+ recorded_with: VCR 2.5.0
@@ -4,15 +4,28 @@ require 'open-uri'
4
4
  module UkBuses
5
5
  class Query
6
6
 
7
+ attr_accessor :stop_code
8
+
7
9
  def initialize(stop_code)
8
- fetch_buses(stop_code)
10
+ @stop_code = stop_code
11
+ end
12
+
13
+ def url
14
+ "http://nextbuses.mobi/WebView/BusStopSearch/BusStopSearchResults/#{ @stop_code }"
15
+ end
16
+
17
+ def xpath
18
+ '//*[@id="wrapper"]/div[4]/table[1]/tr'
19
+ end
20
+
21
+ def get_document
22
+ Nokogiri::HTML(open(url))
9
23
  end
10
24
 
11
- def fetch_buses(stop_code)
25
+ def fetch_buses
12
26
  buses = []
13
- doc = Nokogiri::HTML(open("http://nextbuses.mobi/WebView/BusStopSearch/BusStopSearchResults/#{ stop_code }"))
14
27
 
15
- doc.xpath('//*[@id="wrapper"]/div[4]/table[1]/tr').each do |row|
28
+ get_document.xpath(xpath).each do |row|
16
29
  current_bus = {}
17
30
  row.children.each do |child|
18
31
 
@@ -1,3 +1,3 @@
1
1
  module UkBuses
2
- VERSION = "0.0.2"
2
+ VERSION = "0.5"
3
3
  end
@@ -0,0 +1,78 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'initialize' do
4
+
5
+ it 'should create a new Query object' do
6
+ b = UkBuses::Query.new("atestcode")
7
+ expect(b.class).to eq(UkBuses::Query)
8
+ end
9
+
10
+ it 'should assign the stop_code to the object' do
11
+ b = UkBuses::Query.new("atestcode")
12
+ expect(b.stop_code).to eq('atestcode')
13
+ end
14
+
15
+ end
16
+
17
+ describe 'fetch_buses' do
18
+
19
+ it 'should return an Array' do
20
+ VCR.use_cassette('response') do
21
+ b = UkBuses::Query.new('cditmpa')
22
+ expect(b.fetch_buses.class).to eq(Array)
23
+ end
24
+ end
25
+
26
+ it 'should return an Array of Bus objects' do
27
+ VCR.use_cassette('response') do
28
+ b = UkBuses::Query.new('cditmpa')
29
+ expect(b.fetch_buses.first.class).to eq(UkBuses::Bus)
30
+ end
31
+ end
32
+
33
+ end
34
+
35
+ describe 'url' do
36
+
37
+ it 'should return the base URL with the stopcode appended' do
38
+ b = UkBuses::Query.new('cditmpa')
39
+ expect(b.url).to eq('http://nextbuses.mobi/WebView/BusStopSearch/BusStopSearchResults/cditmpa')
40
+ end
41
+
42
+ end
43
+
44
+ describe 'xpath' do
45
+
46
+ it 'should return an XPath string' do
47
+ b = UkBuses::Query.new('cditmpa')
48
+ expect(b.xpath).to eq('//*[@id="wrapper"]/div[4]/table[1]/tr')
49
+ end
50
+
51
+ end
52
+
53
+ describe 'get_document' do
54
+
55
+ it 'should return a Nokogiri::Document object' do
56
+ VCR.use_cassette('response') do
57
+ b = UkBuses::Query.new('cditmpa')
58
+ expect(b.get_document.class).to eq(Nokogiri::HTML::Document)
59
+ end
60
+ end
61
+
62
+ end
63
+
64
+
65
+
66
+
67
+
68
+
69
+
70
+
71
+
72
+
73
+
74
+
75
+
76
+
77
+
78
+
data/spec/spec_helper.rb CHANGED
@@ -2,6 +2,13 @@ require 'rubygems'
2
2
  require 'bundler/setup'
3
3
 
4
4
  require 'uk_buses'
5
+ require 'vcr'
6
+
7
+ VCR.configure do |c|
8
+ c.cassette_library_dir = 'fixtures/vcr_cassettes'
9
+ c.hook_into :webmock
10
+ c.allow_http_connections_when_no_cassette = true
11
+ end
5
12
 
6
13
  RSpec.configure do |config|
7
14
  # some (optional) config here
data/uk_buses.gemspec CHANGED
@@ -21,4 +21,6 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency "bundler", "~> 1.3"
22
22
  spec.add_development_dependency "rake"
23
23
  spec.add_dependency "nokogiri"
24
+ spec.add_development_dependency "vcr"
25
+ spec.add_development_dependency "webmock"
24
26
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uk_buses
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: '0.5'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Max Woolf
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-05-19 00:00:00.000000000 Z
11
+ date: 2013-05-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,34 @@ dependencies:
52
52
  - - '>='
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: vcr
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
55
83
  description: Web scraper for all UK bus data
56
84
  email:
57
85
  - maximus@zingysaturn.co.uk
@@ -66,12 +94,14 @@ files:
66
94
  - LICENSE.txt
67
95
  - README.md
68
96
  - Rakefile
97
+ - fixtures/vcr_cassettes/response.yml
69
98
  - lib/.DS_Store
70
99
  - lib/uk_buses.rb
71
100
  - lib/uk_buses/bus.rb
72
101
  - lib/uk_buses/query.rb
73
102
  - lib/uk_buses/version.rb
74
103
  - spec/bus_spec.rb
104
+ - spec/query_spec.rb
75
105
  - spec/spec_helper.rb
76
106
  - uk_buses.gemspec
77
107
  homepage: http://maxehmookau.github.io
@@ -100,4 +130,5 @@ specification_version: 4
100
130
  summary: Grab real-time bus info for all UK buses using a groovy Ruby DSL
101
131
  test_files:
102
132
  - spec/bus_spec.rb
133
+ - spec/query_spec.rb
103
134
  - spec/spec_helper.rb