Permalink
Browse files

try static methods and migrate to sinatra server

  • Loading branch information...
1 parent 7cbdfb4 commit af7c48811d8addd4cc59e0d4906e365d9dd5306d @alx committed Jul 13, 2011
Oops, something went wrong.
@@ -66,10 +66,16 @@ def seat_data
output = "var test = {data: ["
$SEATS.each do |seat|
if seat[:count] && seat[:count] > 0
- output << "{count: #{seat[:count]}, x: #{seat[:x]}, y: #{seat[:y]}},"
max = seat[:count] if max < seat[:count]
end
end
+ $SEATS.each do |seat|
+ if seat[:count] && seat[:count] > 0
+ output << "{count: #{max - seat[:count]}, x: #{seat[:x]}, y: #{seat[:y]}},"
+ else
+ output << "{count: #{max}, x: #{seat[:x]}, y: #{seat[:y]}},"
+ end
+ end
output << "], max: #{max}};"
File.open('total.json', 'w') do |file|
file.write output
Oops, something went wrong.
@@ -0,0 +1,6 @@
+*.db
+*.sqlite3
+.bundle/*
+log/*
+*.swp
+*.swo
View
@@ -0,0 +1 @@
+rvm use ree@politimap --create
View
@@ -0,0 +1,15 @@
+source :rubygems
+gem 'sinatra', '>= 1.0'
+gem 'rake'
+gem 'data_mapper'
+gem 'dm-core'
+gem 'dm-sqlite-adapter'
+gem 'dm-timestamps'
+gem 'dm-validations'
+gem 'dm-aggregates'
+gem 'dm-migrations'
+
+group :test do
+ gem 'rspec', :require => 'spec'
+ gem 'rack-test'
+end
@@ -0,0 +1,88 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ addressable (2.2.2)
+ data_mapper (1.0.2)
+ dm-aggregates (= 1.0.2)
+ dm-constraints (= 1.0.2)
+ dm-core (= 1.0.2)
+ dm-migrations (= 1.0.2)
+ dm-serializer (= 1.0.2)
+ dm-timestamps (= 1.0.2)
+ dm-transactions (= 1.0.2)
+ dm-types (= 1.0.2)
+ dm-validations (= 1.0.2)
+ data_objects (0.10.2)
+ addressable (~> 2.1)
+ diff-lcs (1.1.2)
+ dm-aggregates (1.0.2)
+ dm-core (~> 1.0.2)
+ dm-constraints (1.0.2)
+ dm-core (~> 1.0.2)
+ dm-migrations (~> 1.0.2)
+ dm-core (1.0.2)
+ addressable (~> 2.2)
+ extlib (~> 0.9.15)
+ dm-do-adapter (1.0.2)
+ data_objects (~> 0.10.2)
+ dm-core (~> 1.0.2)
+ dm-migrations (1.0.2)
+ dm-core (~> 1.0.2)
+ dm-serializer (1.0.2)
+ dm-core (~> 1.0.2)
+ fastercsv (~> 1.5.3)
+ json_pure (~> 1.4)
+ dm-sqlite-adapter (1.0.2)
+ dm-do-adapter (~> 1.0.2)
+ do_sqlite3 (~> 0.10.2)
+ dm-timestamps (1.0.2)
+ dm-core (~> 1.0.2)
+ dm-transactions (1.0.2)
+ dm-core (~> 1.0.2)
+ dm-types (1.0.2)
+ dm-core (~> 1.0.2)
+ fastercsv (~> 1.5.3)
+ json_pure (~> 1.4)
+ stringex (~> 1.1.0)
+ uuidtools (~> 2.1.1)
+ dm-validations (1.0.2)
+ dm-core (~> 1.0.2)
+ do_sqlite3 (0.10.2)
+ data_objects (= 0.10.2)
+ extlib (0.9.15)
+ fastercsv (1.5.3)
+ json_pure (1.4.6)
+ rack (1.2.1)
+ rack-test (0.5.6)
+ rack (>= 1.0)
+ rake (0.8.7)
+ rspec (2.1.0)
+ rspec-core (~> 2.1.0)
+ rspec-expectations (~> 2.1.0)
+ rspec-mocks (~> 2.1.0)
+ rspec-core (2.1.0)
+ rspec-expectations (2.1.0)
+ diff-lcs (~> 1.1.2)
+ rspec-mocks (2.1.0)
+ sinatra (1.1.0)
+ rack (~> 1.1)
+ tilt (~> 1.1)
+ stringex (1.1.0)
+ tilt (1.1)
+ uuidtools (2.1.1)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ data_mapper
+ dm-aggregates
+ dm-core
+ dm-migrations
+ dm-sqlite-adapter
+ dm-timestamps
+ dm-validations
+ rack-test
+ rake
+ rspec
+ sinatra (>= 1.0)
@@ -0,0 +1,20 @@
+Copyright (c) 2009 Nick Plante
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,35 @@
+= Sinatra Application Template
+
+A base Sinatra application template. Just fork and build. Yay!
+Includes Bundler, DataMapper, RSpec2, and Haml, all ready to go.
+
+Works with both Ruby 1.8.7 and Ruby 1.9.2.
+
+== Configuration
+
+Dependencies and all configuration is done in <tt>environment.rb</tt>. Your database is also set up here. DataMapper will use sqlite3 by default. Tests use the sqlite3-memory adapter (no configuration needed).
+
+Add your controller actions in <tt>application.rb</tt>. Views for these actions are placed in the <tt>views</tt> directory. Static files, including a stock stylesheet, go in the <tt>public</tt> directory. Models go in the <tt>lib</tt> directory and are auto-loaded.
+
+== Testing
+
+Add your specs in <tt>spec</tt>; just require <tt>spec_helper.rb</tt> to pre-configure the test environment. A number of samples are provided (including a sample model, which can be removed). To run the specs:
+
+ rake spec
+
+== Getting Started
+
+ bundle install
+ rake db:migrate
+ ruby application.rb
+
+== Thanks
+
+This project includes contributions from the following developers:
+
+ * garrensmith
+ * bryanwoods
+ * flexd
+ * mcollina
+
+(c) 2011 Nick Plante. This code is distributed under the MIT license.
View
@@ -0,0 +1,32 @@
+require 'rubygems'
+require 'bundler/setup'
+require 'rspec/core/rake_task'
+
+task :default => :test
+task :test => :spec
+
+if !defined?(RSpec)
+ puts "spec targets require RSpec"
+else
+ desc "Run all examples"
+ RSpec::Core::RakeTask.new(:spec) do |t|
+ t.pattern = 'spec/**/*.rb'
+ t.rspec_opts = ['-cfs']
+ end
+end
+
+namespace :db do
+ desc 'Auto-migrate the database (destroys data)'
+ task :migrate => :environment do
+ DataMapper.auto_migrate!
+ end
+
+ desc 'Auto-upgrade the database (preserves data)'
+ task :upgrade => :environment do
+ DataMapper.auto_upgrade!
+ end
+end
+
+task :environment do
+ require File.join(File.dirname(__FILE__), 'environment')
+end
@@ -0,0 +1,33 @@
+require 'rubygems'
+require 'bundler/setup'
+require 'sinatra'
+require File.join(File.dirname(__FILE__), 'environment')
+
+configure do
+ set :views, "#{File.dirname(__FILE__)}/views"
+end
+
+error do
+ e = request.env['sinatra.error']
+ Kernel.puts e.backtrace.join("\n")
+ 'Application error'
+end
+
+helpers do
+ # add your helpers here
+end
+
+# root page
+get '/' do
+ erb :root
+end
+
+get '/heatmap' do
+ content_type :json
+ month = 201001
+ 12.times do
+ parse_synthese "#{month}"
+ month += 1
+ end
+ $SEATS.to_json
+end
View
@@ -0,0 +1,11 @@
+require File.join(File.dirname(__FILE__), 'application')
+
+set :run, false
+set :environment, :production
+
+FileUtils.mkdir_p 'log' unless File.exists?('log')
+log = File.new("log/sinatra.log", "a+")
+$stdout.reopen(log)
+$stderr.reopen(log)
+
+run Sinatra::Application
@@ -0,0 +1,25 @@
+require 'rubygems'
+require 'bundler/setup'
+require 'dm-core'
+require 'dm-timestamps'
+require 'dm-validations'
+require 'dm-aggregates'
+require 'dm-migrations'
+require 'erb'
+require 'ostruct'
+
+require 'sinatra' unless defined?(Sinatra)
+
+configure do
+ SiteConfig = OpenStruct.new(
+ :title => 'Your Application Name',
+ :author => 'Your Name',
+ :url_base => 'http://localhost:4567/'
+ )
+
+ # load models
+ $LOAD_PATH.unshift("#{File.dirname(__FILE__)}/lib")
+ Dir.glob("#{File.dirname(__FILE__)}/lib/*.rb") { |lib| require File.basename(lib, '.*') }
+
+ DataMapper.setup(:default, (ENV["DATABASE_URL"] || "sqlite3:///#{File.expand_path(File.dirname(__FILE__))}/#{Sinatra::Base.environment}.db"))
+end
@@ -0,0 +1,81 @@
+require 'rubygems'
+require 'net/http'
+require 'json'
+require 'pp'
+
+
+def create_name_table
+ depute_seats = Array.new(650)
+
+ depute_list = api_json("http://www.nosdeputes.fr/deputes/json")
+ depute_list["deputes"].each do |depute_json|
+
+ depute = depute_json["depute"]
+ depute_api = api_json(depute["api_url"])
+
+ name = depute_api["depute"]["nom"]
+ seat = depute_api["depute"]["place_en_hemicycle"].to_i
+ pp "name: #{name} - seat: #{seat}"
+
+ if seat > 0
+ depute_seats[seat] = name
+ end
+
+ end
+ File.open('names.rb', 'w') do |file|
+ file.write "$NAMES=[\n"
+ depute_seats.each do |name|
+ file.write "\"#{name}\",\n"
+ end
+ file.write "]"
+ end
+end
+
+def api_json(json_url)
+ url = URI.parse(json_url)
+ req = Net::HTTP::Get.new(url.path)
+ res = Net::HTTP.start(url.host, url.port) {|http|
+ http.request(req)
+ }
+ return JSON.parse(res.body)
+end
+
+def parse_synthese(date)
+ json = api_json("http://www.nosdeputes.fr/synthese/#{date}/json")
+ json["deputes"].each do |depute_json|
+ depute = depute_json["depute"]
+ nb_interventions = depute["hemicycle_interventions_courtes"]
+ seat_index = $NAMES.index(depute["nom"])
+ if seat_index.to_i > 0 && nb_interventions > 0
+ if seat = $SEATS.select{|seat| seat[:id] == seat_index}.first
+ if seat[:count]
+ seat[:count] += nb_interventions
+ else
+ seat[:count] = nb_interventions
+ end
+ end
+ end
+ end
+end
+
+def seat_data
+ max = 0
+ output = "var test = {data: ["
+ $SEATS.each do |seat|
+ if seat[:count] && seat[:count] > 0
+ max = seat[:count] if max < seat[:count]
+ end
+ end
+ $SEATS.each do |seat|
+ if seat[:count] && seat[:count] > 0
+ output << "{count: #{max - seat[:count]}, x: #{seat[:x]}, y: #{seat[:y]}},"
+ else
+ output << "{count: #{max}, x: #{seat[:x]}, y: #{seat[:y]}},"
+ end
+ end
+ output << "], max: #{max}};"
+ File.open('total.json', 'w') do |file|
+ file.write output
+ end
+end
+
Oops, something went wrong.

0 comments on commit af7c488

Please sign in to comment.