kanazawa_loop_bus 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in kanazawa_loop_bus.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Keisuke KITA
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,42 @@
1
+ # KanazawaLoopBus
2
+
3
+ 石川県金沢市の金沢周遊バスの位置を取得できます.
4
+ データは次のサイトから取得しています. http://vbusloc.hokutetsu.co.jp/bus/?rno=1&lang=jp
5
+ サイトの構造が変化するとデータが取得できなくなるので注意してください.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'kanazawa_loop_bus'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install kanazawa_loop_bus
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ service = KanazawaLoopBus::BusLocationService.new
25
+
26
+ service.fetch_time # データを取得した時間
27
+ service.buses[0].name # バスの名前(鏡花号,秋声号,犀星号).
28
+
29
+ # バスのおおよその位置を取得
30
+ service.buses[0].prev_stop.name # 前のバス停の名前
31
+ service.buses[0].next_stop.name # 次のバス停の名前
32
+
33
+ service.buses[0].next_stop.next.name # 次のバス停のさらに次のバス停の名前
34
+ ```
35
+
36
+ ## Contributing
37
+
38
+ 1. Fork it
39
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
40
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
41
+ 4. Push to the branch (`git push origin my-new-feature`)
42
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'kanazawa_loop_bus/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "kanazawa_loop_bus"
8
+ gem.version = KanazawaLoopBus::VERSION
9
+ gem.authors = ["Keisuke KITA"]
10
+ gem.email = ["kei.kita2501@gmail.com"]
11
+ gem.description = %q{search Kanazawa Loop Bus location}
12
+ gem.summary = %q{search Kanazawa Loop Bus location}
13
+ gem.homepage = "https://github.com/kitak/kanazawa_loop_bus"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_runtime_dependency 'nokogiri'
21
+ end
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+
3
+ module KanazawaLoopBus
4
+ class Bus
5
+ attr_reader :previous_stop, :next_stop
6
+
7
+ NAMES = [
8
+ "鏡花号",
9
+ "鏡花号",
10
+ "秋声号",
11
+ "犀星号"
12
+ ]
13
+
14
+ class << self
15
+ def from_node(node)
16
+ node = node.parent.parent
17
+ id = /\.\.\/img\/busicon\/car_s_(\d)\.gif/.match(node["src"]).to_a[1].to_i or raise "Bus not found"
18
+ Bus.new({
19
+ id: id-1,
20
+ previous_stop_name: node.previous.css('font').inner_text,
21
+ next_stop_name: node.next.css('font').inner_text
22
+ })
23
+ end
24
+ end
25
+
26
+ def initialize(option)
27
+ @id = option[:id]
28
+ @previous_stop = BusStop.from_name(option[:previous_stop_name])
29
+ @next_stop = BusStop.from_name(option[:next_stop_name])
30
+ end
31
+
32
+ def name
33
+ NAMES[@id]
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,41 @@
1
+ # coding: utf-8
2
+ require 'open-uri'
3
+ require 'nokogiri'
4
+
5
+ module KanazawaLoopBus
6
+ class BusLocationService
7
+ attr_reader :fetch_time
8
+
9
+ def initialize
10
+ fetch
11
+ end
12
+
13
+ def fetch
14
+ @doc = Nokogiri::HTML(fetch_html)
15
+ raise "Service Finished" if service_finished?
16
+ @fetch_time = Time.now
17
+ end
18
+ alias_method :refetch, :fetch
19
+
20
+ def buses
21
+ @doc.xpath('//td/img').select { |img|
22
+ /\.\.\/img\/busicon\/car_s_.\.gif/ =~ img["src"]
23
+ }.map { |img|
24
+ Bus.from_node(img)
25
+ }
26
+ end
27
+
28
+ private
29
+ def fetch_html
30
+ open('http://vbusloc.hokutetsu.co.jp/bus/?rno=1&lang=jp').read
31
+ .encode("UTF-8", "Shift_JIS",
32
+ invalid: :replace, undef: :replace, replace: "")
33
+ end
34
+
35
+ def service_finished?
36
+ @doc.css('//td/img').select { |img|
37
+ /\.\.\/img\/busicon\/car_s_.\.gif/ =~ img["src"]
38
+ }.empty?
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,51 @@
1
+ # coding: utf-8
2
+
3
+ module KanazawaLoopBus
4
+ class BusStop
5
+ NAMES = [
6
+ "L0.金沢駅東口",
7
+ "L1.リファーレ前",
8
+ "L2.明成小学校前",
9
+ "L3.小橋",
10
+ "L4.馬場児童公園",
11
+ "L5.森山1丁目",
12
+ "L6.橋場町(交番前)",
13
+ "L7.橋場町(金城楼前)",
14
+ "L8.兼六元町",
15
+ "L9.兼六園下(石川門向い)",
16
+ "L10.広坂(石浦神社前)",
17
+ "L11.本多町",
18
+ "L12.犀星文学碑前",
19
+ "L13.十三間町",
20
+ "L14.片町(ラブロ前)",
21
+ "L15.香林坊(日銀前)",
22
+ "L16.香林坊(東横イン前)",
23
+ "L17.南町",
24
+ "L18.武蔵ヶ辻(めいてつエムザ前)",
25
+ "L19.リファーレ前"
26
+ ]
27
+
28
+ class << self
29
+ def from_name(name)
30
+ id = NAMES.index(name) or raise "BusStop not found"
31
+ BusStop.new(id)
32
+ end
33
+ end
34
+
35
+ def initialize(id)
36
+ @id = id
37
+ end
38
+
39
+ def name
40
+ NAMES[@id]
41
+ end
42
+
43
+ def next
44
+ BusStop.new((@id+1) % NAMES.size)
45
+ end
46
+
47
+ def prev
48
+ BusStop.new((@id-1) % NAMES.size)
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,3 @@
1
+ module KanazawaLoopBus
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,14 @@
1
+ require "kanazawa_loop_bus/version"
2
+ require "kanazawa_loop_bus/bus_stop"
3
+ require "kanazawa_loop_bus/bus"
4
+ require "kanazawa_loop_bus/bus_location_service"
5
+
6
+ module KanazawaLoopBus
7
+ end
8
+
9
+ if __FILE__ == $0
10
+ service = KanazawaLoopBus::BusLocationService.new
11
+ puts service.fetch_time
12
+ puts service.buses[0].name
13
+ puts service.buses[0].next_stop.name
14
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kanazawa_loop_bus
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Keisuke KITA
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: search Kanazawa Loop Bus location
31
+ email:
32
+ - kei.kita2501@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - kanazawa_loop_bus.gemspec
43
+ - lib/kanazawa_loop_bus.rb
44
+ - lib/kanazawa_loop_bus/bus.rb
45
+ - lib/kanazawa_loop_bus/bus_location_service.rb
46
+ - lib/kanazawa_loop_bus/bus_stop.rb
47
+ - lib/kanazawa_loop_bus/version.rb
48
+ homepage: https://github.com/kitak/kanazawa_loop_bus
49
+ licenses: []
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 1.8.23
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: search Kanazawa Loop Bus location
72
+ test_files: []
73
+ has_rdoc: