Commit efb994a0b620edea70f552af4cef3afb2fb205e9
Committed by
David Silva
1 parent
fd3d68ce
Exists in
master
and in
5 other branches
Remove institution dead code
Signed-off-by: Fabio Teixeira <fabio1079@gmail.com>
Showing
5 changed files
with
0 additions
and
152 deletions
Show diff stats
bin/update_institutions
config/institutions_update.example
config/siorg.yml
| @@ -1,11 +0,0 @@ | @@ -1,11 +0,0 @@ | ||
| 1 | -web_service: | ||
| 2 | - base_url: http://estruturaorganizacional.dados.gov.br/doc/estrutura-organizacional/resumida.json | ||
| 3 | - sphere_codes: | ||
| 4 | - federal: 1 | ||
| 5 | - power_codes: | ||
| 6 | - executive: 1 | ||
| 7 | - legislative: 2 | ||
| 8 | - judiciary: 3 | ||
| 9 | - additional_params: retornarOrgaoEntidadeVinculados=NAO | ||
| 10 | - environment_name: Noosfero | ||
| 11 | - default_community_admin: adminuser | ||
| 12 | \ No newline at end of file | 0 | \ No newline at end of file |
lib/institution_helper.rb
| @@ -1,106 +0,0 @@ | @@ -1,106 +0,0 @@ | ||
| 1 | -require "net/http" | ||
| 2 | -require "yaml" | ||
| 3 | - | ||
| 4 | -module InstitutionHelper | ||
| 5 | - | ||
| 6 | - SPHERES = ['federal'] | ||
| 7 | - POWERS = ['executive', 'legislative', 'judiciary'] | ||
| 8 | - | ||
| 9 | - def self.mass_update | ||
| 10 | - @web_service_info = self.web_service_info | ||
| 11 | - | ||
| 12 | - environment = Environment.find_by_name(@web_service_info["environment_name"]) | ||
| 13 | - | ||
| 14 | - if environment.nil? | ||
| 15 | - return | ||
| 16 | - end | ||
| 17 | - | ||
| 18 | - admin = environment.people.find_by_name(@web_service_info["default_community_admin"]) | ||
| 19 | - | ||
| 20 | - if admin.nil? | ||
| 21 | - return | ||
| 22 | - end | ||
| 23 | - | ||
| 24 | - POWERS.each do |power| | ||
| 25 | - SPHERES.each do |sphere| | ||
| 26 | - json = self.get_json power, sphere | ||
| 27 | - | ||
| 28 | - units = json["unidades"] | ||
| 29 | - | ||
| 30 | - units.each do |unit| | ||
| 31 | - institution = PublicInstitution.where(:name=>unit["nome"]) | ||
| 32 | - | ||
| 33 | - institution = if institution.count == 0 | ||
| 34 | - PublicInstitution::new | ||
| 35 | - else | ||
| 36 | - institution.first | ||
| 37 | - end | ||
| 38 | - | ||
| 39 | - institution = self.set_institution_data institution, unit | ||
| 40 | - institution.save | ||
| 41 | - institution.community.add_admin(admin) | ||
| 42 | - end | ||
| 43 | - end | ||
| 44 | - end | ||
| 45 | - end | ||
| 46 | - | ||
| 47 | - def self.register_institution_modification institution | ||
| 48 | - institution.date_modification = current_date | ||
| 49 | - institution.save | ||
| 50 | - end | ||
| 51 | - | ||
| 52 | - protected | ||
| 53 | - | ||
| 54 | - def self.web_service_info | ||
| 55 | - YAML.load_file(File.dirname(__FILE__) + '/../config/siorg.yml')['web_service'] | ||
| 56 | - end | ||
| 57 | - | ||
| 58 | - def self.service_url power, sphere | ||
| 59 | - base_url = @web_service_info['base_url'] | ||
| 60 | - power_code = @web_service_info['power_codes'][power].to_s | ||
| 61 | - sphere_code = @web_service_info['sphere_codes'][sphere].to_s | ||
| 62 | - additional_params = @web_service_info['additional_params'] | ||
| 63 | - | ||
| 64 | - return URI("#{base_url}?codigoPoder=#{power_code}&codigoEsfera=#{sphere_code}&#{additional_params}") | ||
| 65 | - end | ||
| 66 | - | ||
| 67 | - def self.get_json power, sphere | ||
| 68 | - uri = self.service_url power, sphere #URI(BASE_URL+"codigoPoder=#{power_code}&codigoEsfera=#{sphere_code}&retornarOrgaoEntidadeVinculados=NAO") | ||
| 69 | - data = Net::HTTP.get(uri) | ||
| 70 | - ActiveSupport::JSON.decode(data) | ||
| 71 | - end | ||
| 72 | - | ||
| 73 | - def self.retrieve_code unit, field | ||
| 74 | - data = unit[field] | ||
| 75 | - return data.split("/").last unless (data.blank? || data.nil?) | ||
| 76 | - return nil | ||
| 77 | - end | ||
| 78 | - | ||
| 79 | - def self.set_institution_data institution, unit | ||
| 80 | - | ||
| 81 | - community = if Community.where(:name=>unit["nome"]).length != 0 | ||
| 82 | - Community.where(:name=>unit["nome"]).first | ||
| 83 | - else | ||
| 84 | - Community.new :name=>unit["nome"] | ||
| 85 | - end | ||
| 86 | - | ||
| 87 | - institution.community = community | ||
| 88 | - institution.name = unit["nome"] | ||
| 89 | - institution.acronym = unit["sigla"] | ||
| 90 | - institution.unit_code = self.retrieve_code(unit,"codigoUnidade") | ||
| 91 | - institution.parent_code = self.retrieve_code(unit,"codigoUnidadePai") | ||
| 92 | - institution.unit_type = self.retrieve_code(unit,"codigoTipoUnidade") | ||
| 93 | - institution.juridical_nature = JuridicalNature.where(:name=>self.retrieve_code(unit,"codigoNaturezaJuridica").titleize).first | ||
| 94 | - institution.sub_juridical_nature = self.retrieve_code(unit,"codigoSubNaturezaJuridica") | ||
| 95 | - institution.normalization_level = unit["nivelNormatizacao"] | ||
| 96 | - institution.version = unit["versaoConsulta"] | ||
| 97 | - institution.governmental_power = GovernmentalPower.where(:name=>self.retrieve_code(unit,"codigoPoder").capitalize).first | ||
| 98 | - institution.governmental_sphere = GovernmentalSphere.where(:name=>self.retrieve_code(unit,"codigoEsfera").capitalize).first | ||
| 99 | - institution | ||
| 100 | - end | ||
| 101 | - | ||
| 102 | - def self.current_date | ||
| 103 | - date = Time.now.day.to_s + "/" + Time.now.month.to_s + "/" + Time.now.year.to_s | ||
| 104 | - date | ||
| 105 | - end | ||
| 106 | -end |
test/integration/institution_helper_test.rb
| @@ -1,25 +0,0 @@ | @@ -1,25 +0,0 @@ | ||
| 1 | -require File.dirname(__FILE__) + '/../../../../test/test_helper' | ||
| 2 | - | ||
| 3 | -class InstitutionHelperTest < ActionController::IntegrationTest | ||
| 4 | - | ||
| 5 | - def setup | ||
| 6 | - admin = create_user("test_admin").person | ||
| 7 | - admin.stubs(:has_permission?).returns("true") | ||
| 8 | - | ||
| 9 | - @environment = Environment.default | ||
| 10 | - @environment.enabled_plugins = ['MpogSoftwarePlugin'] | ||
| 11 | - @environment.add_admin(admin) | ||
| 12 | - @environment.name = "test_environment" | ||
| 13 | - @environment.save | ||
| 14 | - end | ||
| 15 | - | ||
| 16 | - should "not proceed with SIORG script if environment name isn't the informed" do | ||
| 17 | - assert !InstitutionHelper.mass_update, "Should not find the environment." | ||
| 18 | - end | ||
| 19 | - should "not proceed with SIORG script if admin name isn't the informed" do | ||
| 20 | - @environment.name = "Noosfero" | ||
| 21 | - @environment.save | ||
| 22 | - | ||
| 23 | - assert !InstitutionHelper.mass_update, "Could not find the admin." | ||
| 24 | - end | ||
| 25 | -end |