transit_in_ua 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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 transit_in_ua.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Leonid Shevtsov
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.
@@ -0,0 +1,31 @@
1
+ # Ruby API for transit.in.ua
2
+
3
+ This gem provides a Ruby interface to transit.in.ua's awesome database.
4
+
5
+ Fair use policy is greatly encouraged.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'transit_in_ua'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install transit_in_ua
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,6 @@
1
+ require "transit_in_ua/version"
2
+ require "transit_in_ua/client"
3
+
4
+ module TransitInUa
5
+
6
+ end
@@ -0,0 +1,4 @@
1
+ module TransitInUa
2
+ class City
3
+ end
4
+ end
@@ -0,0 +1,78 @@
1
+ require 'mechanize'
2
+ require 'json'
3
+
4
+ module TransitInUa
5
+ class Client
6
+ BASE_URL = "http://transit.in.ua"
7
+
8
+ def get_cities
9
+ get_index unless @cities
10
+ @cities
11
+ end
12
+
13
+ def get_types
14
+ get_index unless @types
15
+ @types
16
+ end
17
+
18
+ def get_routes
19
+ get_index unless @routes
20
+ @routes
21
+ end
22
+
23
+ def get_route(route_id)
24
+ get_routes(Array(route_id))[0]
25
+ end
26
+
27
+ def get_routes(route_ids)
28
+ get_json_data(route_url(route_id))
29
+ end
30
+
31
+ def get_units(route_id)
32
+ get_json_data(units_url(route_id))
33
+ end
34
+
35
+ def route_url(id)
36
+ "#{BASE_URL}/import.php?dataRequest[]=#{id}"
37
+ end
38
+
39
+ def units_url(id)
40
+ "#{BASE_URL}/importTransport.php?dataRequest[]=#{id}"
41
+ end
42
+ private
43
+ def agent
44
+ @agent ||= Mechanize.new
45
+ end
46
+
47
+ def get_json_data(url)
48
+ page = agent.get(url)
49
+ page.body.gsub!("\xEF\xBB\xBF",'')
50
+ page.body.force_encoding('utf-8')
51
+ page.body.strip!
52
+ if page.body == ''
53
+ nil
54
+ else
55
+ JSON.parse(page.body)
56
+ end
57
+ end
58
+
59
+ def get_index
60
+ page = agent.get(BASE_URL)
61
+ city_links = page.parser.css('div.menu-name-menu-route a.city')
62
+ type_links = page.parser.css('div.menu-name-menu-route a.transport')
63
+ route_links = page.parser.css('div.menu-name-menu-route a.number')
64
+
65
+ @cities = {}
66
+ city_links.each {|a| @cities[a[:id].strip] = {name: a.text.strip}}
67
+
68
+ @types = {}
69
+ type_links.each{|a| @types[a[:id].strip] = {name: a.text.strip}}
70
+
71
+ @routes = {}
72
+ route_links.each do |a|
73
+ city, type, _ = a[:id].strip.split('-', 3)
74
+ @routes[a[:id].strip] = {name: a.text.strip, type: type, city: city}
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,6 @@
1
+ module TransitInUa
2
+ class Route
3
+ def initialiize(id)
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,3 @@
1
+ module TransitInUa
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'transit_in_ua/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "transit_in_ua"
8
+ gem.version = TransitInUa::VERSION
9
+ gem.authors = ["Leonid Shevtsov"]
10
+ gem.email = ["leonid@shevtsov.me"]
11
+ gem.description = %q{Ruby API for transit.in.ua}
12
+ gem.summary = %q{Ruby API for transit.in.ua}
13
+ #gem.homepage = ""
14
+
15
+ gem.add_runtime_dependency 'mechanize', '~>2'
16
+
17
+ gem.add_development_dependency 'rake'
18
+
19
+ gem.files = `git ls-files`.split($/)
20
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
21
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
22
+ gem.require_paths = ["lib"]
23
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: transit_in_ua
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Leonid Shevtsov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mechanize
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2'
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: '2'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Ruby API for transit.in.ua
47
+ email:
48
+ - leonid@shevtsov.me
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - lib/transit_in_ua.rb
59
+ - lib/transit_in_ua/city.rb
60
+ - lib/transit_in_ua/client.rb
61
+ - lib/transit_in_ua/route.rb
62
+ - lib/transit_in_ua/version.rb
63
+ - transit_in_ua.gemspec
64
+ homepage:
65
+ licenses: []
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ segments:
77
+ - 0
78
+ hash: -912574689748329736
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ! '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ segments:
86
+ - 0
87
+ hash: -912574689748329736
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 1.8.24
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: Ruby API for transit.in.ua
94
+ test_files: []