indian-rail 1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in indian-rail.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,26 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ indian-rail (0.0.1)
5
+ nokogiri (~> 1.5.9)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.2.4)
11
+ nokogiri (1.5.9-x86-mingw32)
12
+ rspec (2.13.0)
13
+ rspec-core (~> 2.13.0)
14
+ rspec-expectations (~> 2.13.0)
15
+ rspec-mocks (~> 2.13.0)
16
+ rspec-core (2.13.1)
17
+ rspec-expectations (2.13.0)
18
+ diff-lcs (>= 1.1.3, < 2.0)
19
+ rspec-mocks (2.13.1)
20
+
21
+ PLATFORMS
22
+ x86-mingw32
23
+
24
+ DEPENDENCIES
25
+ indian-rail!
26
+ rspec (~> 2.13.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Kalyan Allampalli
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,29 @@
1
+ # Indian::Rail
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'indian-rail'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install indian-rail
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,36 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'indian-rail/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = %q{indian-rail}
8
+ gem.version = IndianRail::VERSION
9
+ gem.authors = ["Kalyan Allampalli"]
10
+ gem.email = %q{kalyan.allampalli@gmail.com}
11
+ gem.description = %q{Client for the RESTful JSON Indian Rail}
12
+ gem.summary = %q{Indian Rail helps you get all the required information you want to know about Indian Railways.}
13
+ gem.homepage = %q{https://github.com/kalyanallampalli/indian-rail}
14
+
15
+ gem.files = [
16
+ "Gemfile",
17
+ "Gemfile.lock",
18
+ "LICENSE.txt",
19
+ "README.md",
20
+ "Rakefile",
21
+ "lib/indian-rail.rb",
22
+ "lib/indian-rail/version.rb",
23
+ "lib/indian-rail/api.rb",
24
+ "lib/indian-rail/pnr.rb",
25
+ "lib/indian-rail/schedule.rb",
26
+ "indian-rail.gemspec"
27
+ ]
28
+
29
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
30
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
31
+ gem.require_paths = ["lib"]
32
+
33
+ gem.add_dependency "nokogiri", "~> 1.5.9"
34
+ gem.add_development_dependency "rspec", "~> 2.13.0"
35
+
36
+ end
@@ -0,0 +1,40 @@
1
+ require 'rubygems'
2
+ require 'uri'
3
+ require 'nokogiri'
4
+ require 'net/http'
5
+
6
+ module IndianRail
7
+ class Api
8
+ class << self
9
+ def get_response(url, options={})
10
+ response, uri = '', URI.parse(url)
11
+ options = Hash[options.map{|(k,v)| [k.to_sym,v]}]
12
+ net_http = if options.has_key?(:proxy)
13
+ options[:proxy][:url] = URI.parse(options[:proxy][:url].gsub('www.', '')).host if options[:proxy][:url].include?('http://')
14
+ Net::HTTP::Proxy(options[:proxy][:url], options[:proxy][:port])
15
+ else
16
+ Net::HTTP
17
+ end
18
+ net_http.start(uri.host, uri.port) do |http|
19
+ request = Net::HTTP::Post.new(uri.request_uri)
20
+ request.set_form_data(options[:form_params])
21
+ request['referer'] = options[:referer] if options.has_key?(:referer)
22
+ response = http.request(request).body
23
+ end
24
+ response
25
+ end
26
+
27
+ def base_url_prefix
28
+ @base_url ||= "http://www.indianrail.gov.in/cgi_bin"
29
+ end
30
+
31
+ def pnr_url_sufix
32
+ @pnr_url ||= "inet_pnrstat_cgi.cgi"
33
+ end
34
+
35
+ def schedule_url_sufix
36
+ @schedule ||= "inet_trnnum_cgi.cgi"
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,43 @@
1
+ module IndianRail
2
+ class Pnr < Api
3
+ def self.enquiry(pnr=nil, options={})
4
+ response = {}
5
+ response[:message] = 'Please Enter PNR' and return response if pnr.nil?
6
+
7
+ begin
8
+ page_response = get_response([base_url_prefix, pnr_url_sufix].compact.join("/"),
9
+ {:form_params => {'lccp_pnrno1' => pnr},
10
+ :referer => 'http://www.indianrail.gov.in/pnr_stat.html'}.merge!(options))
11
+ response = parse_page(page_response, pnr)
12
+ rescue Exception => e
13
+ response[:message] = "Service is not available - #{e.message}"
14
+ end
15
+ response
16
+ end
17
+
18
+ private
19
+
20
+ # Crawling the response page and scraping the content
21
+ def self.parse_page(body, pnr)
22
+ page = Nokogiri::HTML(body)
23
+ data = {:pnr => pnr}
24
+ rows = [:train_no, :train_name, :boarding_date, :from, :to, :reserved_upto, :boarding_point, :class]
25
+ pass_rows = [:sno, :booking_status, :current_status, :coach_position]
26
+ passenger_list, list, i = {}, [], 0
27
+ page.css('//table[class="table_border"]/tr/td[class="table_border_both"]').each_with_index do |row, index|
28
+ data[rows[index]] = row.text.gsub('*', '').strip if index <= 7
29
+ if index > 7
30
+ row.text.strip.downcase.match('passenger') ? (passenger_list = {}; i=0;) : (i += 1)
31
+ i = -1 if row.text.strip if row.text.strip.upcase.match('CHART')
32
+ passenger_list[pass_rows[i]] = row.text.strip if i <= 3 && i >= 0
33
+ list << passenger_list if i == 2
34
+ data[:charting_status] = row.text.strip if i == -1
35
+ end
36
+ end
37
+
38
+ data[:passenger_list] = list unless list.empty?
39
+ data[:message] = 'FLUSHED PNR / PNR NOT YET GENERATED' if list.empty?
40
+ data
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,75 @@
1
+ module IndianRail
2
+ class Schedule < Api
3
+ def self.find(train_no=nil, options={})
4
+ response = {}
5
+ response[:message] = 'Please Enter Train Number or Name' and return response if train_no.nil?
6
+ begin
7
+ page_response = get_response([base_url_prefix, schedule_url_sufix].compact.join("/"), {:form_params => {'lccp_trnname' => train_no}}.merge!(options))
8
+ response = parse_train_details(page_response, train_no)
9
+ rescue Exception => e
10
+ response[:message] = "Service is not available - #{e.message}"
11
+ end
12
+ response
13
+ end
14
+
15
+ private
16
+
17
+ def self.parse_train_details(body, train_no)
18
+ page = Nokogiri::HTML(body)
19
+ data = {:train_no => train_no}
20
+ index = 1
21
+ tables = page.xpath('//table[@class="table_border_both"]')
22
+ details, train_detail = {}, {}
23
+ runs_on = []
24
+ coach_details, stn_details = [], [['Stn Code', 'Stn Name', 'Route No.', 'Arrival Time', 'Dep. time', 'Halt Time(In Min))', 'Distance', 'Day']]
25
+ tables.collect do |table|
26
+ if index == 1
27
+ table.css("tr:not([@class='heading_table_top'])").collect do |row|
28
+ [
29
+ [:train_no, 'td[1]/text()'],
30
+ [:train_name, 'td[2]/text()'],
31
+ [:runs_from, 'td[3]/text()']
32
+ ].each do |name, xpath|
33
+ train_detail[name] = row.at_xpath(xpath).to_s.strip
34
+ end
35
+ (4..10).each do |day|
36
+ runs_on << row.at_xpath("td[#{day}]/text()").to_s.strip
37
+ end
38
+ train_detail[:runs_on] = runs_on.join(', ')
39
+ end
40
+ elsif index == 2
41
+ table.css("tr:not([@class='heading_table_top'])").collect do |row|
42
+ coach_detail = {}
43
+ [
44
+ [:title, 'td[1]/text()'],
45
+ ['1A', 'td[2]/text()'],
46
+ ['2A', 'td[3]/text()'],
47
+ ['FC', 'td[4]/text()'],
48
+ ['3A', 'td[5]/text()'],
49
+ ['CC', 'td[6]/text()'],
50
+ ['SL', 'td[7]/text()'],
51
+ ['2S', 'td[8]/text()'],
52
+ ['3E', 'td[9]/text()']
53
+ ].each do |name, xpath|
54
+ coach_detail[name] = row.at_xpath(xpath).to_s.strip
55
+ end
56
+ coach_details << coach_detail
57
+ end
58
+ elsif index == 3
59
+ table.css("tr:not([@class='heading_table_top'])").collect do |row|
60
+ stn_detail = []
61
+ ['td[2]/text()', 'td[3]/text()', 'td[4]/text()', 'td[5]/text()', 'td[6]/text()', 'td[7]/text()', 'td[8]/text()','td[9]/text()'].each do |xpath|
62
+ stn_detail << row.at_xpath(xpath).to_s.strip
63
+ end
64
+ stn_details << stn_detail
65
+ end
66
+ end
67
+ index += 1
68
+ end
69
+ details[:train_details] = train_detail
70
+ details[:coach_details] = coach_details
71
+ details[:stn_details] = stn_details
72
+ details
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,3 @@
1
+ module IndianRail
2
+ VERSION = "1.0"
3
+ end
@@ -0,0 +1,4 @@
1
+ require_relative "./indian-rail/version"
2
+ require_relative "./indian-rail/api"
3
+ require_relative "./indian-rail/pnr"
4
+ require_relative "./indian-rail/schedule"
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: indian-rail
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kalyan Allampalli
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-29 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: &16179492 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.5.9
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *16179492
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &16178952 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 2.13.0
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *16178952
36
+ description: Client for the RESTful JSON Indian Rail
37
+ email: kalyan.allampalli@gmail.com
38
+ executables: []
39
+ extensions: []
40
+ extra_rdoc_files: []
41
+ files:
42
+ - Gemfile
43
+ - Gemfile.lock
44
+ - LICENSE.txt
45
+ - README.md
46
+ - Rakefile
47
+ - lib/indian-rail.rb
48
+ - lib/indian-rail/version.rb
49
+ - lib/indian-rail/api.rb
50
+ - lib/indian-rail/pnr.rb
51
+ - lib/indian-rail/schedule.rb
52
+ - indian-rail.gemspec
53
+ homepage: https://github.com/kalyanallampalli/indian-rail
54
+ licenses: []
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubyforge_project:
73
+ rubygems_version: 1.8.11
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: Indian Rail helps you get all the required information you want to know about
77
+ Indian Railways.
78
+ test_files: []