ares_cz 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,58 @@
1
+ = Ares
2
+
3
+ Simple library for querying Ares system in Czech republic with translation of
4
+ labels.
5
+
6
+ http://wwwinfo.mfcr.cz/ares/ares.html.cz
7
+
8
+ == Usage
9
+
10
+ Is simple as creating ares instance by finder and calling methods on it:
11
+
12
+ require 'rubygems'
13
+ require 'ares_cz'
14
+
15
+ ares = Ares.find(:ico => '27386830')
16
+ ares.found? == true
17
+ ares.company_name == "GravaStar s.r.o."
18
+ ares.subject_type == "P"
19
+ ares.address == {
20
+ :city => "16000 Praha",
21
+ :street => "Charlese de Gaulla 800/3",
22
+ :country => "Česká republika"
23
+ }
24
+ ares = Ares.find(:ico => '666')
25
+ ares.found? == false
26
+
27
+ It should be posible to find by company name, but is not tested. Also subject
28
+ type F needs more testing (see rake test:rcov).
29
+
30
+ == Dependencies
31
+
32
+ Only crack for xml parsing. Install with sudo gem install crack.
33
+
34
+ == License
35
+
36
+ Copyright (c) 2008 pepe@gravastar.cz
37
+
38
+ Permission is hereby granted, free of charge, to any person
39
+ obtaining a copy of this software and associated documentation
40
+ files (the "Software"), to deal in the Software without
41
+ restriction, including without limitation the rights to use,
42
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
43
+ copies of the Software, and to permit persons to whom the
44
+ Software is furnished to do so, subject to the following
45
+ conditions:
46
+
47
+ The above copyright notice and this permission notice shall be
48
+ included in all copies or substantial portions of the Software.
49
+
50
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
51
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
52
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
53
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
54
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
55
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
56
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
57
+ OTHER DEALINGS IN THE SOFTWARE.
58
+
@@ -0,0 +1,49 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'ftools'
4
+ require 'rspec'
5
+ require 'rspec/core/rake_task'
6
+
7
+ desc "Runs all specs as default"
8
+ task :default => 'test:spec'
9
+
10
+ namespace :test do
11
+ desc "Run all specs"
12
+ RSpec::Core::RakeTask.new do |t|
13
+ t.pattern = 'spec/erector/*_spec.rb'
14
+ t.rspec_opts = ['-u']
15
+ end
16
+
17
+ desc "Run all examples with RCov"
18
+ RSpec::Core::RakeTask.new('rcov') do |t|
19
+ t.pattern = 'spec/erector/*_spec.rb'
20
+ t.rcov = true
21
+ t.rcov_opts = ['--exclude', 'spec']
22
+ end
23
+ end
24
+
25
+ namespace :gems do
26
+ desc "Install gems"
27
+ task :install do
28
+ exec 'gem install crack'
29
+ end
30
+ end
31
+
32
+ begin
33
+ require 'jeweler'
34
+ Jeweler::Tasks.new do |gem|
35
+ gem.name = "ares_cz"
36
+ gem.summary = %Q{Simple ruby wrapper for Czech Ares service}
37
+ gem.email = "pepe@gravastar.cz"
38
+ gem.homepage = "http://github.com/pepe/ares"
39
+ gem.authors = ["Josef Pospisil"]
40
+ gem.description = "Simple library for querying Ares system in Czech republic with translation of labels."
41
+ gem.add_dependency('crack', '>= 0.1.4')
42
+ IGNORE = [/\.gitignore$/, /VERSION$/]
43
+ gem.files.reject! { |f| IGNORE.any? { |re| f.match(re) } }
44
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
45
+ end
46
+
47
+ rescue LoadError
48
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
49
+ end
@@ -0,0 +1,42 @@
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
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "ares_cz"
8
+ s.version = "0.3.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Josef Pospisil"]
12
+ s.date = "2012-10-05"
13
+ s.description = "Simple library for querying Ares system in Czech republic with translation of labels."
14
+ s.email = "pepe@gravastar.cz"
15
+ s.extra_rdoc_files = [
16
+ "README.rdoc"
17
+ ]
18
+ s.files = [
19
+ "README.rdoc",
20
+ "Rakefile",
21
+ "ares_cz.gemspec",
22
+ "lib/ares_cz.rb",
23
+ "spec/ares_cz_spec.rb"
24
+ ]
25
+ s.homepage = "http://github.com/pepe/ares"
26
+ s.require_paths = ["lib"]
27
+ s.rubygems_version = "1.8.24"
28
+ s.summary = "Simple ruby wrapper for Czech Ares service"
29
+
30
+ if s.respond_to? :specification_version then
31
+ s.specification_version = 3
32
+
33
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
34
+ s.add_runtime_dependency(%q<crack>, [">= 0.1.4"])
35
+ else
36
+ s.add_dependency(%q<crack>, [">= 0.1.4"])
37
+ end
38
+ else
39
+ s.add_dependency(%q<crack>, [">= 0.1.4"])
40
+ end
41
+ end
42
+
@@ -0,0 +1,103 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'net/http'
3
+ require 'crack/xml'
4
+
5
+ class Ares
6
+ SERVICE_URL = "http://wwwinfo.mfcr.cz/cgi-bin/ares/darv_std.cgi?%s".freeze
7
+ attr_reader :options, :result
8
+
9
+ # class methods
10
+ class << self
11
+ # finds subject by any part on ares service
12
+ def find(options)
13
+ return new(options)
14
+ end
15
+ end
16
+
17
+ # initializes new ares object
18
+ def initialize(options)
19
+ @options = options
20
+ @result = Crack::XML.parse(Net::HTTP.get(URI.parse(SERVICE_URL % self.params)))
21
+ return self
22
+ end
23
+
24
+ # returns true if subject found on ares, otherwise false
25
+ def found?
26
+ @found ||= !(self.result["are:Ares_odpovedi"]["are:Odpoved"]["are:Pocet_zaznamu"] == '0' ||
27
+ self.result["are:Ares_odpovedi"]["are:Odpoved"]["are:Error"])
28
+ end
29
+
30
+ # returns params like concatenated options
31
+ def params
32
+ sanitize_options
33
+ @params ||= URI.escape(@options.sort.inject([]) do |res, pair|
34
+ res << "%s=%s" % [pair.first, pair.last]
35
+ end.join('&'))
36
+ end
37
+
38
+ # returns just answer part
39
+ def answer
40
+ @answer ||= self.result["are:Ares_odpovedi"]["are:Odpoved"]["are:Zaznam"]
41
+ end
42
+
43
+ # returns company name
44
+ def company_name
45
+ @company_name ||= self.answer["are:Obchodni_firma"]
46
+ end
47
+
48
+ # returns ico
49
+ def ico
50
+ @ico ||= self.answer["are:ICO"]
51
+ end
52
+
53
+ # returns subject type
54
+ def subject_type
55
+ @subject_type ||= if self.answer["are:Identifikace"]["are:Osoba"].nil?
56
+ "P"
57
+ end
58
+ end
59
+
60
+ # returns address
61
+ def address
62
+ unless @address
63
+ @address = {
64
+ :city => self.raw_address['dtt:PSC'][0..0] == "1" ? self.raw_address['dtt:Nazev_mestske_casti'] : self.raw_address['dtt:Nazev_obce'],
65
+ :street => [street_name, building_number].join(' '),
66
+ :street_name => street_name,
67
+ :building_number => building_number,
68
+ :zip => self.raw_address['dtt:PSC']
69
+ }
70
+ @address[:country] = "Česká republika" if self.raw_address['dtt:Kod_statu'].to_i == 203
71
+ end
72
+ return @address
73
+ end
74
+
75
+ # returns raw address
76
+ def raw_address
77
+ @raw_address ||= if self.subject_type == "P"
78
+ self.answer["are:Identifikace"]["are:Adresa_ARES"]
79
+ end
80
+ end
81
+
82
+ private
83
+ # returns street name
84
+ def street_name
85
+ self.raw_address["dtt:Nazev_ulice"]
86
+ end
87
+
88
+ # returns building number
89
+ def building_number
90
+ [self.raw_address['dtt:Cislo_domovni'],
91
+ self.raw_address['dtt:Cislo_orientacni']].compact.join('/')
92
+ end
93
+
94
+ # sanitizes url options
95
+ def sanitize_options
96
+ @options = @options.inject({}) do |opts, (key, value)|
97
+ opts[key.to_s] = value
98
+ opts[key.to_s || key] = value
99
+ opts
100
+ end
101
+ @options['ico'] = @options['ico'].gsub(/ /, '')
102
+ end
103
+ end
@@ -0,0 +1,185 @@
1
+ require 'rubygems'
2
+ require 'rspec'
3
+ require 'lib/ares_cz'
4
+
5
+ SERVICE_URL = "http://wwwinfo.mfcr.cz/cgi-bin/ares/darv_std.cgi?%s".freeze
6
+ describe "ares" do
7
+ # mocks net/http to return gravastar result
8
+ def mock_found
9
+ @test_xml = <<TEST_XML
10
+ <?xml version="1.0" encoding="UTF-8"?>
11
+ <are:Ares_odpovedi xmlns:are="http://wwwinfo.mfcr.cz/ares/xml_doc/schemas/ares/ares_answer/v_1.0.1" xmlns:dtt="http://wwwinfo.mfcr.cz/ares/xml_doc/schemas/ares/ares_datatypes/v_1.0.4" xmlns:udt="http://wwwinfo.mfcr.cz/ares/xml_doc/schemas/uvis_datatypes/v_1.0.1" odpoved_datum_cas="2009-08-07T10:55:41" odpoved_pocet="1" odpoved_typ="Standard" vystup_format="XML" xslt="klient" validation_XSLT="/ares/xml_doc/schemas/ares/ares_answer/v_1.0.0/ares_answer.xsl" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://wwwinfo.mfcr.cz/ares/xml_doc/schemas/ares/ares_answer/v_1.0.1 http://wwwinfo.mfcr.cz/ares/xml_doc/schemas/ares/ares_answer/v_1.0.1/ares_answer_v_1.0.1.xsd" Id="ares">
12
+ <are:Odpoved>
13
+ <are:Pocet_zaznamu>1</are:Pocet_zaznamu>
14
+ <are:Typ_vyhledani>FREE</are:Typ_vyhledani>
15
+ <are:Zaznam>
16
+ <are:Shoda_ICO>
17
+ <dtt:Kod>9</dtt:Kod>
18
+ </are:Shoda_ICO>
19
+ <are:Vyhledano_dle>ICO</are:Vyhledano_dle>
20
+ <are:Typ_registru>
21
+ <dtt:Kod>2</dtt:Kod>
22
+
23
+ <dtt:Text>OR</dtt:Text>
24
+ </are:Typ_registru>
25
+ <are:Datum_vzniku>2005-11-03</are:Datum_vzniku>
26
+ <are:Datum_platnosti>2009-08-07</are:Datum_platnosti>
27
+ <are:Pravni_forma>
28
+ <dtt:Kod_PF>112</dtt:Kod_PF>
29
+ </are:Pravni_forma>
30
+ <are:Obchodni_firma>GravaStar s.r.o.</are:Obchodni_firma>
31
+ <are:ICO>27386830</are:ICO>
32
+ <are:Identifikace>
33
+ <are:Adresa_ARES>
34
+
35
+ <dtt:ID_adresy>202396372</dtt:ID_adresy>
36
+ <dtt:Kod_statu>203</dtt:Kod_statu>
37
+ <dtt:Nazev_okresu>Hlavní město Praha</dtt:Nazev_okresu>
38
+ <dtt:Nazev_obce>Praha</dtt:Nazev_obce>
39
+ <dtt:Nazev_casti_obce>Bubeneč</dtt:Nazev_casti_obce>
40
+ <dtt:Nazev_mestske_casti>Praha 6</dtt:Nazev_mestske_casti>
41
+ <dtt:Nazev_ulice>Charlese de Gaulla</dtt:Nazev_ulice>
42
+ <dtt:Cislo_domovni>800</dtt:Cislo_domovni>
43
+ <dtt:Cislo_orientacni>3</dtt:Cislo_orientacni>
44
+
45
+ <dtt:PSC>16000</dtt:PSC>
46
+ <dtt:Adresa_UIR>
47
+ <udt:Kod_oblasti>19</udt:Kod_oblasti>
48
+ <udt:Kod_kraje>19</udt:Kod_kraje>
49
+ <udt:Kod_okresu>3100</udt:Kod_okresu>
50
+ <udt:Kod_obce>554782</udt:Kod_obce>
51
+ <udt:Kod_pobvod>60</udt:Kod_pobvod>
52
+ <udt:Kod_sobvod>60</udt:Kod_sobvod>
53
+ <udt:Kod_casti_obce>490024</udt:Kod_casti_obce>
54
+
55
+ <udt:Kod_mestske_casti>500178</udt:Kod_mestske_casti>
56
+ <udt:PSC>16000</udt:PSC>
57
+ <udt:Kod_ulice>470813</udt:Kod_ulice>
58
+ <udt:Cislo_domovni>800</udt:Cislo_domovni>
59
+ <udt:Typ_cislo_domovni>1</udt:Typ_cislo_domovni>
60
+ <udt:Cislo_orientacni>3</udt:Cislo_orientacni>
61
+ <udt:Kod_adresy>22182616</udt:Kod_adresy>
62
+ <udt:Kod_objektu>22099042</udt:Kod_objektu>
63
+ <udt:PCD>2495506</udt:PCD>
64
+
65
+ </dtt:Adresa_UIR>
66
+ </are:Adresa_ARES>
67
+ </are:Identifikace>
68
+ <are:Kod_FU>6</are:Kod_FU>
69
+ <are:Priznaky_subjektu>NAAANANNNNNNNNNNNNNNNNNNNNNNNN</are:Priznaky_subjektu>
70
+ </are:Zaznam>
71
+ </are:Odpoved>
72
+ </are:Ares_odpovedi>
73
+ TEST_XML
74
+ Net::HTTP.stub!(:get).and_return(@test_xml)
75
+ end
76
+
77
+ # mocks net/http to return empty result
78
+ def mock_not_found
79
+ @test_xml = <<TEST_XML
80
+ <?xml version="1.0" encoding="UTF-8"?>
81
+ <are:Ares_odpovedi xmlns:are="http://wwwinfo.mfcr.cz/ares/xml_doc/schemas/ares/ares_answer/v_1.0.1" xmlns:dtt="http://wwwinfo.mfcr.cz/ares/xml_doc/schemas/ares/ares_datatypes/v_1.0.4" xmlns:udt="http://wwwinfo.mfcr.cz/ares/xml_doc/schemas/uvis_datatypes/v_1.0.1" odpoved_datum_cas="2009-08-07T10:49:40" odpoved_pocet="1" odpoved_typ="Standard" vystup_format="XML" xslt="klient" validation_XSLT="/ares/xml_doc/schemas/ares/ares_answer/v_1.0.0/ares_answer.xsl" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://wwwinfo.mfcr.cz/ares/xml_doc/schemas/ares/ares_answer/v_1.0.1 http://wwwinfo.mfcr.cz/ares/xml_doc/schemas/ares/ares_answer/v_1.0.1/ares_answer_v_1.0.1.xsd" Id="ares">
82
+ <are:Odpoved>
83
+ <are:Pocet_zaznamu>0</are:Pocet_zaznamu>
84
+ <are:Typ_vyhledani>FREE</are:Typ_vyhledani>
85
+ </are:Odpoved>
86
+ </are:Ares_odpovedi>
87
+ TEST_XML
88
+ Net::HTTP.should_receive(:get).at_least(:once).and_return(@test_xml)
89
+ end
90
+
91
+ describe "initialization and basic methods" do
92
+ before(:each) do
93
+ mock_not_found
94
+ end
95
+
96
+ it "should be created by finder" do
97
+ mock_found
98
+ ares = Ares.find(:ico => '666')
99
+ ares.should_not be_nil
100
+ ares.class.should == Ares
101
+ end
102
+
103
+ it "should store options" do
104
+ mock_found
105
+ ares = Ares.find(:ico => '666')
106
+ ares.options.should == {"ico" => '666'}
107
+ end
108
+
109
+ it "should have params with equal sign concatenated options" do
110
+ mock_found
111
+ ares = Ares.find(:ico => '27386830')
112
+ ares.params.should == "ico=27386830"
113
+ ares = Ares.find(:ico => '27386830', :obchodni_firma => 'Grava')
114
+ ares.params.should == "ico=27386830&obchodni_firma=Grava"
115
+ ares = Ares.find(:ico => '27386 830')
116
+ ares.params.should == "ico=27386830"
117
+ end
118
+
119
+ it "should have result with hash from xml" do
120
+ ares = Ares.find(:ico => '27386830')
121
+ ares.result.class.should == Hash
122
+ end
123
+ end
124
+
125
+ describe "founding" do
126
+ it "should not find not existing subject on ares" do
127
+ mock_not_found
128
+ ares = Ares.find(:ico => '666')
129
+ ares.found?.should be_false
130
+ end
131
+
132
+ it "should find existing subject on ares" do
133
+ mock_found
134
+ ares = Ares.find(:ico => '27386830')
135
+ ares.found?.should be_true
136
+ end
137
+
138
+ it "should not find when there is ico with not suitable chars" do
139
+ mock_not_found
140
+ ares = Ares.find(:ico => '27386$830')
141
+ ares.found?.should be_false
142
+ end
143
+
144
+ it "should find when there is ico with spaces in it" do
145
+ mock_not_found
146
+ ares = Ares.find(:ico => '27386$830')
147
+ ares.found?.should be_false
148
+ end
149
+ end
150
+
151
+ describe "parser" do
152
+ it "should parse company_name" do
153
+ mock_found
154
+ ares = Ares.find(:ico => '27386830')
155
+ ares.company_name.should == "GravaStar s.r.o."
156
+ end
157
+
158
+ it "should parse ico" do
159
+ mock_found
160
+ ares = Ares.find(:ico => '27386830')
161
+ ares.ico.should == '27386830'
162
+ end
163
+
164
+ it "should parse subject type" do
165
+ mock_found
166
+ ares = Ares.find(:ico => '27386830')
167
+ ares.subject_type.should == 'P'
168
+ end
169
+
170
+ it "should parse address" do
171
+ mock_found
172
+ ares = Ares.find(:ico => '27386830')
173
+ ares.address.is_a?(Hash).should be_true
174
+ ares.address.should == {
175
+ :city => "Praha 6",
176
+ :street => "Charlese de Gaulla 800/3",
177
+ :street_name => "Charlese de Gaulla",
178
+ :building_number => "800/3",
179
+ :zip => '16000',
180
+ :country => "Česká republika"
181
+ }
182
+ end
183
+ end
184
+ end
185
+
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ares_cz
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Josef Pospisil
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: crack
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.1.4
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: 0.1.4
30
+ description: Simple library for querying Ares system in Czech republic with translation
31
+ of labels.
32
+ email: pepe@gravastar.cz
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files:
36
+ - README.rdoc
37
+ files:
38
+ - README.rdoc
39
+ - Rakefile
40
+ - ares_cz.gemspec
41
+ - lib/ares_cz.rb
42
+ - spec/ares_cz_spec.rb
43
+ homepage: http://github.com/pepe/ares
44
+ licenses: []
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubyforge_project:
63
+ rubygems_version: 1.8.24
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: Simple ruby wrapper for Czech Ares service
67
+ test_files: []