Class: EgovUtils::Address

Inherits:
ApplicationRecord show all
Defined in:
app/models/egov_utils/address.rb

Defined Under Namespace

Classes: Country, District, Region, Town

Constant Summary collapse

CZ_ISO_CODE =
'203'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.countriesObject



25
26
27
28
29
30
31
32
33
# File 'app/models/egov_utils/address.rb', line 25

def self.countries
  return @countries if @countries
  require 'csv'
  @countries = []
  CSV.foreach(EgovUtils::Engine.root.join('config', 'countries.csv'), col_sep: ';', headers: true) do |row|
    @countries << Country.new( row[0], row[1], row[2], row[3]) if row[1]
  end
  @countries
end

.districtsObject



35
36
37
38
39
40
41
42
43
# File 'app/models/egov_utils/address.rb', line 35

def self.districts
  return @districts if @districts
  require 'csv'
  @districts = []
  CSV.foreach(EgovUtils::Engine.root.join('config', 'okres.csv'), col_sep: ';', headers: true) do |row|
    @districts << District.new( row[0].to_i, row[1], row[2].to_i) if row[1]
  end
  @districts
end

.region_for_district(district_name) ⇒ Object



64
65
66
67
# File 'app/models/egov_utils/address.rb', line 64

def self.region_for_district(district_name)
  district = districts.detect{|d| d[:name] == district_name }
  regions.detect{|r| r[:id] == district[:region_id] } if district
end

.regionsObject



45
46
47
48
49
50
51
52
53
# File 'app/models/egov_utils/address.rb', line 45

def self.regions
  return @regions if @regions
  require 'csv'
  @regions = []
  CSV.foreach(EgovUtils::Engine.root.join('config', 'kraj.csv'), col_sep: ';', headers: true) do |row|
    @regions << Region.new( row[0].to_i, row[1]) if row[1]
  end
  @regions
end

.townsObject



55
56
57
58
59
60
61
62
# File 'app/models/egov_utils/address.rb', line 55

def self.towns
  @towns ||= begin
    towns = File.read(EgovUtils::Engine.root.join('config', 'obec.json'))
    JSON.parse(towns).map do |town|
      Town.new(*town.slice('code', 'name', 'district_name').values)
    end
  end
end

Instance Method Details

#country_idsObject



79
80
81
# File 'app/models/egov_utils/address.rb', line 79

def country_ids
  self.class.countries.collect{|r| r[:iso_id]}
end

#district=(value) ⇒ Object



84
85
86
87
# File 'app/models/egov_utils/address.rb', line 84

def district=(value)
  self.region = self.class.region_for_district(value).try(:[], :name)
  super
end

#district_namesObject



73
74
75
# File 'app/models/egov_utils/address.rb', line 73

def district_names
  self.class.districts.collect{|r| r[:name]}
end

#from_egon_info(message) ⇒ Object



114
115
116
117
118
119
120
121
122
123
# File 'app/models/egov_utils/address.rb', line 114

def from_egon_info(message)
  self.egov_identifier = message[:adresni_misto_kod]
  self.district = message[:okres_nazev]
  self.city = message[:obec_nazev]
  self.city_part = message[:cast_obce_nazev]
  self.street = message[:ulice_nazev]
  self.postcode = message[:posta_kod]
  self.house_number = message[:cislo_domovni]
  self.orientation_number = message[:cislo_orientacni]+(message[:cislo_orientacni_pismeno] || '')
end

#full_addressObject



97
98
99
# File 'app/models/egov_utils/address.rb', line 97

def full_address
  to_s
end

#in_czech_republic?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'app/models/egov_utils/address.rb', line 69

def in_czech_republic?
  country == CZ_ISO_CODE || country.nil?
end

#numberObject



89
90
91
92
93
94
95
# File 'app/models/egov_utils/address.rb', line 89

def number
  if !house_number.blank? && !orientation_number.blank?
    "#{house_number}/#{orientation_number}"
  else
    house_number.presence || orientation_number.presence
  end
end

#prepare_egon_message(message) ⇒ Object



105
106
107
108
109
110
111
112
# File 'app/models/egov_utils/address.rb', line 105

def prepare_egon_message(message)
  message.obec_nazev = city.presence
  message.cast_obce_nazev = city_part.presence
  message.ulice_nazev = street.presence
  message.posta_kod = postcode.presence
  message.cislo_domovni = house_number.presence
  message.cislo_orientacni = orientation_number.presence
end

#region_namesObject



76
77
78
# File 'app/models/egov_utils/address.rb', line 76

def region_names
  self.class.regions.collect{|r| r[:name]}
end

#to_sObject



101
102
103
# File 'app/models/egov_utils/address.rb', line 101

def to_s
  "#{street} #{number}, #{postcode} #{city}"
end