Last active
August 29, 2015 14:24
-
-
Save lukaskonarovsky/8234f9ba70e089b9ba89 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'faraday' | |
require 'multi_json' | |
# set API key in `MERK_API_TOKEN` | |
MerkSource.new('cz').find_by_registration_no('28897501') | |
MerkSource.new('sk').find_by_registration_no('31398871') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# For docs on MERK see: | |
# https://api.merk.cz/docs/#!/suggest/Suggest_list | |
# This operates on endpoint: | |
# https://api.merk.cz:443/suggest/?regno=31398871&country_code=sk | |
# Created in Fakturoid for public use under MIT licence. | |
class MerkSource | |
attr_reader :country | |
MERK_API_ENDPOINT = "https://api.merk.cz:443" | |
MERK_API_TOKEN = "agaca ..." # or Rails.application.secrets.merk_api_key | |
MERK_HEADERS = { accept: 'application/json', user_agent: 'RubyMerkSource' } | |
def initialize(country) | |
@country = country.downcase | |
end | |
def find_by_registration_no(registration_no) | |
response = connection.get "/suggest/", regno: registration_no, country_code: country | |
parse_response(response) | |
end | |
def find_by_email(email) | |
response = connection.get "/suggest/", email: email, country_code: country | |
parse_response(response) | |
end | |
def find_by_name(name) | |
response = connection.get "/suggest/", name: name, country_code: country | |
parse_response(response) | |
end | |
private | |
def connection | |
@connection ||= begin | |
conn = Faraday.new url: MERK_API_ENDPOINT, headers: MERK_HEADERS | |
conn.authorization :Token, MERK_API_TOKEN | |
conn | |
end | |
end | |
def parse_response(response) | |
case response.env.status | |
when 200 then merk_data = MultiJson.load(response.env.body) | |
when 204 then return nil | |
else | |
raise "Merk request failed with code: #{response.env.status} and error: #{response.env.body.to_s}" | |
end | |
if merk_data.is_a?(Array) | |
parse_collection(merk_data) | |
else | |
parse_entity(merk_data) | |
end | |
end | |
def parse_collection(entities) | |
entities.map do |entity| | |
parse_entity(entity) | |
end | |
end | |
def parse_entity(merk_data) | |
subject = {} | |
subject[:name] = merk_data['name'] | |
subject[:registration_no] = merk_data['regno'].to_s | |
subject[:vat_no] = merk_data['vatno'] | |
subject[:registration_date] = merk_data['estab_date'].to_date | |
subject[:legal_form] = merk_data['legal_form_code'].to_s | |
if merk_data['court'].present? | |
subject[:court_name] = merk_data['court'] | |
subject[:court_reg_no] = merk_data['court_file_nr'] | |
end | |
subject[:street] = "#{merk_data['address']['street_fixed']} #{merk_data['address']['number']}".rstrip | |
if merk_data['address']['municipality_part'].include?(merk_data['address']['municipality']) | |
subject[:city] = merk_data['address']['municipality'] | |
else | |
subject[:city] = "#{merk_data['address']['municipality']} - #{merk_data['address']['municipality_part']}".rstrip | |
end | |
subject[:zip] = merk_data['address']['postal_code'] | |
subject[:country] = merk_data['address']['country_code'].presence || subject[:vat_no].try(:[], 0..1) | |
subject[:bank_accounts] = merk_data['bank_accounts'] | |
subject | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment