bdz 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.rspec +1 -0
- data/README.md +3 -27
- data/bdz.gemspec +9 -1
- data/lib/bdz/client.rb +35 -0
- data/lib/bdz/parser/schedule.rb +26 -0
- data/lib/bdz/parser.rb +1 -0
- data/lib/bdz/train.rb +17 -0
- data/lib/bdz/version.rb +1 -1
- data/lib/bdz.rb +7 -3
- data/spec/bdz/client_spec.rb +19 -0
- data/spec/bdz/parser/schedule_spec.rb +27 -0
- data/spec/bdz_spec.rb +9 -0
- data/spec/mocks/response.html +1552 -0
- data/spec/spec_helper.rb +12 -0
- metadata +116 -5
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/README.md
CHANGED
@@ -1,29 +1,5 @@
|
|
1
|
-
# Bdz
|
1
|
+
# Bdz Usage
|
2
|
+
client = Bdz::Client.new
|
3
|
+
client.search({:from_station => "Пловдив", :to_station => "София", :date => "20/08/2012"})
|
2
4
|
|
3
|
-
TODO: Write a gem description
|
4
5
|
|
5
|
-
## Installation
|
6
|
-
|
7
|
-
Add this line to your application's Gemfile:
|
8
|
-
|
9
|
-
gem 'bdz'
|
10
|
-
|
11
|
-
And then execute:
|
12
|
-
|
13
|
-
$ bundle
|
14
|
-
|
15
|
-
Or install it yourself as:
|
16
|
-
|
17
|
-
$ gem install bdz
|
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 'Added some feature'`)
|
28
|
-
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
-
5. Create new Pull Request
|
data/bdz.gemspec
CHANGED
@@ -5,7 +5,7 @@ Gem::Specification.new do |gem|
|
|
5
5
|
gem.authors = ["Todor Grudev"]
|
6
6
|
gem.email = ["tagrudev@gmail.com"]
|
7
7
|
gem.description = %q{BDZ query maker}
|
8
|
-
gem.summary = %q{
|
8
|
+
gem.summary = %q{The Bulgarian State Railways schedule retriever}
|
9
9
|
gem.homepage = ""
|
10
10
|
|
11
11
|
gem.files = `git ls-files`.split($\)
|
@@ -14,4 +14,12 @@ Gem::Specification.new do |gem|
|
|
14
14
|
gem.name = "bdz"
|
15
15
|
gem.require_paths = ["lib"]
|
16
16
|
gem.version = Bdz::VERSION
|
17
|
+
|
18
|
+
gem.add_runtime_dependency "faraday"
|
19
|
+
gem.add_runtime_dependency "nokogiri"
|
20
|
+
|
21
|
+
gem.add_development_dependency "pry"
|
22
|
+
gem.add_development_dependency "rake"
|
23
|
+
gem.add_development_dependency "rspec", "~> 2.0"
|
24
|
+
gem.add_development_dependency "webmock", "~> 1.6"
|
17
25
|
end
|
data/lib/bdz/client.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
module Bdz
|
3
|
+
class Client
|
4
|
+
attr_reader :params
|
5
|
+
attr_reader :faraday_client
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@params = {}
|
9
|
+
@faraday_client = Faraday.new(:url => Bdz::ROOT_URL) do |faraday|
|
10
|
+
faraday.request :url_encoded # form-encode POST params
|
11
|
+
faraday.response :logger # log requests to STDOUT
|
12
|
+
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# client.search({:from_station => "Пловдив", :to_station => "София", :date => "20/08/2012"})
|
17
|
+
def search(params = {})
|
18
|
+
content = get(params)
|
19
|
+
parser = Bdz::Parser::Schedule.new content
|
20
|
+
parser.parse
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
def get(params = {})
|
25
|
+
@faraday_client.post do |req|
|
26
|
+
req.params = build_params(params)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def build_params(params = {})
|
31
|
+
@params = params.merge({ :action => 'listOptions', :dep_arr => '1', :time_from => '00:00', :time_to => '24:00', :all_cats => 'checked',
|
32
|
+
:cardId => '30', :sort_by => '0'})
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Bdz
|
2
|
+
module Parser
|
3
|
+
class Schedule
|
4
|
+
def initialize(response)
|
5
|
+
@response = response
|
6
|
+
end
|
7
|
+
|
8
|
+
def parse
|
9
|
+
@content = Nokogiri::HTML(@response.body)
|
10
|
+
parse_content
|
11
|
+
end
|
12
|
+
|
13
|
+
def parse_content
|
14
|
+
@content.css('div.accordionTabTitleBar').collect do |train|
|
15
|
+
t = Bdz::Train.new(
|
16
|
+
:id => train.css('table tr td')[0].text,
|
17
|
+
:leaves => train.css('table tr td')[1].text,
|
18
|
+
:arrives => train.css('table tr td')[2].text,
|
19
|
+
:type => train.css('table tr td')[3].text,
|
20
|
+
:docks => train.css('table tr td')[4].text
|
21
|
+
)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/bdz/parser.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bdz/parser/schedule"
|
data/lib/bdz/train.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
module Bdz
|
2
|
+
class Train
|
3
|
+
attr_reader :id
|
4
|
+
attr_reader :leaves
|
5
|
+
attr_reader :arrives
|
6
|
+
attr_reader :type
|
7
|
+
attr_reader :docks
|
8
|
+
def initialize (params)
|
9
|
+
return if params.nil?
|
10
|
+
|
11
|
+
params.each do |key, value|
|
12
|
+
name = key.to_s
|
13
|
+
instance_variable_set("@#{name}", value) if respond_to?(name)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/bdz/version.rb
CHANGED
data/lib/bdz.rb
CHANGED
@@ -1,7 +1,11 @@
|
|
1
|
+
require "nokogiri"
|
1
2
|
require "bdz/version"
|
2
3
|
|
3
4
|
module Bdz
|
4
|
-
|
5
|
-
|
6
|
-
end
|
5
|
+
BASE_URL = "http://razpisanie.bdz.bg/"
|
6
|
+
ROOT_URL = "http://razpisanie.bdz.bg/SearchServlet"
|
7
7
|
end
|
8
|
+
|
9
|
+
require "bdz/client"
|
10
|
+
require "bdz/parser"
|
11
|
+
require "bdz/train"
|
@@ -0,0 +1,19 @@
|
|
1
|
+
#encoding: UTF-8
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
|
3
|
+
|
4
|
+
describe Bdz::Client do
|
5
|
+
let(:client) { Bdz::Client.new }
|
6
|
+
|
7
|
+
describe '#build_params' do
|
8
|
+
it 'should merge defaults with params' do
|
9
|
+
params = client.send :build_params, {:from => 'Пловдив'}
|
10
|
+
params.should == {:from=>'Пловдив', :action=>"listOptions", :dep_arr=>"1", :time_from=>"00:00", :time_to=>"24:00",
|
11
|
+
:all_cats=>"checked", :cardId=>"30", :sort_by=>"0"}
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '#search' do
|
16
|
+
it 'should return content' do
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
#encoding: UTF-8
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
|
3
|
+
require 'webmock/rspec'
|
4
|
+
WebMock.disable_net_connect!
|
5
|
+
|
6
|
+
describe Bdz::Parser::Schedule do
|
7
|
+
let(:client) { Bdz::Client.new}
|
8
|
+
|
9
|
+
|
10
|
+
it 'parse and return response of objects' do
|
11
|
+
|
12
|
+
stub_request(:post, "http://razpisanie.bdz.bg/SearchServlet?action=listOptions&all_cats=checked&cardId=30&date=20/08/2012&dep_arr=1&from_station=%D0%9F%D0%BB%D0%BE%D0%B2%D0%B4%D0%B8%D0%B2&sort_by=0&time_from=00:00&time_to=24:00&to_station=%D0%A1%D0%BE%D1%84%D0%B8%D1%8F").
|
13
|
+
with(:headers => {'Accept'=>'*/*', 'Content-Length'=>'0', 'User-Agent'=>'Ruby'}).
|
14
|
+
to_return(:status => 200, :body => File.new(File.join(File.dirname(__FILE__), '/../../mocks', "response.html")), :headers => {})
|
15
|
+
m = client.search({:from_station => "Пловдив", :to_station => "София", :date => "20/08/2012"})
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'doesnt get enough params' do
|
19
|
+
stub_request(:post, "http://razpisanie.bdz.bg/SearchServlet?action=listOptions&all_cats=checked&cardId=30&dep_arr=1&from_station=%D0%9F%D0%BB%D0%BE%D0%B2%D0%B4%D0%B8%D0%B2&sort_by=0&time_from=00:00&time_to=24:00").
|
20
|
+
with(:headers => {'Accept'=>'*/*', 'Content-Length'=>'0', 'User-Agent'=>'Ruby'}).
|
21
|
+
to_return(:status => 200, :body => "", :headers => {})
|
22
|
+
|
23
|
+
|
24
|
+
m = client.search({:from_station => "Пловдив"})
|
25
|
+
m.should be_empty
|
26
|
+
end
|
27
|
+
end
|
data/spec/bdz_spec.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + "/spec_helper")
|
2
|
+
|
3
|
+
describe Bdz do
|
4
|
+
describe '#root_url' do
|
5
|
+
it 'shoudl define a constant ROOT_URL that returns the url of bdz' do
|
6
|
+
Bdz::ROOT_URL.should == 'http://razpisanie.bdz.bg/SearchServlet'
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|