camara 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +7 -0
  2. data/.coveralls.yml +1 -0
  3. data/.gitignore +22 -0
  4. data/.travis.yml +14 -0
  5. data/Gemfile +4 -0
  6. data/Guardfile +12 -0
  7. data/LICENSE.txt +22 -0
  8. data/README.md +37 -0
  9. data/Rakefile +24 -0
  10. data/camara.gemspec +34 -0
  11. data/lib/camara.rb +42 -0
  12. data/lib/camara/connection.rb +17 -0
  13. data/lib/camara/deputados/bancada.rb +4 -0
  14. data/lib/camara/deputados/bloco.rb +5 -0
  15. data/lib/camara/deputados/cargo_comissao.rb +4 -0
  16. data/lib/camara/deputados/client.rb +37 -0
  17. data/lib/camara/deputados/comissao.rb +4 -0
  18. data/lib/camara/deputados/deputado.rb +12 -0
  19. data/lib/camara/deputados/filiacao_partidaria.rb +5 -0
  20. data/lib/camara/deputados/gabinete.rb +4 -0
  21. data/lib/camara/deputados/partido.rb +5 -0
  22. data/lib/camara/deputados/periodo_exercicio.rb +5 -0
  23. data/lib/camara/initialize.rb +8 -0
  24. data/lib/camara/orgaos/client.rb +7 -0
  25. data/lib/camara/orgaos/orgao.rb +3 -0
  26. data/lib/camara/version.rb +3 -0
  27. data/lib/camara/xml_fields.rb +110 -0
  28. data/spec/camara/deputados/bancada_spec.rb +10 -0
  29. data/spec/camara/deputados/bloco_spec.rb +8 -0
  30. data/spec/camara/deputados/cargo_comissao_spec.rb +9 -0
  31. data/spec/camara/deputados/client_spec.rb +101 -0
  32. data/spec/camara/deputados/comissao_spec.rb +9 -0
  33. data/spec/camara/deputados/deputado_spec.rb +18 -0
  34. data/spec/camara/deputados/filiacao_partidaria_spec.rb +9 -0
  35. data/spec/camara/deputados/gabinete_spec.rb +8 -0
  36. data/spec/camara/deputados/partido_spec.rb +9 -0
  37. data/spec/camara/deputados/periodo_exercicio_spec.rb +9 -0
  38. data/spec/camara/orgaos/client_spec.rb +12 -0
  39. data/spec/camara/xml_fields_spec.rb +52 -0
  40. data/spec/cassettes/camara/deputados/obter_deputado_178387.yml +96 -0
  41. data/spec/cassettes/camara/deputados/obter_deputado_9999.yml +47 -0
  42. data/spec/cassettes/camara/deputados/obter_deputados.yml +45 -0
  43. data/spec/cassettes/camara/deputados/obter_lideres_bancadas.yml +45 -0
  44. data/spec/cassettes/camara/deputados/obter_partidos_bloco_cd.yml +687 -0
  45. data/spec/cassettes/camara/deputados/obter_partidos_cd.yml +547 -0
  46. data/spec/cassettes/camara/orgaos/listar_tipos_orgaos.yml +101 -0
  47. data/spec/spec_helper.rb +16 -0
  48. data/spec/support/xml_fields_matchers.rb +21 -0
  49. metadata +294 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3f0c57fd63eeade21ca60d933d2e86a769aa0232
4
+ data.tar.gz: 97792e1375bd4703ec855ba761c04ebeff63d291
5
+ SHA512:
6
+ metadata.gz: 5efc41b265c931748f7f2ee868dcb35787eebcdd67143f4b6410f6c6d556cfba7c8e48ba2d18c32456b78b99d77549a63d471f528f3171e713f8dfcff36c2e31
7
+ data.tar.gz: 670d64b7699927f343310879c75970ec2a8b09c107f645c8008f211ed1886175a3d99cd7cf8648c78f4318d95ab6b987ff7b8cfaf3f6e4023d5100200c5504cf
@@ -0,0 +1 @@
1
+ service_name: travis-ci
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
@@ -0,0 +1,14 @@
1
+ language: ruby
2
+ cache: bundler
3
+
4
+ rvm:
5
+ - 2.0.0
6
+
7
+ script: 'bundle exec rake'
8
+
9
+ notifications:
10
+ email:
11
+ recipients:
12
+ - dimascyriaco@gmail.com
13
+ on_failure: change
14
+ on_success: never
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in camara.gemspec
4
+ gemspec
@@ -0,0 +1,12 @@
1
+ guard :rspec, cmd: 'bundle exec rspec --color' do
2
+ # watch /lib/ files
3
+ watch(%r{^lib/(.+).rb$}) do |m|
4
+ "spec/#{m[1]}_spec.rb"
5
+ end
6
+
7
+ # watch /spec/ files
8
+ watch(%r{^spec/(.+).rb$}) do |m|
9
+ "spec/#{m[1]}.rb"
10
+ end
11
+ end
12
+
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Dimas Cyriaco
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,37 @@
1
+ [![Code Climate](https://codeclimate.com/github/dimascyriaco/camara/badges/gpa.svg)](https://codeclimate.com/github/dimascyriaco/camara)
2
+ [![Build Status](https://travis-ci.org/dimascyriaco/camara.svg?branch=master)](https://travis-ci.org/dimascyriaco/camara)
3
+ [![Coverage Status](https://coveralls.io/repos/dimascyriaco/camara/badge.png?branch=master)](https://coveralls.io/r/dimascyriaco/camara?branch=master)
4
+
5
+ # Camara
6
+
7
+ Camara é uma biblioteca para acesso aos webservices da Camera dos Deputados.
8
+
9
+ ## Instalação
10
+
11
+ Adicione esta linha aos seu Gemfile:
12
+
13
+ gem 'camara'
14
+
15
+ E execute:
16
+
17
+ $ bundle
18
+
19
+ Ou instale você mesmo:
20
+
21
+ $ gem install camara
22
+
23
+ ## Uso
24
+
25
+ ```ruby
26
+ deputados = Camara::Deputados.obter_deputados # => [<Deputado>, <Deputado>, ...]
27
+ deputado = Camara::Deputados.obter_detalhes_deputado(deputados.first.id) # => <Deputado>
28
+ deputado.nome # => "Fulano de Tal"
29
+ ```
30
+
31
+ ## Contribuindo
32
+
33
+ 1. Faça um fork do projeto
34
+ 2. Crie um branch para a sua feature (`git checkout -b minha-nova-feature`)
35
+ 3. Commit suas mudanças (`git commit -am 'Adicionada nova feature'`)
36
+ 4. Faça push para o branch (`git push origin minha-nova-feature`)
37
+ 5. Crie um Pull Request
@@ -0,0 +1,24 @@
1
+ require 'rspec/core/rake_task'
2
+ require 'bundler/gem_tasks'
3
+
4
+ RSpec::Core::RakeTask.new(:spec) do |task|
5
+ task.rspec_opts = ['--color']
6
+ end
7
+
8
+ task :default => :spec
9
+
10
+ task :console do
11
+ require 'pry'
12
+ require 'camara'
13
+
14
+ def reload!
15
+ files = $LOADED_FEATURES.select { |feat| feat =~ /\/camara\// }
16
+ files.each { |file| load file }
17
+ true
18
+ end
19
+
20
+ ARGV.clear
21
+ Pry.start
22
+ end
23
+
24
+ task c: :console
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'camara/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "camara"
8
+ spec.version = Camara::VERSION
9
+ spec.authors = ["Dimas Cyriaco"]
10
+ spec.email = ["dimascyriaco@gmail.com"]
11
+ spec.summary = %q{Camara é uma biblioteca para acesso aos webservices da Camera dos Deputados.}
12
+ spec.description = %q{Camara é uma biblioteca para acesso aos webservices da Camera dos Deputados.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "rspec-collection_matchers"
25
+ spec.add_development_dependency "rspec-expectations"
26
+ spec.add_development_dependency "guard-rspec"
27
+ spec.add_development_dependency "vcr"
28
+ spec.add_development_dependency "webmock"
29
+ spec.add_development_dependency "coveralls"
30
+ spec.add_development_dependency "pry"
31
+ spec.add_dependency 'activesupport'
32
+ spec.add_dependency 'faraday'
33
+ spec.add_dependency 'nokogiri'
34
+ end
@@ -0,0 +1,42 @@
1
+ require_relative "camara/version"
2
+ require_relative 'camara/initialize'
3
+ require "active_support/dependencies/autoload"
4
+ require 'faraday'
5
+ require 'nokogiri'
6
+
7
+ module Camara
8
+ extend ActiveSupport::Autoload
9
+
10
+ autoload :Connection
11
+ autoload :XmlFields
12
+
13
+ module Deputados
14
+ extend ActiveSupport::Autoload
15
+
16
+ autoload :Deputado
17
+ autoload :Partido
18
+ autoload :Gabinete
19
+ autoload :Comissao
20
+ autoload :CargoComissao
21
+ autoload :PeriodoExercicio
22
+ autoload :FiliacaoPartidaria
23
+ autoload :Bancada
24
+ autoload :Bloco
25
+ autoload :Client
26
+
27
+ extend Client
28
+ end
29
+
30
+ module Orgaos
31
+ extend ActiveSupport::Autoload
32
+
33
+ autoload :Orgao
34
+ autoload :Client
35
+
36
+ extend Client
37
+ end
38
+
39
+ extend Connection
40
+ end
41
+
42
+ Camara.init
@@ -0,0 +1,17 @@
1
+ module Camara::Connection
2
+ attr_reader :connection
3
+
4
+ def default_options
5
+ {
6
+ request: :url_encoded,
7
+ adapter: Faraday.default_adapter
8
+ }
9
+ end
10
+
11
+ def init
12
+ @connection = Faraday.new(url: 'http://www.camara.gov.br') do |faraday|
13
+ faraday.request default_options[:request]
14
+ faraday.adapter default_options[:adapter]
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,4 @@
1
+ class Camara::Deputados::Bancada
2
+ include Camara::XmlFields
3
+ fields :sigla, :nome, lider: Camara::Deputados::Deputado, vice_lideres: [Camara::Deputados::Deputado]
4
+ end
@@ -0,0 +1,5 @@
1
+ class Camara::Deputados::Bloco
2
+ include Camara::XmlFields
3
+ fields :idBloco, :nomeBloco, :siglaBloco, :dataCriacaoBloco, :dataExtincaoBloco,
4
+ partidos: [Camara::Deputados::Partido]
5
+ end
@@ -0,0 +1,4 @@
1
+ class Camara::Deputados::CargoComissao
2
+ include Camara::XmlFields
3
+ fields :idOrgaoLegislativoCD, :siglaComissao, :nomeComissao, :idCargo, :nomeCargo, :dataEntrada, :dataSaida
4
+ end
@@ -0,0 +1,37 @@
1
+ module Camara::Deputados::Client
2
+ def obter_deputados
3
+ response = Camara.connection.get "/SitCamaraWS/Deputados.asmx/ObterDeputados"
4
+ data = Nokogiri::XML clean_xml(response.body)
5
+ data.css('deputado').map { |deputado| Camara::Deputados::Deputado.new(deputado) }
6
+ end
7
+
8
+ def obter_detalhes_deputado(id)
9
+ response = Camara.connection.get "/SitCamaraWS/Deputados.asmx/ObterDetalhesDeputado?ideCadastro=#{id}&numLegislatura="
10
+
11
+ data = Nokogiri::XML(clean_xml(response.body)).at_css('Deputado')
12
+ Camara::Deputados::Deputado.new data if data
13
+ end
14
+
15
+ def obter_partidos_cd
16
+ response = Camara.connection.get "/SitCamaraWS/Deputados.asmx/ObterPartidosCD"
17
+ data = Nokogiri::XML clean_xml(response.body)
18
+ data.css('partido').map { |deputado| Camara::Deputados::Partido.new(deputado) }
19
+ end
20
+
21
+ def obter_lideres_bancadas
22
+ response = Camara.connection.get "/SitCamaraWS/Deputados.asmx/ObterLideresBancadas"
23
+ data = Nokogiri::XML clean_xml(response.body)
24
+ data.css('bancada').map { |bancada| Camara::Deputados::Bancada.new(bancada) }
25
+ end
26
+
27
+ def obter_partidos_bloco_cd(id=nil, legislatura=nil)
28
+ response = Camara.connection.get "/SitCamaraWS/Deputados.asmx/ObterPartidosBlocoCD?numLegislatura=#{legislatura}&idBloco=#{id}"
29
+ data = Nokogiri::XML clean_xml(response.body)
30
+ data.css('bloco').map { |bloco| Camara::Deputados::Bloco.new(bloco) }
31
+ end
32
+
33
+ private
34
+ def clean_xml(xml)
35
+ xml.gsub(/\r\n\s*/, '')
36
+ end
37
+ end
@@ -0,0 +1,4 @@
1
+ class Camara::Deputados::Comissao
2
+ include Camara::XmlFields
3
+ fields :idOrgaoLegislativoCD, :siglaComissao, :nomeComissao, :condicaoMembro, :dataEntrada, :dataSaida
4
+ end
@@ -0,0 +1,12 @@
1
+ class Camara::Deputados::Deputado
2
+ include Camara::XmlFields
3
+
4
+ fields :email, :nomeProfissao, :dataNascimento, :dataFalecimento, :ufRepresentacaoAtual, :situacaoNaLegislaturaAtual,
5
+ :ideCadastro, :idParlamentarDeprecated, :nomeParlamentarAtual, :nomeCivil, :sexo,
6
+ :historicoNomeParlamentar, :historicoLider, :condicao, :matricula, :idParlamentar,
7
+ :nome, :nomeParlamentar, :urlFoto, :uf, :partido, :anexo, :fone, :numLegislatura,
8
+ partidoAtual: Camara::Deputados::Partido, gabinete: Camara::Deputados::Gabinete,
9
+ comissoes: [Camara::Deputados::Comissao], periodosExercicio: [Camara::Deputados::PeriodoExercicio],
10
+ cargosComissoes: [Camara::Deputados::CargoComissao],
11
+ filiacoesPartidarias: [Camara::Deputados::FiliacaoPartidaria]
12
+ end
@@ -0,0 +1,5 @@
1
+ class Camara::Deputados::FiliacaoPartidaria
2
+ include Camara::XmlFields
3
+ fields :idPartidoAnterior, :siglaPartidoAnterior, :nomePartidoAnterior, :idPartidoPosterior, :siglaPartidoPosterior,
4
+ :nomePartidoPosterior, :dataFiliacaoPartidoPosterior
5
+ end
@@ -0,0 +1,4 @@
1
+ class Camara::Deputados::Gabinete
2
+ include Camara::XmlFields
3
+ fields :numero, :anexo, :telefone
4
+ end
@@ -0,0 +1,5 @@
1
+ class Camara::Deputados::Partido
2
+ include Camara::XmlFields
3
+ fields :idPartido, :sigla, :nome, :siglaPartido, :nomePartido, :dataCriacao, :dataExtincao, :data_adesao_partido,
4
+ :data_desligamento_partido
5
+ end
@@ -0,0 +1,5 @@
1
+ class Camara::Deputados::PeriodoExercicio
2
+ include Camara::XmlFields
3
+ fields :siglaUFRepresentacao, :situacaoExercicio, :dataInicio, :dataFim, :idCausaFimExercicio,
4
+ :descricaoCausaFimExercicio, :idCadastroParlamentarAnterior
5
+ end
@@ -0,0 +1,8 @@
1
+ require 'active_support/inflector'
2
+
3
+ ActiveSupport::Inflector.inflections(:en) do |inflect|
4
+ inflect.singular 'comissoes', 'comissao'
5
+ inflect.singular 'periodosExercicio', 'periodoExercicio'
6
+ inflect.singular 'cargosComissoes', 'cargoComissoes'
7
+ inflect.singular 'lideres', 'lider'
8
+ end
@@ -0,0 +1,7 @@
1
+ module Camara::Orgaos::Client
2
+ def listar_tipos_orgaos
3
+ response = Camara.connection.get '/SitCamaraWS/Orgaos.asmx/ListarTiposOrgaos'
4
+ data = Nokogiri::XML response.body
5
+ response.body
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ class Camara::Orgaos::Orgao
2
+
3
+ end
@@ -0,0 +1,3 @@
1
+ module Camara
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,110 @@
1
+ require 'active_support/core_ext/string/inflections'
2
+ require 'active_support/core_ext/object/blank'
3
+ require 'active_support/concern'
4
+
5
+ # O 'concern' XmlFields provê a possibiliade de se listar campos que serão extraidos
6
+ # do XML passado na inicialização e disponibilizados como attr_readers pela classe.
7
+ #
8
+ # @exemple
9
+ # class Person
10
+ # include Camara::XmlFields
11
+ # fields :name, :phone
12
+ # end
13
+ #
14
+ # data = Nokogiri::XML "<person><name>Dimas Cyriaco</name><phone>9999-9999</phone></person>"
15
+ #
16
+ # person = Person.new(data)
17
+ # person.name # => 'Dimas Cyriaco'
18
+ # person.phone # => '9999-9999'
19
+ #
20
+ # XmlFields também aceita classes aninhadas.
21
+ #
22
+ # @exemple
23
+ # class Address
24
+ # include Camara::XmlFields
25
+ # fields :street, :number
26
+ # end
27
+ #
28
+ # class Person
29
+ # include Camara::XmlFields
30
+ # fields :name, address: Address
31
+ # end
32
+ #
33
+ # data = Nokogiri::XML "<person><name>Dimas Cyriaco</name><address><street>Blah</street><number>999</number></address></person>"
34
+ #
35
+ # person = Person.new(data)
36
+ # person.name # => 'Dimas Cyriaco'
37
+ # person.phone # => '9999-9999'
38
+ # person.address # => <#Address>
39
+ # person.address.street # => 'Blah'
40
+ # person.address.number # => '999'
41
+ #
42
+ # Também é possivel configurar campos que contenham um array.
43
+ #
44
+ # @example
45
+ # class Hobby
46
+ # include Camara::XmlFields
47
+ # fields :name
48
+ # end
49
+ #
50
+ # class Person
51
+ # include Camara::XmlFields
52
+ # fields :name, hobbies: [Hobby]
53
+ # end
54
+ #
55
+ # data = Nokogiri::XML "<person><name>Dimas Cyriaco</name><hobbies><hobby><name>Football</name></hobby></hobbies></person>"
56
+ #
57
+ # person = Person.new(data)
58
+ # person.hobbies # => [<#Hobby name="Football">]
59
+ #
60
+ module Camara::XmlFields
61
+ extend ActiveSupport::Concern
62
+
63
+ included do
64
+ def fields
65
+ self.class.xml_fields
66
+ end
67
+
68
+ # @param [Nokogiri::XML::Element] data
69
+ def initialize(data)
70
+ Array(fields).each do |field_name|
71
+ case field_name
72
+ when String, Symbol
73
+ value = data.css("/#{field_name}").text.presence || data.css("@#{field_name}").text.presence rescue nil
74
+ instance_variable_set "@#{field_name.to_s.underscore}", value
75
+ when Hash
76
+ field_name.keys.each do |key|
77
+ if field_name[key].is_a? Array
78
+ objects = data.css("#{key.to_s.singularize}").map do |data|
79
+ field_name[key].first.new(data)
80
+ end
81
+ instance_variable_set "@#{key.to_s.underscore}", objects
82
+ else
83
+ instance_variable_set "@#{key.to_s.underscore}", field_name[key].new(data.css("/#{key}"))
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
90
+
91
+ module ClassMethods
92
+ attr_reader :xml_fields
93
+
94
+ # @param Array<String, Symbol, Hash> args
95
+ def fields(*args)
96
+ @xml_fields = args
97
+
98
+ @xml_fields.each do |field_name|
99
+ case field_name
100
+ when String, Symbol
101
+ attr_reader field_name.to_s.underscore
102
+ when Hash
103
+ field_name.keys.each do |key|
104
+ attr_reader key.to_s.underscore
105
+ end
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end