diff --git a/app/models/department_of_transportation/bicycle_counter.rb b/app/models/department_of_transportation/bicycle_counter.rb new file mode 100755 index 0000000..4d3d73e --- /dev/null +++ b/app/models/department_of_transportation/bicycle_counter.rb @@ -0,0 +1,85 @@ +require 'open-uri' +require 'csv' + +module DepartmentOfTransportation + class BicycleCounter < ApplicationRecord + self.table_name = :bicycle_counters + + CSV_SODA2_API_ENDPOINT = "https://data.cityofnewyork.us/resource/smn3-rzf9.csv" + + def self.download_csv_with_soda2 + URI.open(CSV_SODA2_API_ENDPOINT) do |f| + f.each_line.each_with_index do |line, index| + next if index == 0 + + data = CSV.parse_line(line) + + original_id = data[0] + name = data[1] + domain = data[2] + latitude = data[3] + longitude = data[4] + interval = data[5] + timezone = data[6] + sens = data[7] + counter = data[8] + + next if BicycleCounter.find_by(original_id: original_id).present? + + BicycleCounter.create!( + original_id: original_id, + name: name, + domain: domain, + latitude: latitude, + longitude: longitude, + interval: interval, + timezone: timezone, + sens: sens, + counter: counter + ) + end + end + end + + def name + "Bicycle Counters" + end + + def description + "Bicycle counts conducted around New York City at key locations. For the counts data, please refer to the Bicycle Counts dataset. Bicycle Counts: https://data.cityofnewyork.us/Transportation/Bicycle-Counts/uczf-rk3c" + end + + def data_provided_by + "Department of Transportation (DOT)" + end + + # Dataset Information + def agency + "Department of Transportation (DOT)" + end + + # Update + def update_frequency + "Daily" + end + + def automation + "Yes" + end + + def date_made_public + "12/31/2019" + end + + # Topics + def category + "Transportation" + end + + def tags + "bicycle, count, eco-counter, bike" + end + end +end + + diff --git a/db/migrate/20251114004802_create_bicycle_counters.rb b/db/migrate/20251114004802_create_bicycle_counters.rb new file mode 100755 index 0000000..eb679d1 --- /dev/null +++ b/db/migrate/20251114004802_create_bicycle_counters.rb @@ -0,0 +1,17 @@ +class CreateBicycleCounters < ActiveRecord::Migration[7.2] + def change + create_table :bicycle_counters do |t| + t.integer :original_id + t.text :name + t.text :domain + t.decimal :latitude, precision: 15, scale: 10 + t.decimal :longitude, precision: 15, scale: 10 + t.integer :interval + t.text :timezone + t.text :sens + t.text :counter + + t.timestamps + end + end +end diff --git a/db/migrate/20251114014438_add_index_to_original_id.rb b/db/migrate/20251114014438_add_index_to_original_id.rb new file mode 100755 index 0000000..1b460b3 --- /dev/null +++ b/db/migrate/20251114014438_add_index_to_original_id.rb @@ -0,0 +1,5 @@ +class AddIndexToOriginalId < ActiveRecord::Migration[7.2] + def change + add_index :bicycle_counters, :original_id, unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..c619e6e --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,31 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# This file is the source Rails uses to define your schema when running `bin/rails +# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema[7.2].define(version: 2025_11_14_014438) do + # These are extensions that must be enabled in order to support this database + enable_extension "plpgsql" + + create_table "bicycle_counters", force: :cascade do |t| + t.integer "original_id" + t.text "name" + t.text "domain" + t.decimal "latitude", precision: 15, scale: 10 + t.decimal "longitude", precision: 15, scale: 10 + t.integer "interval" + t.text "timezone" + t.text "sens" + t.text "counter" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["original_id"], name: "index_bicycle_counters_on_original_id", unique: true + end +end