Class: EgovUtils::AddressesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/egov_utils/addresses_controller.rb

Instance Method Summary collapse

Instance Method Details

#validate_ruianObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/controllers/egov_utils/addresses_controller.rb', line 5

def validate_ruian
  require 'egon_gate'
  require 'egon_gate/egsb/messages/e37_vyhledej_adresu'

  # TODO: shoud be in some validator - external class
  addr_params = params.require(:address).permit(:city, :postcode, :street, :orientation_number, :house_number)
  address = Address.new(addr_params)
  if (address.city || address.postcode) && (( address.street && address.orientation_number ) || address.house_number)
    message = EgonGate::Egsb::Messages::E37VyhledejAdresu.new
    address.prepare_egon_message(message)
    kobra_reqest = EgonGate::Kobra::Request.new
    response = kobra_reqest.send_message(message)
    if response.error?
      respond_to do |format|
        format.json { render json: { error: response.error_message }, status: 404 }
      end
    else
      egon_address_info = message.parse_response( response.egsb_response )
      address.from_egon_info(egon_address_info)
      respond_to do |format|
        format.json { render json: address }
      end
    end
  else
    respond_to do |format|
      format.json { render json: { error: t(:error_not_found) }, status: 404 }
    end
  end
end