Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added endpoint for get company details #6

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
/spec/reports/
/tmp/
Gemfile.lock
.DS_Store
Empty file modified bin/console
100644 → 100755
Empty file.
Empty file modified bin/setup
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion company_register.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Gem::Specification.new do |spec|
spec.name = 'company_register'
spec.version = CompanyRegister::VERSION
spec.authors = ['Artur Beljajev', 'Maciej Szlosarczyk']
spec.email = ['[email protected]', '[email protected]']
spec.email = ['[email protected]', '[email protected]', '[email protected]']
spec.summary = 'A Ruby interface to the Estonian company register (Äriregister) API'
spec.homepage = 'https://github.com/internetee/company_register'
spec.license = 'MIT'
Expand Down
3 changes: 3 additions & 0 deletions lib/company_register.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
require 'company_register/configuration'
require 'company_register/request'
require 'company_register/request/representation_rights'
require 'company_register/request/simple_data'
require 'company_register/request/company_details'
require 'company_register/request/entries_and_rulings'

module CompanyRegister
class Error < StandardError; end
Expand Down
154 changes: 147 additions & 7 deletions lib/company_register/client.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
module CompanyRegister
Company = Struct.new(:registration_number, :company_name)
Company = Struct.new(:registration_number, :company_name, :status)

CompanyDetails = Struct.new(
:registration_number,
:company_name,
:status,
:kandeliik,
)

class Client
# API returns request params back with the response. They are stripped out to avoid caching
Expand All @@ -18,24 +25,73 @@ def representation_rights(citizen_personal_code:, citizen_country_code:)
keel: 'eng' }

request = Request::RepresentationRightsRequest.new(search_params)
response_body = cache_store.fetch(request,
expires_in: CompanyRegister.configuration.cache_period) do
response_body = cache_store.fetch(request, expires_in: CompanyRegister.configuration.cache_period) do
response = request.perform
filter_response_body(response.body)
filter_response_body(response.body, :esindus_v1_response)
end

parse_representation_rights_response_body(response_body)
end

def simple_data(registration_number:)
search_params = { ariregistri_kood: registration_number, keel: 'eng' }

request = Request::SimpleDataRequest.new(search_params)
response = request.perform
response_body = filter_response_body(response.body, :lihtandmed_v2_response)

parse_simple_data_response_body(response_body)
end

# yandmed - If General Data is requested
# iandmed - If Personnel Data is requested
# kandmed - If Commercial Pledga data is requested
# dandmed - If data of applications in proceeding is requested
# maarused - If rulings are required
def company_details(registration_number:, is_general_data: 1, is_personnel_data: 1, is_commercial_pledga: 0, is_data_proceed: 0, is_ruling: 0)
search_params = {
ariregistri_kood: registration_number,
yandmed: is_general_data,
iandmed: is_personnel_data,
kandmed: is_commercial_pledga,
dandmed: is_data_proceed,
maarused: is_ruling
}

request = Request::CompanyDetailsRequest.new(search_params)
response = request.perform
response_body = filter_response_body(response.body, :detailandmed_v2_response)

parse_company_details_response_body(response_body)
end

# liik: K – request for entries; M – request for rulings.
# algus_kpv: The input already takes into account summer and winter time. The maximum search period is one day. The date must not be less than 01.01.2013
# lopp_kpv: The input already takes into account summer and winter time. The maximum search period is one day. The date must not be less than the start date.
def entries_and_rulings(request_type: 'M', start_at: '2019-01-18T11:57:00', ends_at: '2019-01-19T11:57:00', lang: 'eng')
search_params = {
keel: lang,
liik: request_type,
algus_kpv: start_at,
lopp_kpv: ends_at
}

request = Request::EntriesAndRulingsRequest.new(search_params)
response = request.perform
response_body = filter_response_body(response.body, :kanded_maarused_v1_response)

parse_entries_and_rulings_response_body(response_body)
end

private

attr_reader :cache_store

def filter_response_body(body)
def filter_response_body(body, response_type)
# Avoid parameter mutation
body = Marshal.load(Marshal.dump(body))

body[:esindus_v1_response][:paring].delete_if do |key, _value|
body[response_type][:paring].delete_if do |key, _value|
RESPONSE_FILTERED_PARAMS.include?(key)
end

Expand All @@ -51,5 +107,89 @@ def parse_representation_rights_response_body(body)
Company.new(item[:ariregistri_kood], item[:arinimi])
end
end

def parse_simple_data_response_body(body)
return [] unless body[:lihtandmed_v2_response][:keha][:ettevotjad]

items = body[:lihtandmed_v2_response][:keha][:ettevotjad][:item]
items = [items] unless items.kind_of?(Array)
items.map do |item|
Company.new(item[:ariregistri_kood], item[:evnimi], item[:staatus])
end
end

def parse_company_details_response_body(body)
return [] unless body[:detailandmed_v2_response][:keha][:ettevotjad]

items = body[:detailandmed_v2_response][:keha][:ettevotjad][:item]
items = [items] unless items.kind_of?(Array)
items.map do |item|
kandeliik = nil

kandeliik = if item[:registrikaardid][:item].kind_of?(Array)
return_list_of_registry_cards(item)
else
return_list_of_kanded_items(item)
end

CompanyDetails.new(
item[:ariregistri_kood], item[:nimi], item[:yldandmed][:staatus], kandeliik
)
end
end

def return_list_of_registry_cards(item)
item[:registrikaardid][:item].map do |i|
i[:kanded][:item].map do |entry|
Struct.new(:kandeliik, :kandeliik_tekstina, :kande_kpv)
.new(entry[:kandeliik], entry[:kandeliik_tekstina], entry[:kande_kpv])
end
end
end

def return_list_of_kanded_items(item)
if item[:registrikaardid][:item][:kanded][:item].kind_of?(Array)

item[:registrikaardid][:item][:kanded].map do |reg_pair|
reg_pair[1].map do |reg|

Struct.new(:kandeliik, :kandeliik_tekstina, :kande_kpv)
.new(reg[:kandeliik], reg[:kandeliik_tekstina], reg[:kande_kpv])
end
end

else
Struct.new(:kandeliik, :kandeliik_tekstina, :kande_kpv)
.new(
item[:registrikaardid][:item][:kanded][:item][:kandeliik],
item[:registrikaardid][:item][:kanded][:item][:kandeliik_tekstina],
item[:registrikaardid][:item][:kanded][:item][:kande_kpv]
)
end
end

def parse_entries_and_rulings_response_body(body)
return [] unless body[:kanded_maarused_v1_response][:keha][:ettevotjad]

items = body[:kanded_maarused_v1_response][:keha][:ettevotjad][:item]
items = [items] unless items.kind_of?(Array)
items.map do |item|
maarused = unless item[:maarused][:item][0].nil?
Struct.new(:kandeliik, :kandeliik_tekstina, :maaruse_liik, :maaruse_liik_tekstina)
.new(
item[:maarused][:item][0][:kandeliik],
item[:maarused][:item][0][:kandeliik_tekstina],
item[:maarused][:item][0][:maaruse_liik],
item[:maarused][:item][0][:maaruse_liik_tekstina]
)
else
[]
end

CompanyDetails.new(
item[:ariregistri_kood], item[:evnimi], item[:staatus], maarused
)
end
end
end
end
end
11 changes: 11 additions & 0 deletions lib/company_register/request/company_details.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module CompanyRegister
class Request
class CompanyDetailsRequest < Request
private

def soap_operation
:detailandmed_v2
end
end
end
end
11 changes: 11 additions & 0 deletions lib/company_register/request/entries_and_rulings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module CompanyRegister
class Request
class EntriesAndRulingsRequest < Request
private

def soap_operation
:kanded_maarused_v1
end
end
end
end
11 changes: 11 additions & 0 deletions lib/company_register/request/simple_data.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module CompanyRegister
class Request
class SimpleDataRequest < Request
private

def soap_operation
:lihtandmed_v2
end
end
end
end
9 changes: 9 additions & 0 deletions test/client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,15 @@ def stub_request_with_payload
headers: { 'Soapaction' => '"esindus_v1"' }).to_return(status: 200, body: response_body)
end

# TODO:
# def stub_request_with_payload_of_company_details
# response_body = File.read('test/fixtures/deleted_company.xml')
# stub_request(:post, 'https://ariregxmlv6.rik.ee/').
# with(
# body: "<?xml version=\"1.0\" encoding=\"UTF-8\"?><env:Envelope xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:ar=\"http://arireg.x-road.eu/producer/\" xmlns:env=\"http://schemas.xmlsoap.org/soap/envelope/\"><env:Body><ar:esindus_v1><ar:keha><ar:ariregister_kasutajanimi>john</ar:ariregister_kasutajanimi><ar:ariregister_parool>pwd</ar:ariregister_parool><ar:fyysilise_isiku_kood>1234</ar:fyysilise_isiku_kood><ar:fyysilise_isiku_koodi_riik>USA</ar:fyysilise_isiku_koodi_riik><ar:keel>eng</ar:keel></ar:keha></ar:esindus_v1></env:Body></env:Envelope>",
# headers: { 'Soapaction' => '"esindus_v1"' }).to_return(status: 200, body: response_body)
# end

def stub_request_without_payload
response_body = File.read('test/fixtures/representation_rights_response_without_payload.xml')
stub_request(:post, 'https://ariregxmlv6.rik.ee/').to_return(status: 200,
Expand Down
Loading