ruby-ares 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,24 @@
1
+ ## Usage
2
+
3
+ subject = ARES::Subject.get ic
4
+
5
+ puts subject
6
+ puts subject.name
7
+ puts subject.ic
8
+ puts subject.address # same as puts subject.addresses[0].to_str
9
+
10
+ #require './lib/ares.rb'
11
+ require 'xml'
12
+ require 'libxml'
13
+ require 'net/http'
14
+ require './lib/ares/subject.rb'
15
+ require './lib/ares/address.rb'
16
+ require './lib/ares/parser.rb'
17
+ require './lib/ares/http.rb'
18
+
19
+ subject = ARES::Subject.get(74948440)
20
+
21
+ puts subject.name
22
+ puts subject.ic
23
+ puts subject.dic
24
+ puts subject.address
@@ -0,0 +1,31 @@
1
+ require 'rubygems/package_task'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+
5
+ gemspec = Gem::Specification.new do |s|
6
+ s.name = "ruby-ares"
7
+ s.version = "0.0.1"
8
+ s.platform = Gem::Platform::RUBY
9
+ s.summary = "Gem for accesing business information from ARES database."
10
+ s.description = <<-EOF
11
+ ARES is the Czech business database maintained by Ministry of Finance of the Czech Republic.
12
+ This gem helps to retrieve data provided by this database.
13
+ EOF
14
+ s.licenses = ["GPLv3"]
15
+ s.author = "Josef Strzibny"
16
+ s.email = "strzibny@strzibny.name"
17
+ s.homepage = "http://github.com/strzibny/ares"
18
+ s.required_ruby_version = ">= 1.8.7"
19
+ s.required_rubygems_version = ">= 1.8.0"
20
+ s.files = FileList["README.md", "Rakefile",
21
+ "lib/**/*.rb", "test/**/test*.rb"]
22
+ end
23
+
24
+ Gem::PackageTask.new gemspec do |pkg|
25
+ end
26
+
27
+ Rake::TestTask.new('test') do |t|
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/test*.rb'
30
+ t.verbose = true
31
+ end
@@ -0,0 +1,8 @@
1
+ # encoding: utf-8
2
+
3
+ require 'xml'
4
+ require 'libxml'
5
+ require 'net/http'
6
+ require 'ruby-ares/subject'
7
+ require 'ruby-ares/address'
8
+ require 'ruby-ares/parser'
@@ -0,0 +1,51 @@
1
+ # encoding: utf-8
2
+
3
+ module RubyARES
4
+ class Address
5
+ attr_reader :id, # ID_adresy
6
+ :city, # nazev_obce
7
+ :city_part, # Nazev_casti_obce
8
+ :street, # Nazev_ulice
9
+ :house_number, # Cislo_domovni
10
+ :house_number_type, # Typ_cislo_domovni
11
+ :orientational_number, # Cislo_orientacni
12
+ :postcode # PSC
13
+
14
+ def initialize(id, street, postcode, city, city_part,
15
+ house_number, house_number_type, orientational_number)
16
+ @id = id
17
+ @street = street
18
+ @postcode = postcode
19
+ @city = city
20
+ @city_part = city_part
21
+ @house_number = house_number
22
+ @house_number_type = house_number_type
23
+ @orientational_number = orientational_number
24
+ end
25
+
26
+ def to_str
27
+ @city_part = '' if @city == @city_part
28
+ <<EOF
29
+ #{self.street} #{self.street_number}
30
+ #{self.postcode} #{self.city}
31
+ #{self.city_part}
32
+ EOF
33
+ end
34
+
35
+ def street_number
36
+ unless @orientational_number
37
+ @house_number
38
+ else
39
+ "#{@orientational_number}/#{@house_number}"
40
+ end
41
+ end
42
+
43
+ def street_line
44
+ "#{@street} #{street_number}"
45
+ end
46
+
47
+ def city_line
48
+ "#{@postcode} #{@city}"
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,25 @@
1
+ # encoding: utf-8
2
+
3
+ require 'net/http'
4
+
5
+ module RubyARES
6
+ class HTTP
7
+
8
+ class ConnectionError < StandardError; end
9
+
10
+ def self.fetch_subject_xml(ic)
11
+ # Get a subject info from ARES[http://wwwinfo.mfcr.cz/ares/]
12
+ uri = URI('http://wwwinfo.mfcr.cz/cgi-bin/ares/darv_rzp.cgi')
13
+ params = { :ico => ic, :ver => '1.0.4' }
14
+ uri.query = URI.encode_www_form(params)
15
+
16
+ begin
17
+ res = Net::HTTP.get_response uri
18
+ @xml = res.body if res.is_a? Net::HTTPSuccess
19
+ rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError,
20
+ Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
21
+ raise ConnectionError, e
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,70 @@
1
+ # encoding: utf-8
2
+
3
+ require 'xml'
4
+ require 'libxml'
5
+ require 'ruby-ares/subject'
6
+
7
+ module RubyARES
8
+ class Parser
9
+
10
+ class ARESDatabaseError < StandardError; end
11
+ class ParseError < StandardError; end
12
+
13
+ def self.get_subject(xml)
14
+ begin
15
+ doc = self.parse_document xml
16
+
17
+ # Basic info
18
+ doc.find('//dtt:Zakladni_udaje').each do |node|
19
+ attrs = node.children()
20
+
21
+ # Attributes of the subject
22
+ @status = node.find('dtt:Stav').to_a[0].content unless node.find('dtt:Stav') == 0
23
+ @updated_at = node.find('dtt:Datum_zmeny').to_a[0].content unless node.find('dtt:Datum_zmeny').to_a.size == 0
24
+ @ic = node.find('dtt:ICO').to_a[0].content
25
+ @dic = node.find('dtt:DIC').to_a[0].content unless node.find('dtt:DIC').to_a.size == 0
26
+ @name = node.find('dtt:Obchodni_firma').to_a[0].content unless node.find('dtt:Obchodni_firma').to_a.size == 0
27
+ end
28
+
29
+ # Corresponding addresses
30
+ @addresses = self.find_addresses doc
31
+ rescue
32
+ raise ParseError, "Can't parse the given document."
33
+ end
34
+
35
+ if doc.find('//dtt:Error').to_a.size > 0
36
+ raise ARESDatabaseError, 'ARES returned an error.'
37
+ end
38
+
39
+ # Create and return subject
40
+ return RubyARES::Subject.new(@ic, @dic, @name, @status, @addresses, @updated_at)
41
+ end
42
+
43
+ protected
44
+
45
+ def self.find_addresses(doc)
46
+ @addresses = []
47
+
48
+ doc.find('//dtt:Adresa').each do |node|
49
+ id = node.find('dtt:ID_adresy').to_a[0].content
50
+ street = node.find('dtt:Nazev_ulice').to_a[0].content unless node.find('dtt:Nazev_ulice').to_a.size == 0
51
+ postcode = node.find('dtt:PSC').to_a[0].content unless node.find('dtt:PSC').to_a.size == 0
52
+ city = node.find('dtt:Nazev_obce').to_a[0].content unless node.find('dtt:Nazev_obce').to_a.size == 0
53
+ city_part = node.find('dtt:Nazev_casti_obce').to_a[0].content unless node.find('dtt:Nazev_casti_obce').to_a.size == 0
54
+ house_number = node.find('dtt:Cislo_domovni').to_a[0].content unless node.find('dtt:Cislo_domovni').to_a.size == 0
55
+ house_number_type = node.find('dtt:Typ_cislo_domovni').to_a[0].content unless node.find('dtt:Typ_cislo_domovni').to_a.size == 0
56
+ orientational_number = node.find('dtt:Cislo_orientacni').to_a[0].content unless node.find('dtt:Cislo_orientacni').to_a.size == 0
57
+
58
+ @addresses << RubyARES::Address.new(id, street, postcode, city, city_part,
59
+ house_number, house_number_type, orientational_number)
60
+ end
61
+
62
+ return @addresses
63
+ end
64
+
65
+ def self.parse_document(xml)
66
+ parser = XML::Parser.string xml
67
+ parser.parse
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,35 @@
1
+ # encoding: utf-8
2
+
3
+ require 'ruby-ares/parser'
4
+ require 'ruby-ares/http'
5
+
6
+ module RubyARES
7
+ class Subject
8
+ attr_reader :ico, # ICO
9
+ :ic, # alias for ic
10
+ :dic, # DIC
11
+ :name, # Obchodni_firma
12
+ :company, # alias for name
13
+ :status, # Stav
14
+ :addresses,
15
+ :updated_at # Datum_zmeny
16
+
17
+ def self.get(ic)
18
+ xml = RubyARES::HTTP.fetch_subject_xml ic
19
+ RubyARES::Parser.get_subject xml
20
+ end
21
+
22
+ def initialize(ic, dic, name, status, addresses, updated_at)
23
+ @ic, @ico = ic, ic
24
+ @dic = dic
25
+ @name, @company = name
26
+ @status = status
27
+ @addresses = addresses
28
+ @updated_at = updated_at
29
+ end
30
+
31
+ def address
32
+ @addresses[0]
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,35 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'test/unit'
4
+ require 'helper'
5
+ require 'ruby-ares'
6
+
7
+ class RubyARESTest < Test::Unit::TestCase
8
+ def setup
9
+ @subject = RubyARES::Parser.get_subject RubyARESTestHelper.subject_xml
10
+ end
11
+
12
+ def test_subject_address_house_number
13
+ assert_equal '2178 1', "#{@subject.addresses[0].house_number} #{@subject.addresses[0].house_number_type}"
14
+ end
15
+
16
+ def test_subject_address_street
17
+ assert_equal 'Podvinný mlýn 6/2178', "#{@subject.addresses[0].street} #{@subject.addresses[0].street_number}"
18
+ end
19
+
20
+ def test_subject_address_city
21
+ assert_equal 'Praha Libeň', "#{@subject.addresses[0].city} #{@subject.addresses[0].city_part}"
22
+ end
23
+
24
+ def test_subject_address_postcode
25
+ assert_equal '19000', @subject.addresses[0].postcode
26
+ end
27
+
28
+ def test_subject_name
29
+ assert_equal 'Asseco Central Europe, a.s.', @subject.name
30
+ end
31
+
32
+ def test_number_of_subject_addresses
33
+ assert_equal 1, @subject.addresses.size
34
+ end
35
+ end
@@ -0,0 +1,23 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'test/unit'
4
+ require 'ruby-ares/address'
5
+
6
+ class RubyARESAddressTest < Test::Unit::TestCase
7
+
8
+ def setup
9
+ @address = RubyARES::Address.new(1, 'Kunzova', '10010', 'Brno', 'Královo pole', 1, 1, nil)
10
+ end
11
+
12
+ def test_address_to_string
13
+ assert_equal "Kunzova 1\n10010 Brno\nKrálovo pole\n", @address.to_str
14
+ end
15
+
16
+ def test_address_street_line
17
+ assert_equal "Kunzova 1", @address.street_line
18
+ end
19
+
20
+ def test_address_city_line
21
+ assert_equal "10010 Brno", @address.city_line
22
+ end
23
+ end
@@ -0,0 +1,8 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'test/unit'
4
+ require 'ruby-ares/http'
5
+
6
+ class RubyARESHTTPTest < Test::Unit::TestCase
7
+
8
+ end
@@ -0,0 +1,14 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'test/unit'
4
+ require 'helper'
5
+ require 'ruby-ares/parser'
6
+
7
+ class RubyARESParserTest < Test::Unit::TestCase
8
+
9
+ def test_parser_should_fail
10
+ assert_raise(RubyARES::Parser::ARESDatabaseError) {
11
+ RubyARES::Parser.get_subject(RubyARESTestHelper.error_responce_xml)
12
+ }
13
+ end
14
+ end
@@ -0,0 +1,8 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'test/unit'
4
+ require 'ruby-ares/subject'
5
+
6
+ class RubyARESSubjectTest < Test::Unit::TestCase
7
+
8
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-ares
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Josef Strzibny
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-02 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: ! " ARES is the Czech business database maintained
15
+ by Ministry of Finance of the Czech Republic.\n This gem helps
16
+ to retrieve data provided by this database.\n"
17
+ email: strzibny@strzibny.name
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - README.md
23
+ - Rakefile
24
+ - lib/ruby-ares.rb
25
+ - lib/ruby-ares/address.rb
26
+ - lib/ruby-ares/http.rb
27
+ - lib/ruby-ares/parser.rb
28
+ - lib/ruby-ares/subject.rb
29
+ - test/test_ares.rb
30
+ - test/test_ares_address.rb
31
+ - test/test_ares_http.rb
32
+ - test/test_ares_parser.rb
33
+ - test/test_ares_subject.rb
34
+ homepage: http://github.com/strzibny/ares
35
+ licenses:
36
+ - GPLv3
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: 1.8.7
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: 1.8.0
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 1.8.6
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: Gem for accesing business information from ARES database.
59
+ test_files: []