kubicek-ares 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/README.rdoc +30 -0
  2. data/Rakefile +44 -0
  3. data/lib/ares.rb +77 -0
  4. data/spec/ares_spec.rb +165 -0
  5. metadata +66 -0
@@ -0,0 +1,30 @@
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
+ ares = Ares.find(:ico => '27386830')
13
+ ares.found? == true
14
+ ares.company_name == "GravaStar s.r.o."
15
+ ares.subject_type == "P"
16
+ ares.address == {
17
+ :city => "16000 Praha",
18
+ :street => "Charlese de Gaulla 800/3",
19
+ :country => "Česká republika"
20
+ }
21
+ ares = Ares.find(:ico => '666')
22
+ ares.found? == false
23
+
24
+ It should be posible to find by company name, but is not tested. Also subject
25
+ type F needs more testing (see rake test:rcov).
26
+
27
+ == Dependencies
28
+
29
+ Only active support for Hash.from_xml. Maybe it should be redone with something
30
+ less fat.
@@ -0,0 +1,44 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'ftools'
4
+ require 'spec'
5
+ require 'spec/rake/spectask'
6
+
7
+ desc "Runs all specs as default"
8
+ task :default => 'test:spec'
9
+
10
+ namespace :test do
11
+ desc "Run all specs"
12
+ Spec::Rake::SpecTask.new do |t|
13
+ t.spec_files = FileList['spec/*_spec.rb']
14
+ t.spec_opts = ['-u']
15
+ end
16
+
17
+
18
+
19
+ desc "Run all examples with RCov"
20
+ Spec::Rake::SpecTask.new('rcov') do |t|
21
+ t.spec_files = FileList['spec/*_spec.rb']
22
+ t.rcov = true
23
+ t.rcov_opts = ['--exclude', 'spec']
24
+ end
25
+ end
26
+
27
+ begin
28
+ require 'jeweler'
29
+ Jeweler::Tasks.new do |gem|
30
+ gem.name = "ares"
31
+ gem.summary = %Q{Simple ruby wrapper for Czech Ares service}
32
+ gem.email = "pepe@gravastar.cz"
33
+ gem.homepage = "http://github.com/pepe/ares"
34
+ gem.authors = ["Josef Pospisil"]
35
+ gem.description = "Simple library for querying Ares system in Czech republic with translation of labels."
36
+ gem.add_dependency('activesupport', '>= 1.4.0')
37
+ IGNORE = [/\.gitignore$/, /VERSION$/]
38
+ gem.files.reject! { |f| IGNORE.any? { |re| f.match(re) } }
39
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
40
+ end
41
+
42
+ rescue LoadError
43
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
44
+ end
@@ -0,0 +1,77 @@
1
+ require 'net/http'
2
+ require 'active_support'
3
+ class Ares
4
+ SERVICE_URL = "http://wwwinfo.mfcr.cz/cgi-bin/ares/darv_std.cgi?%s".freeze
5
+ attr_reader :options, :result
6
+
7
+ # class methods
8
+ class << self
9
+ # finds subject by any part on ares service
10
+ def find(options)
11
+ return new(options)
12
+ end
13
+ end
14
+
15
+ # initializes new ares object
16
+ def initialize(options)
17
+ @options = options
18
+ @result = Hash.from_xml(Net::HTTP.get(URI.parse(SERVICE_URL % self.params)))
19
+ return self
20
+ end
21
+
22
+ # returns true if subject found on ares, otherwise false
23
+ def found?
24
+ @found ||= !(self.result["Ares_odpovedi"]["Odpoved"]["Pocet_zaznamu"] == '0' ||
25
+ self.result["Ares_odpovedi"]["Odpoved"]["error"])
26
+ end
27
+
28
+ # returns params like concatenated options
29
+ def params
30
+ @params ||= options.inject([]) do |res, pair|
31
+ res << "%s=%s" % [pair.first, pair.last]
32
+ end.join('&')
33
+ end
34
+
35
+ # returns just answer part
36
+ def answer
37
+ @answer ||= self.result["Ares_odpovedi"]["Odpoved"]["Zaznam"]
38
+ end
39
+
40
+ # returns company name
41
+ def company_name
42
+ @company_name ||= self.answer["Obchodni_firma"]
43
+ end
44
+
45
+ # returns ico
46
+ def ico
47
+ @company_name ||= self.answer["ICO"]
48
+ end
49
+
50
+ # returns subject type
51
+ def subject_type
52
+ @subject_type ||= if self.answer["Identifikace"]["Osoba"].nil?
53
+ "P"
54
+ else
55
+ "F"
56
+ end
57
+ end
58
+
59
+ # returns address
60
+ def address
61
+ @address ||= {
62
+ :city => self.raw_address['PSC'][0..0] == "1" ? self.raw_address['Nazev_mestske_casti'] : self.raw_address['Nazev_obce'],
63
+ :street => [self.raw_address["Nazev_ulice"], [self.raw_address['Cislo_domovni'],self.raw_address['Cislo_orientacni']].compact.join('/')].join(' '),
64
+ :zip => self.raw_address['PSC']
65
+
66
+ }
67
+ end
68
+
69
+ # returns raw address
70
+ def raw_address
71
+ @raw_address ||= if self.subject_type == "P"
72
+ self.answer["Identifikace"]["Adresa_ARES"]
73
+ else
74
+ self.answer["Identifikace"]["Osoba"]["Bydliste"]
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,165 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+ require 'lib/ares'
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.stub!(:get).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
+ ares = Ares.find(:ico => '666')
98
+ ares.should_not be_nil
99
+ ares.class.should == Ares
100
+ end
101
+
102
+ it "should store options" do
103
+ ares = Ares.find(:ico => '666')
104
+ ares.options.should == {:ico => '666'}
105
+ end
106
+
107
+ it "should have params with equal sign concatenated options" do
108
+ ares = Ares.find(:ico => '27386830')
109
+ ares.params.should == "ico=27386830"
110
+ ares = Ares.find(:ico => '27386830', :obchodni_firma => 'Grava')
111
+ ares.params.should == "ico=27386830&obchodni_firma=Grava"
112
+ end
113
+
114
+ it "should have result with hash from xml" do
115
+ ares = Ares.find(:ico => '27386830')
116
+ ares.result.class.should == Hash
117
+ end
118
+ end
119
+
120
+ describe "founding" do
121
+ it "should not find not existing subject on ares" do
122
+ mock_not_found
123
+ ares = Ares.find(:ico => '666')
124
+ ares.found?.should be_false
125
+ end
126
+
127
+ it "should find existing subject on ares" do
128
+ mock_found
129
+ ares = Ares.find(:ico => '27386830')
130
+ ares.found?.should be_true
131
+ end
132
+ end
133
+
134
+ describe "parser" do
135
+ it "should parse company_name" do
136
+ mock_found
137
+ ares = Ares.find(:ico => '27386830')
138
+ ares.company_name.should == "GravaStar s.r.o."
139
+ end
140
+
141
+ it "should parse ico" do
142
+ mock_found
143
+ ares = Ares.find(:ico => '27386830')
144
+ ares.ico.should == '27386830'
145
+ end
146
+
147
+ it "should parse subject type" do
148
+ mock_found
149
+ ares = Ares.find(:ico => '27386830')
150
+ ares.subject_type.should == 'P'
151
+ end
152
+
153
+ it "should parse address" do
154
+ mock_found
155
+ ares = Ares.find(:ico => '27386830')
156
+ ares.address.is_a?(Hash).should be_true
157
+ ares.address.should == {
158
+ :city => "Praha 6",
159
+ :street => "Charlese de Gaulla 800/3",
160
+ :zip => '16000'
161
+ }
162
+ end
163
+ end
164
+ end
165
+
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kubicek-ares
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Josef Pospisil
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-08-07 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activesupport
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.4.0
24
+ version:
25
+ description: Simple library for querying Ares system in Czech republic with translation of labels.
26
+ email: pepe@gravastar.cz
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README.rdoc
33
+ files:
34
+ - README.rdoc
35
+ - Rakefile
36
+ - lib/ares.rb
37
+ - spec/ares_spec.rb
38
+ has_rdoc: true
39
+ homepage: http://github.com/pepe/ares
40
+ licenses:
41
+ post_install_message:
42
+ rdoc_options:
43
+ - --charset=UTF-8
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.3.5
62
+ signing_key:
63
+ specification_version: 2
64
+ summary: Simple ruby wrapper for Czech Ares service
65
+ test_files:
66
+ - spec/ares_spec.rb