Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernando Blat committed Sep 3, 2015
0 parents commit 8732dfa
Show file tree
Hide file tree
Showing 19 changed files with 8,605 additions and 0 deletions.
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.2.2
14 changes: 14 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
source 'https://rubygems.org'

gem 'activesupport'
gem 'activerecord'
gem 'pg'

group :development, :test do
gem 'byebug'
gem 'spring'
end

group :test do
gem 'rspec-rails'
end
96 changes: 96 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
GEM
remote: https://rubygems.org/
specs:
actionpack (4.2.4)
actionview (= 4.2.4)
activesupport (= 4.2.4)
rack (~> 1.6)
rack-test (~> 0.6.2)
rails-dom-testing (~> 1.0, >= 1.0.5)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
actionview (4.2.4)
activesupport (= 4.2.4)
builder (~> 3.1)
erubis (~> 2.7.0)
rails-dom-testing (~> 1.0, >= 1.0.5)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
activemodel (4.2.4)
activesupport (= 4.2.4)
builder (~> 3.1)
activerecord (4.2.4)
activemodel (= 4.2.4)
activesupport (= 4.2.4)
arel (~> 6.0)
activesupport (4.2.4)
i18n (~> 0.7)
json (~> 1.7, >= 1.7.7)
minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
arel (6.0.3)
builder (3.2.2)
byebug (6.0.2)
diff-lcs (1.2.5)
erubis (2.7.0)
i18n (0.7.0)
json (1.8.3)
loofah (2.0.3)
nokogiri (>= 1.5.9)
mini_portile (0.6.2)
minitest (5.8.0)
nokogiri (1.6.6.2)
mini_portile (~> 0.6.0)
pg (0.18.2)
rack (1.6.4)
rack-test (0.6.3)
rack (>= 1.0)
rails-deprecated_sanitizer (1.0.3)
activesupport (>= 4.2.0.alpha)
rails-dom-testing (1.0.7)
activesupport (>= 4.2.0.beta, < 5.0)
nokogiri (~> 1.6.0)
rails-deprecated_sanitizer (>= 1.0.1)
rails-html-sanitizer (1.0.2)
loofah (~> 2.0)
railties (4.2.4)
actionpack (= 4.2.4)
activesupport (= 4.2.4)
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rake (10.4.2)
rspec-core (3.3.2)
rspec-support (~> 3.3.0)
rspec-expectations (3.3.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.3.0)
rspec-mocks (3.3.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.3.0)
rspec-rails (3.3.3)
actionpack (>= 3.0, < 4.3)
activesupport (>= 3.0, < 4.3)
railties (>= 3.0, < 4.3)
rspec-core (~> 3.3.0)
rspec-expectations (~> 3.3.0)
rspec-mocks (~> 3.3.0)
rspec-support (~> 3.3.0)
rspec-support (3.3.0)
spring (1.3.6)
thor (0.19.1)
thread_safe (0.3.5)
tzinfo (1.2.2)
thread_safe (~> 0.1)

PLATFORMS
ruby

DEPENDENCIES
activerecord
activesupport
byebug
pg
rspec-rails
spring

BUNDLED WITH
1.10.6
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Gobify Data

El código en este repositorio es responsable de suministrar a las instancias de Gobify con los datos
necesarios para arrancar la aplicación.

## Datos manejados por Gobify data

### Presupuestos municipales

```ruby

PresupuestosMunicipales::DataDump.new(2014, INE::Place.find_by_name('Gijón')).dump

```
23 changes: 23 additions & 0 deletions app/environment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'csv'
require 'ostruct'
require 'fileutils'
require 'rubygems'
require 'bundler/setup'
Bundler.require

require 'active_support/all'
require 'active_record'

ROOT = File.join(File.dirname(__FILE__), '..')

module INE; end
module PresupuestosMunicipales; end

require_relative './ine/csv_record'
Dir.glob('app/ine/*.rb').each{ |f| require_relative '../'+f }

INE::AutonomousRegionsCollection.records
INE::ProvincesCollection.records
INE::PlacesCollection.records

Dir.glob('app/presupuestos_municipales/*.rb').each{ |f| require_relative '../'+f }
21 changes: 21 additions & 0 deletions app/ine/autonomous_region.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class INE::AutonomousRegion < OpenStruct
include INE::CSVRecord

def provinces
INE::Province.find_all_by_autonomous_region_id(self.id)
end

private

def self.collection_klass
INE::AutonomousRegionsCollection
end

def self.filter(records, name)
return [] if name.blank?
parameterized_name = name.parameterize

records.select{|a| a.slug.include?(parameterized_name) }.
sort_by{|a| a.name.parameterize }
end
end
14 changes: 14 additions & 0 deletions app/ine/autonomous_regions_collection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class INE::AutonomousRegionsCollection
include Singleton

def self.records
@records ||= begin
CSV.read("data/ine/ccaa.csv").map do |raw_data|
INE::AutonomousRegion.new({
id: raw_data.first, name: raw_data.second, slug: raw_data.third,
lon: raw_data[3], lat: raw_data[4]
})
end.sort_by(&:slug)
end
end
end
26 changes: 26 additions & 0 deletions app/ine/csv_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module INE::CSVRecord
extend ActiveSupport::Concern

def to_param
self.slug
end

class_methods do

def all
collection_klass.records
end

def find_by_name(name)
collection_klass.records.detect{|obj| obj.name == name } if name.present?
end

def find_by_slug(slug)
collection_klass.records.detect{|obj| obj.slug == slug } if slug.present?
end

def find(id)
collection_klass.records.detect{|obj| obj.id == id } if id.present?
end
end
end
24 changes: 24 additions & 0 deletions app/ine/place.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class INE::Place < OpenStruct
include INE::CSVRecord

def self.find_all_by_province_id(province_id)
return [] if province_id.blank?

collection_klass.records.select{ |place| place.province_id == province_id }
end

private

def self.collection_klass
INE::PlacesCollection
end

def self.filter(records, name)
return [] if name.blank?
parameterized_name = name.parameterize

records.select{|p| p.slug.include?(parameterized_name) }.
reject{|p| ['ceuta','melilla'].include?(p.slug) }.
sort_by{|p| p.province.name.parameterize }
end
end
22 changes: 22 additions & 0 deletions app/ine/places_collection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class INE::PlacesCollection
include Singleton

def self.records
@records ||= begin
CSV.read("data/ine/municipios.csv", headers: true).map do |raw_data|
build_from_raw_data(raw_data)
end.sort_by(&:slug)
end
end

def self.build_from_raw_data(raw_data)
provinces = INE::ProvincesCollection.records

INE::Place.new({
id: raw_data[0], name: raw_data[2],
slug: raw_data[3], province_id: raw_data[1],
province: provinces.detect{|p| p.id == raw_data[1] },
lon: raw_data[6], lat: raw_data[7]
})
end
end
28 changes: 28 additions & 0 deletions app/ine/province.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class INE::Province < OpenStruct
include INE::CSVRecord

def self.find_all_by_autonomous_region_id(autonomous_region_id)
return [] if autonomous_region_id.blank?

collection_klass.records.select{ |province| province.autonomous_region_id == autonomous_region_id }
end

def places
INE::Place.find_all_by_province_id(self.id)
end

private

def self.collection_klass
INE::ProvincesCollection
end

def self.filter(records, name)
return [] if name.blank?
parameterized_name = name.parameterize

records.select{|p| p.slug.include?(parameterized_name) }.
reject{|p| ['ceuta','melilla'].include?(p.slug) }.
sort_by{|p| p.autonomous_region.name.parameterize }
end
end
22 changes: 22 additions & 0 deletions app/ine/provinces_collection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class INE::ProvincesCollection
include Singleton

def self.records
@records ||= begin
CSV.read("data/ine/provincias.csv").map do |raw_data|
build_from_raw_data(raw_data)
end.sort_by(&:slug)
end
end

def self.build_from_raw_data(raw_data)
autonomous_regions = INE::AutonomousRegionsCollection.records

INE::Province.new({
id: raw_data[0], name: raw_data[2],
slug: raw_data[3], autonomous_region_id: raw_data[1],
autonomous_region: autonomous_regions.detect{|a| a.id == raw_data[1] },
lon: raw_data[4], lat: raw_data[5]
})
end
end
75 changes: 75 additions & 0 deletions app/presupuestos_municipales/data_dump.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
class PresupuestosMunicipales::DataDump
DB_NAME = 'presupuestos_municipales'

def initialize(year, ine_place)
@year = year
@ine_place = ine_place
end

def dump
directory = "/tmp/#{DB_NAME}_#{@year}_#{@ine_place.id}/"
FileUtils.rm_r directory
FileUtils.mkdir directory

FileUtils.cp "./data/#{DB_NAME}/#{@year}/data_clean.sql", directory
FileUtils.cp "./data/#{DB_NAME}/#{@year}/tb_cuentasEconomica.sql", directory
FileUtils.cp "./data/#{DB_NAME}/#{@year}/tb_cuentasProgramas.sql", directory

ar = ActiveRecord::Base.establish_connection(
adapter: 'postgresql',
host: 'localhost',
username: 'fernando',
password: '',
database: database_name
)

# tb_inventario
ar.connection.execute %Q{COPY (SELECT * FROM tb_inventario WHERE codente = '#{codente}') TO '#{directory}tb_inventario.csv' DELIMITER ',' CSV HEADER;}

id = ar.connection.execute("SELECT * FROM tb_inventario WHERE codente = '#{codente}'").first['id']

# tb_funcional
ar.connection.execute %Q{COPY (SELECT * FROM tb_funcional WHERE id = '#{id}') TO '#{directory}tb_funcional.csv' DELIMITER ',' CSV HEADER;}

# tb_economica
ar.connection.execute %Q{COPY (SELECT * FROM tb_economica WHERE id = '#{id}') TO '#{directory}tb_economica.csv' DELIMITER ',' CSV HEADER;}

%W{ tb_inventario tb_funcional tb_economica }.each do |table_name|
%x{pg_dump #{database_name} -t #{table_name} -s > #{directory}/#{table_name}.sql}
end

# load_data.sh
fd = File.open("#{directory}load_data.sh", 'w+')
fd.write(<<-FILE)
DB=presupuestos_municipales_2014_#{@ine_place.id}_development
dropdb $DB
createdb $DB
psql $DB < tb_cuentasEconomica.sql
psql $DB < tb_cuentasProgramas.sql
psql $DB < tb_economica.sql
psql $DB < tb_funcional.sql
psql $DB < tb_inventario.sql
psql $DB < data_clean.sql
psql -c "COPY tb_inventario FROM '#{directory}tb_inventario.csv' DELIMITER ',' CSV HEADER;" $DB
psql -c "COPY tb_funcional FROM '#{directory}tb_funcional.csv' DELIMITER ',' CSV HEADER;" $DB
psql -c "COPY tb_economica FROM '#{directory}tb_economica.csv' DELIMITER ',' CSV HEADER;" $DB
FILE
fd.close

FileUtils.chmod "u=wrx", "#{directory}load_data.sh"
puts "Database dumped to #{directory}..."
end

private

def codente
"#{@ine_place.id}AA000"
end

def database_name
"#{DB_NAME}_#{@year}"
end
end
Loading

0 comments on commit 8732dfa

Please sign in to comment.