From 3b1bbdb566cd9b4e8b8c8847bb849e7de67d3238 Mon Sep 17 00:00:00 2001 From: dgonzdev Date: Thu, 13 Nov 2025 19:58:08 -0500 Subject: [PATCH 1/5] create bicycle counters --- .../20251114004802_create_bicycle_counters.rb | 17 +++++++++++ db/schema.rb | 30 +++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100755 db/migrate/20251114004802_create_bicycle_counters.rb create mode 100644 db/schema.rb 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/schema.rb b/db/schema.rb new file mode 100644 index 0000000..de0c750 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,30 @@ +# 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_004802) 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 + end +end From 26d7bc99eb7855dd76dfd534f3fd3b39dec0aae9 Mon Sep 17 00:00:00 2001 From: dgonzdev Date: Thu, 13 Nov 2025 20:02:15 -0500 Subject: [PATCH 2/5] add model --- app/models/department_of_transportation/bicycle_counter.rb | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100755 app/models/department_of_transportation/bicycle_counter.rb 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..ab3c0c2 --- /dev/null +++ b/app/models/department_of_transportation/bicycle_counter.rb @@ -0,0 +1,7 @@ +module DepartmentOfTransportation + class BicycleCounter < ApplicationRecord + self.table_name = :bicycle_counters + end +end + + From 8b4c977407ab7e857913bf5f6b4bc63e045fbbe7 Mon Sep 17 00:00:00 2001 From: dgonzdev Date: Thu, 13 Nov 2025 20:39:56 -0500 Subject: [PATCH 3/5] add metadata and method for downloading from csv --- .../bicycle_counter.rb | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/app/models/department_of_transportation/bicycle_counter.rb b/app/models/department_of_transportation/bicycle_counter.rb index ab3c0c2..2009bcf 100755 --- a/app/models/department_of_transportation/bicycle_counter.rb +++ b/app/models/department_of_transportation/bicycle_counter.rb @@ -1,6 +1,84 @@ +require 'open-uri' +require 'csv' + module DepartmentOfTransportation class BicycleCounter < ApplicationRecord self.table_name = :bicycle_counters + + CSV_ENDPOINT = "https://data.cityofnewyork.us/resource/smn3-rzf9.csv" + + def self.download + URI.open("https://data.cityofnewyork.us/resource/smn3-rzf9.csv") 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 From 6cd60ae74c30a22c94af31e5abc0eff1ef2b8a1e Mon Sep 17 00:00:00 2001 From: dgonzdev Date: Thu, 13 Nov 2025 20:44:14 -0500 Subject: [PATCH 4/5] refactor naming for download method --- app/models/department_of_transportation/bicycle_counter.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/models/department_of_transportation/bicycle_counter.rb b/app/models/department_of_transportation/bicycle_counter.rb index 2009bcf..4d3d73e 100755 --- a/app/models/department_of_transportation/bicycle_counter.rb +++ b/app/models/department_of_transportation/bicycle_counter.rb @@ -5,10 +5,10 @@ module DepartmentOfTransportation class BicycleCounter < ApplicationRecord self.table_name = :bicycle_counters - CSV_ENDPOINT = "https://data.cityofnewyork.us/resource/smn3-rzf9.csv" + CSV_SODA2_API_ENDPOINT = "https://data.cityofnewyork.us/resource/smn3-rzf9.csv" - def self.download - URI.open("https://data.cityofnewyork.us/resource/smn3-rzf9.csv") do |f| + 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 From 10dfccafc485df0295c8fa66a126eeb08353ef96 Mon Sep 17 00:00:00 2001 From: dgonzdev Date: Thu, 13 Nov 2025 20:46:02 -0500 Subject: [PATCH 5/5] add index --- db/migrate/20251114014438_add_index_to_original_id.rb | 5 +++++ db/schema.rb | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100755 db/migrate/20251114014438_add_index_to_original_id.rb 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 index de0c750..c619e6e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.2].define(version: 2025_11_14_004802) do +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" @@ -26,5 +26,6 @@ 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