Skip to content

Commit

Permalink
add public register
Browse files Browse the repository at this point in the history
  • Loading branch information
adammartak committed Jan 24, 2024
1 parent 4216eda commit 6c2e373
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
ruby_ares (0.1.0)
ruby_ares (0.2.0)

GEM
remote: https://rubygems.org/
Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions lib/ruby_ares.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
29 changes: 29 additions & 0 deletions lib/ruby_ares/base.rb
Original file line number Diff line number Diff line change
@@ -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
43 changes: 43 additions & 0 deletions lib/ruby_ares/public_register.rb
Original file line number Diff line number Diff line change
@@ -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
28 changes: 3 additions & 25 deletions lib/ruby_ares/subject.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_ares/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module RubyAres
VERSION = "0.1.0"
VERSION = "0.2.0"
end
45 changes: 45 additions & 0 deletions spec/public_register_spec.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion spec/subject_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:
{
Expand Down

0 comments on commit 6c2e373

Please sign in to comment.