ege_parser 1.0.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6d97966c307a6afba2579d16a15671415ecbbd89
4
+ data.tar.gz: 84bab26b2f56247818f316d2a5e71cf619821219
5
+ SHA512:
6
+ metadata.gz: 0362644dc515fa306c2fafa13bcf52619911de8241fe03cd013723b8bf1483cbd49dab9f83ce85f57ff5be8761f37af2b91d34d06b28d68f3d46c3f8e0a51880
7
+ data.tar.gz: d4901b1d2589058326759e1d314eeaffe39bfa0b851f73bdd7fd10524918d11dbb8821634ae36571549a29fb66318641237f388195c0e92e18068715b33e4e15
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,19 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.8.0"
10
+ gem "rdoc", "~> 3.12"
11
+ gem "bundler", "~> 1.0"
12
+ gem "jeweler", "~> 2.0.1"
13
+ gem "simplecov", ">= 0"
14
+ gem "webmock"
15
+ end
16
+
17
+ gem 'nokogiri'
18
+ gem 'mechanize'
19
+ gem 'foreman'
data/Gemfile.lock ADDED
@@ -0,0 +1,111 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ addressable (2.3.6)
5
+ builder (3.2.2)
6
+ crack (0.4.2)
7
+ safe_yaml (~> 1.0.0)
8
+ descendants_tracker (0.0.4)
9
+ thread_safe (~> 0.3, >= 0.3.1)
10
+ diff-lcs (1.1.3)
11
+ docile (1.1.3)
12
+ domain_name (0.5.18)
13
+ unf (>= 0.0.5, < 1.0.0)
14
+ dotenv (0.11.1)
15
+ dotenv-deployment (~> 0.0.2)
16
+ dotenv-deployment (0.0.2)
17
+ faraday (0.9.0)
18
+ multipart-post (>= 1.2, < 3)
19
+ foreman (0.71.0)
20
+ dotenv (~> 0.11.1)
21
+ thor (~> 0.19.1)
22
+ git (1.2.6)
23
+ github_api (0.11.3)
24
+ addressable (~> 2.3)
25
+ descendants_tracker (~> 0.0.1)
26
+ faraday (~> 0.8, < 0.10)
27
+ hashie (>= 1.2)
28
+ multi_json (>= 1.7.5, < 2.0)
29
+ nokogiri (~> 1.6.0)
30
+ oauth2
31
+ hashie (3.0.0)
32
+ highline (1.6.21)
33
+ http-cookie (1.0.2)
34
+ domain_name (~> 0.5)
35
+ jeweler (2.0.1)
36
+ builder
37
+ bundler (>= 1.0)
38
+ git (>= 1.2.5)
39
+ github_api
40
+ highline (>= 1.6.15)
41
+ nokogiri (>= 1.5.10)
42
+ rake
43
+ rdoc
44
+ json (1.8.1)
45
+ jwt (1.0.0)
46
+ mechanize (2.7.3)
47
+ domain_name (~> 0.5, >= 0.5.1)
48
+ http-cookie (~> 1.0)
49
+ mime-types (~> 2.0)
50
+ net-http-digest_auth (~> 1.1, >= 1.1.1)
51
+ net-http-persistent (~> 2.5, >= 2.5.2)
52
+ nokogiri (~> 1.4)
53
+ ntlm-http (~> 0.1, >= 0.1.1)
54
+ webrobots (>= 0.0.9, < 0.2)
55
+ mime-types (2.3)
56
+ mini_portile (0.6.0)
57
+ multi_json (1.10.1)
58
+ multi_xml (0.5.5)
59
+ multipart-post (2.0.0)
60
+ net-http-digest_auth (1.4)
61
+ net-http-persistent (2.9.4)
62
+ nokogiri (1.6.2.1)
63
+ mini_portile (= 0.6.0)
64
+ ntlm-http (0.1.1)
65
+ oauth2 (0.9.4)
66
+ faraday (>= 0.8, < 0.10)
67
+ jwt (~> 1.0)
68
+ multi_json (~> 1.3)
69
+ multi_xml (~> 0.5)
70
+ rack (~> 1.2)
71
+ rack (1.5.2)
72
+ rake (10.3.2)
73
+ rdoc (3.12.2)
74
+ json (~> 1.4)
75
+ rspec (2.8.0)
76
+ rspec-core (~> 2.8.0)
77
+ rspec-expectations (~> 2.8.0)
78
+ rspec-mocks (~> 2.8.0)
79
+ rspec-core (2.8.0)
80
+ rspec-expectations (2.8.0)
81
+ diff-lcs (~> 1.1.2)
82
+ rspec-mocks (2.8.0)
83
+ safe_yaml (1.0.3)
84
+ simplecov (0.8.2)
85
+ docile (~> 1.1.0)
86
+ multi_json
87
+ simplecov-html (~> 0.8.0)
88
+ simplecov-html (0.8.0)
89
+ thor (0.19.1)
90
+ thread_safe (0.3.4)
91
+ unf (0.1.4)
92
+ unf_ext
93
+ unf_ext (0.0.6)
94
+ webmock (1.18.0)
95
+ addressable (>= 2.3.6)
96
+ crack (>= 0.3.2)
97
+ webrobots (0.1.1)
98
+
99
+ PLATFORMS
100
+ ruby
101
+
102
+ DEPENDENCIES
103
+ bundler (~> 1.0)
104
+ foreman
105
+ jeweler (~> 2.0.1)
106
+ mechanize
107
+ nokogiri
108
+ rdoc (~> 3.12)
109
+ rspec (~> 2.8.0)
110
+ simplecov
111
+ webmock
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2014 manyrus
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = ege_parser
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to ege_parser
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2014 manyrus. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,50 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://guides.rubygems.org/specification-reference/ for more options
17
+ gem.name = "ege_parser"
18
+ gem.homepage = "http://github.com/manyrus/ege_parser"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{EGE gem praser}
21
+ gem.description = %Q{Lib for parsing EGE results}
22
+ gem.email = "manyrus@gmail.com"
23
+ gem.authors = ["manyrus"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ desc "Code coverage detail"
35
+ task :simplecov do
36
+ ENV['COVERAGE'] = "true"
37
+ Rake::Task['spec'].execute
38
+ end
39
+
40
+ task :default => :spec
41
+
42
+ require 'rdoc/task'
43
+ Rake::RDocTask.new do |rdoc|
44
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
45
+
46
+ rdoc.rdoc_dir = 'rdoc'
47
+ rdoc.title = "ege_parser #{version}"
48
+ rdoc.rdoc_files.include('README*')
49
+ rdoc.rdoc_files.include('lib/**/*.rb')
50
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,83 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+ # stub: ege_parser 1.0.0 ruby lib
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = "ege_parser"
9
+ s.version = "1.0.0"
10
+
11
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
+ s.require_paths = ["lib"]
13
+ s.authors = ["manyrus"]
14
+ s.date = "2014-06-06"
15
+ s.description = "Lib for parsing EGE results"
16
+ s.email = "manyrus@gmail.com"
17
+ s.extra_rdoc_files = [
18
+ "LICENSE.txt",
19
+ "README.rdoc"
20
+ ]
21
+ s.files = [
22
+ ".document",
23
+ ".rspec",
24
+ "Gemfile",
25
+ "Gemfile.lock",
26
+ "LICENSE.txt",
27
+ "README.rdoc",
28
+ "Rakefile",
29
+ "VERSION",
30
+ "ege_parser.gemspec",
31
+ "examples/2.rb",
32
+ "lib/ege_parser.rb",
33
+ "lib/error.rb",
34
+ "lib/pupil.rb",
35
+ "spec/ege_parser_spec.rb",
36
+ "spec/spec_helper.rb",
37
+ "spec/templates/authed_error.html",
38
+ "spec/templates/bad_data.html",
39
+ "spec/templates/success.html",
40
+ "spec/templates/unknown_error.html"
41
+ ]
42
+ s.homepage = "http://github.com/manyrus/ege_parser"
43
+ s.licenses = ["MIT"]
44
+ s.rubygems_version = "2.2.2"
45
+ s.summary = "EGE gem praser"
46
+
47
+ if s.respond_to? :specification_version then
48
+ s.specification_version = 4
49
+
50
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
51
+ s.add_runtime_dependency(%q<nokogiri>, [">= 0"])
52
+ s.add_runtime_dependency(%q<mechanize>, [">= 0"])
53
+ s.add_runtime_dependency(%q<foreman>, [">= 0"])
54
+ s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
55
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
56
+ s.add_development_dependency(%q<bundler>, ["~> 1.0"])
57
+ s.add_development_dependency(%q<jeweler>, ["~> 2.0.1"])
58
+ s.add_development_dependency(%q<simplecov>, [">= 0"])
59
+ s.add_development_dependency(%q<webmock>, [">= 0"])
60
+ else
61
+ s.add_dependency(%q<nokogiri>, [">= 0"])
62
+ s.add_dependency(%q<mechanize>, [">= 0"])
63
+ s.add_dependency(%q<foreman>, [">= 0"])
64
+ s.add_dependency(%q<rspec>, ["~> 2.8.0"])
65
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
66
+ s.add_dependency(%q<bundler>, ["~> 1.0"])
67
+ s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
68
+ s.add_dependency(%q<simplecov>, [">= 0"])
69
+ s.add_dependency(%q<webmock>, [">= 0"])
70
+ end
71
+ else
72
+ s.add_dependency(%q<nokogiri>, [">= 0"])
73
+ s.add_dependency(%q<mechanize>, [">= 0"])
74
+ s.add_dependency(%q<foreman>, [">= 0"])
75
+ s.add_dependency(%q<rspec>, ["~> 2.8.0"])
76
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
77
+ s.add_dependency(%q<bundler>, ["~> 1.0"])
78
+ s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
79
+ s.add_dependency(%q<simplecov>, [">= 0"])
80
+ s.add_dependency(%q<webmock>, [">= 0"])
81
+ end
82
+ end
83
+
data/examples/2.rb ADDED
@@ -0,0 +1,11 @@
1
+ require_relative '../lib/ege_parser.rb'
2
+ pupil = EgeParser::Pupil.new
3
+
4
+ pupil.name = 'Никитос'
5
+ pupil.surname = 'Преблагин'
6
+ pupil.patronymic = 'Олегович'
7
+ pupil.passport = '453312'
8
+ pupil.region = '45'
9
+
10
+ ege_parser = EgeParser::Parser.new
11
+ p ege_parser.get_subjects pupil
data/lib/ege_parser.rb ADDED
@@ -0,0 +1,77 @@
1
+ require 'open-uri'
2
+ require 'net/http'
3
+ require 'nokogiri'
4
+ require 'mechanize'
5
+
6
+ require_relative 'error.rb'
7
+ require_relative 'pupil.rb'
8
+
9
+ module EgeParser
10
+
11
+ class Parser
12
+
13
+ attr_writer :auth_agent
14
+
15
+ def auth_agent
16
+ @auth_agent ||= Mechanize.new
17
+ @auth_agent.open_timeout=180
18
+ @auth_agent.read_timeout=180
19
+ @auth_agent
20
+ end
21
+
22
+ def get_subjects pupil
23
+ page = auth_agent.post('http://check.ege.edu.ru/ru/index.php', {
24
+ name: pupil.name,
25
+ surname: pupil.surname,
26
+ patronymic: pupil.patronymic,
27
+ doc_number: pupil.passport,
28
+ region: pupil.region,
29
+ generate_captcha_val => ''
30
+ })
31
+
32
+ generate_subjects page
33
+ end
34
+
35
+ private
36
+
37
+ def generate_captcha_val
38
+ captcha_key = '-a-UHS2Wj8Cy8W4QB4cpZ8y_6XZNUWU'
39
+ page = auth_agent.post('http://check.ege.edu.ru/common/Qaptcha.jquery.php', {
40
+ action:'qaptcha',
41
+ qaptcha_key:captcha_key
42
+ })
43
+ raise EgeParser::CaptchaError if JSON.parse(page.body)['error']
44
+ captcha_key
45
+ end
46
+
47
+ def generate_subjects page
48
+ Hash[find_elements(2, page).zip find_elements(5, page)]
49
+ end
50
+
51
+ def find_elements row, page
52
+ result = []
53
+ page.search("//table[contains(@class, 'appil_resultat')]/tbody/tr/td[#{row}]").to_a.map{|el|
54
+ parsed = el_to_s(el)
55
+ result << parsed if parsed != nil|| parsed != ''
56
+ }
57
+
58
+ check_for_errors page
59
+
60
+ raise EgeParser::BadDataError if result.any?{|k| k.empty?} || result.empty?
61
+
62
+ result
63
+ end
64
+
65
+ def check_for_errors page
66
+ service_error = el_to_s(page.search('//h2'))
67
+ raise EgeParser::ServiceError.new(service_error) unless service_error.empty?
68
+
69
+ auth_error = el_to_s(page.search('//div[@id="show_div2"]/form'))
70
+ raise EgeParser::AuthError.new unless auth_error.empty?
71
+ end
72
+
73
+ def el_to_s el
74
+ el.to_s.gsub(/<\/?[^>]+>/, '').gsub(/\n|\t/,'').strip
75
+ end
76
+ end
77
+ end
data/lib/error.rb ADDED
@@ -0,0 +1,7 @@
1
+ module EgeParser
2
+ class AuthError < StandardError; end
3
+ class ParserError < StandardError; end
4
+ class BadDataError < StandardError; end
5
+ class ServiceError < StandardError; end
6
+ class CaptchaError < StandardError; end
7
+ end
data/lib/pupil.rb ADDED
@@ -0,0 +1,11 @@
1
+ module EgeParser
2
+
3
+ class Pupil
4
+ attr_accessor :name,
5
+ :surname,
6
+ :patronymic,
7
+ :passport,
8
+ :region
9
+ end
10
+
11
+ end
@@ -0,0 +1,70 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe EgeParser::Parser, :fast do
4
+ let(:pupil){
5
+ pupil = EgeParser::Pupil.new
6
+ pupil.name = 'Сергей'
7
+ pupil.surname = 'Попов'
8
+ pupil.patronymic = 'Олегович'
9
+ pupil.passport = '741131'
10
+ pupil.region = '29'
11
+
12
+ pupil
13
+ }
14
+
15
+ def stub_captcha(is_error=false)
16
+ stub_request(:post, 'http://check.ege.edu.ru/common/Qaptcha.jquery.php').to_return(body:{error:is_error}.to_json)
17
+ end
18
+
19
+ def stub_result file_name
20
+ stub_request(:post, "http://check.ege.edu.ru/ru/index.php")
21
+ .to_return(
22
+ :status => 200,
23
+ :body => File.read(File.dirname(__FILE__) + "/templates/#{file_name}.html"),
24
+ :headers => {'Content-Type'=>'text/html'}
25
+ )
26
+ end
27
+
28
+ describe '#get_subjects' do
29
+ context 'when bad captcha' do
30
+ it 'throw EgeParser::CaptchaError' do
31
+ stub_captcha(is_error = true)
32
+
33
+ expect{subject.get_subjects pupil}.to raise_error(EgeParser::CaptchaError)
34
+ end
35
+ end
36
+
37
+ context 'when not authed' do
38
+ it 'throw EgeParser::AuthError' do
39
+ stub_captcha
40
+ stub_result 'authed_error'
41
+
42
+ expect{subject.get_subjects pupil}.to raise_error(EgeParser::AuthError)
43
+ end
44
+ end
45
+
46
+ context 'when some subject is empty' do
47
+ it 'throw EgeParser::ParserError' do
48
+ stub_captcha
49
+ stub_result 'bad_data'
50
+
51
+ expect{p subject.get_subjects pupil}.to raise_error(EgeParser::BadDataError)
52
+ end
53
+ end
54
+
55
+ context 'all ok' do
56
+ it 'parse all data and get result' do
57
+ stub_captcha
58
+ stub_result 'success'
59
+
60
+ subject.get_subjects(pupil).should ==
61
+ {'Русский язык' => 'Нет результата (обработка)',
62
+ 'Математика' => 'Экзамен не проходил',
63
+ 'Физика' => 'Экзамен не проходил',
64
+ 'Информатика и ИКТ' => 'Экзамен не проходил'}
65
+ end
66
+ end
67
+ end
68
+ end
69
+
70
+
@@ -0,0 +1,30 @@
1
+ require 'simplecov'
2
+ require 'webmock/rspec'
3
+
4
+ module SimpleCov::Configuration
5
+ def clean_filters
6
+ @filters = []
7
+ end
8
+ end
9
+
10
+ SimpleCov.configure do
11
+ clean_filters
12
+ load_profile 'test_frameworks'
13
+ end
14
+
15
+ ENV["COVERAGE"] && SimpleCov.start do
16
+ add_filter "/.rvm/"
17
+ end
18
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
19
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
20
+
21
+ require 'rspec'
22
+ require 'ege_parser'
23
+
24
+ # Requires supporting files with custom matchers and macros, etc,
25
+ # in ./support/ and its subdirectories.
26
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
27
+
28
+ RSpec.configure do |c|
29
+ c.treat_symbols_as_metadata_keys_with_true_values = true
30
+ end