diff --git a/Gemfile.lock b/Gemfile.lock index 3d6b917..a63fc80 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - ruby_ares (0.1.0) + ruby_ares (0.2.0) GEM remote: https://rubygems.org/ diff --git a/README.md b/README.md index b0c45b6..a29b183 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ This gem is not implementing whole ARES API, only the part that allow to get inf Add this line to your application's Gemfile: ```ruby -gem "ruby_ares", "~> 0.1.0", github: "railsformers/ruby_ares" +gem "ruby_ares", "~> 0.2.0", github: "railsformers/ruby_ares" ``` ## Usage @@ -77,6 +77,13 @@ Return data is default type of `OpenStruct`. If you prefer `Hash` type, you can } ``` +To get ARES information about a company in more detail, use the `PublicRegister` class: + +```ruby + response = RubyAres::PublicRegister.get("24704440") + file_mark(response) # use of file mark parser +``` + ### Exceptions In case of an error, the `Ares` class will raise an `RubyAres::Error` exception. The exception will contain the error message from ARES, response status, type and response. diff --git a/lib/ruby_ares.rb b/lib/ruby_ares.rb index a0f18f4..3de13dd 100644 --- a/lib/ruby_ares.rb +++ b/lib/ruby_ares.rb @@ -1,7 +1,9 @@ # frozen_string_literal: true require_relative "ruby_ares/version" +require_relative "ruby_ares/base" require_relative "ruby_ares/subject" +require_relative "ruby_ares/public_register" module RubyAres class Error < StandardError diff --git a/lib/ruby_ares/base.rb b/lib/ruby_ares/base.rb new file mode 100644 index 0000000..398538e --- /dev/null +++ b/lib/ruby_ares/base.rb @@ -0,0 +1,29 @@ +require 'ostruct' + +module RubyAres + class Base + class << self + def get(ico, object_class = OpenStruct) + uri = URI("#{base_url}/#{ico}") + + response = Net::HTTP.get_response(uri) + + case response + when Net::HTTPSuccess then + JSON.parse(response.body, object_class: object_class) + when Net::HTTPRedirection then + location = response['location'] + warn "redirected to #{location}" + fetch(location, limit - 1) + else + raise RubyAres::Error.new( + msg: "Request failed with #{response.code}", + type: response.class.name, + status: response.code, + response: (JSON.parse(response.body) rescue response.body) + ) + end + end + end + end +end diff --git a/lib/ruby_ares/public_register.rb b/lib/ruby_ares/public_register.rb new file mode 100644 index 0000000..a2e54f6 --- /dev/null +++ b/lib/ruby_ares/public_register.rb @@ -0,0 +1,43 @@ +module RubyAres + class PublicRegister < Base + COURT_CODES = { + "MSPH" => "Městský soud v Praze", + "KSPH" => "Krajský soud v Praze", + "KSCB" => "Krajský soud v Českých Budějovicích", + "KSTB" => "Krajský soud v Českých Budějovicích – pobočka v Táboře", + "KSPL" => "Krajský soud v Plzni", + "KSKV" => "Krajský soud v Plzni – pobočka v Karlových Varech", + "KSUL" => "Krajský soud v Ústí nad Labem", + "KSLB" => "Krajský soud v Ústí nad Labem – pobočka v Liberci", + "KSHK" => "Krajský soud v Hradci Králové", + "KSPA" => "Krajský soud v Hradci Králové – pobočka v Pardubicích", + "KSBR" => "Krajský soud v Brně", + "KSJI" => "Krajský soud v Brně – pobočka v Jihlavě", + "KSZL" => "Krajský soud v Brně – pobočka ve Zlíně", + "KSOS" => "Krajský soud v Ostravě", + "KSOL" => "Krajský soud v Ostravě – pobočka v Olomouci", + "VSPH" => "Vrchní soud v Praze", + "VSOL" => "Vrchní soud v Olomouci", + "NSCR" => "Nejvyšší soud České republiky", + "MSp" => "Společný soud" + }.freeze + + class << self + def base_url + 'https://ares.gov.cz/ekonomicke-subjekty-v-be/rest/ekonomicke-subjekty-vr'.freeze + end + + def file_mark(json) + record = json.zaznamy.select { |pr| pr.primarniZaznam == true }.first.spisovaZnacka.select { |sz| !sz.datumVymazu }.first + court_code = record.soud + court_name = COURT_CODES[court_code] || court_code + + { + court_name: court_name, + first_number: record.oddil, + second_number: record.vlozka + } + end + end + end +end diff --git a/lib/ruby_ares/subject.rb b/lib/ruby_ares/subject.rb index 30ae1f9..c87f63a 100644 --- a/lib/ruby_ares/subject.rb +++ b/lib/ruby_ares/subject.rb @@ -1,30 +1,8 @@ -require 'ostruct' - module RubyAres - class Subject - BASE_URL = 'https://ares.gov.cz/ekonomicke-subjekty-v-be/rest/ekonomicke-subjekty'.freeze - + class Subject < Base class << self - def get(ico, object_class = OpenStruct) - uri = URI("#{BASE_URL}/#{ico}") - - response = Net::HTTP.get_response(uri) - - case response - when Net::HTTPSuccess then - JSON.parse(response.body, object_class: object_class) - when Net::HTTPRedirection then - location = response['location'] - warn "redirected to #{location}" - fetch(location, limit - 1) - else - raise RubyAres::Error.new( - msg: "Request failed with #{response.code}", - type: response.class.name, - status: response.code, - response: (JSON.parse(response.body) rescue response.body) - ) - end + def base_url + 'https://ares.gov.cz/ekonomicke-subjekty-v-be/rest/ekonomicke-subjekty'.freeze end end end diff --git a/lib/ruby_ares/version.rb b/lib/ruby_ares/version.rb index 5976c2f..288af18 100644 --- a/lib/ruby_ares/version.rb +++ b/lib/ruby_ares/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module RubyAres - VERSION = "0.1.0" + VERSION = "0.2.0" end diff --git a/spec/public_register_spec.rb b/spec/public_register_spec.rb new file mode 100644 index 0000000..21e8e87 --- /dev/null +++ b/spec/public_register_spec.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +RSpec.describe RubyAres::PublicRegister do + let(:ico) { '26168685' } + + subject { RubyAres::PublicRegister.get(ico) } + + before do + stub_request(:get, "#{RubyAres::PublicRegister.base_url}/#{ico}") + .to_return( + body: + { + "icoId":"26168685", + "zaznamy":[ + {"rejstrik":"OR","primarniZaznam":true,"spisovaZnacka":[{"datumZapisu":"2000-04-05","soud":"MSPH","oddil":"B","vlozka":6493}]} + ] + }.to_json + ) + end + + describe 'get' do + it 'returns result as OpenStruct' do + expect(subject).to be_a(OpenStruct) + expect(subject.icoId).to eq(ico) + end + + context 'Hash structure' do + subject { RubyAres::PublicRegister.get(ico, Hash) } + + it 'returns result as Hash' do + expect(subject).to be_a(Hash) + end + end + end + + describe 'file_mark' do + it 'returns result as Hash' do + expect(RubyAres::PublicRegister.file_mark(subject)).to eq({ + court_name: RubyAres::PublicRegister::COURT_CODES['MSPH'], + first_number: 'B', + second_number: 6493 + }) + end + end +end diff --git a/spec/subject_spec.rb b/spec/subject_spec.rb index 9d09b1e..15a0181 100644 --- a/spec/subject_spec.rb +++ b/spec/subject_spec.rb @@ -7,7 +7,7 @@ describe 'get' do before do - stub_request(:get, "#{RubyAres::Subject::BASE_URL}/#{ico}") + stub_request(:get, "#{RubyAres::Subject.base_url}/#{ico}") .to_return( body: {