Skip to content

Commit

Permalink
Merge branch 'main' into 22-aws-ebs
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeheft committed Jul 7, 2024
2 parents b8e36a3 + 5f25704 commit 1a39ffb
Show file tree
Hide file tree
Showing 39 changed files with 1,658 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rspec_push.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: "3.3.0"
ruby-version: "3.2.0"

- name: Install dependencies
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/rubocop.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 3.3.0
ruby-version: 3.2.0

- name: Install dependencies
run: |
Expand Down
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,7 @@ dump.rdb
# Ignore application configuration
/config/application.yml

/terraform/.terraform/*
# Elastic Beanstalk Files
.elasticbeanstalk/*
!.elasticbeanstalk/*.cfg.yml
!.elasticbeanstalk/*.global.yml
8 changes: 7 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ require:
- ./lib/rubocop/init_autoloader

AllCops:
TargetRubyVersion: 3.3.0
TargetRubyVersion: 3.2.0
Exclude:
- "db/**/*"
- "config/**/*"
Expand All @@ -22,10 +22,16 @@ Layout/ArgumentAlignment:
Layout/IndentationWidth:
Enabled: true
Width: 2
Layout/DotPosition:
EnforcedStyle: leading
Layout/EndAlignment:
EnforcedStyleAlignWith: variable
Layout/MultilineAssignmentLayout:
EnforcedStyle: same_line
Layout/MultilineMethodCallIndentation:
EnforcedStyle: indented
Style/MethodCallWithArgsParentheses:
EnforcedStyle: require_parentheses
Layout/SpaceInsideBlockBraces:
EnforcedStyle: space
Lint/UnusedMethodArgument:
Expand Down
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ruby-3.3.0
3.2.0
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ruby 3.2.0
20 changes: 10 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
# syntax = docker/dockerfile:1

# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
ARG RUBY_VERSION=3.3.0
ARG RUBY_VERSION=3.2.0
FROM registry.docker.com/library/ruby:$RUBY_VERSION-slim as base

# Rails app lives here
WORKDIR /rails

# Set production environment
ENV RAILS_ENV="production" \
BUNDLE_DEPLOYMENT="1" \
BUNDLE_PATH="/usr/local/bundle" \
BUNDLE_WITHOUT="development"
BUNDLE_DEPLOYMENT="1" \
BUNDLE_PATH="/usr/local/bundle" \
BUNDLE_WITHOUT="development"


# Throw-away build stage to reduce size of final image
FROM base as build

# Install packages needed to build gems
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y build-essential git libpq-dev libvips pkg-config
apt-get install --no-install-recommends -y build-essential git libpq-dev libvips pkg-config

# Install application gems
COPY Gemfile Gemfile.lock ./
RUN bundle install && \
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
bundle exec bootsnap precompile --gemfile
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
bundle exec bootsnap precompile --gemfile

# Copy application code
COPY . .
Expand All @@ -39,16 +39,16 @@ FROM base

# Install packages needed for deployment
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y curl libvips postgresql-client && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives
apt-get install --no-install-recommends -y curl libvips postgresql-client && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives

# Copy built artifacts: gems, application
COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build /rails /rails

# Run and own only the runtime files as a non-root user for security
RUN useradd rails --create-home --shell /bin/bash && \
chown -R rails:rails db log storage tmp
chown -R rails:rails db log storage tmp
USER rails:rails

# Entrypoint prepares the database.
Expand Down
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# frozen_string_literal: true

source "https://rubygems.org"
ruby "3.3.0"
ruby "3.2.0"
gem "bootsnap", require: false
gem "database_cleaner"
gem "faker"
gem "faraday"
gem "figaro"
gem "geocoder"
gem "jsonapi-serializer"
gem "money-rails"
gem "pg", "~> 1.1"
gem "puma", ">= 5.0"
Expand Down
6 changes: 5 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ GEM
rdoc (>= 4.0.0)
reline (>= 0.4.2)
json (2.7.2)
jsonapi-serializer (2.2.0)
activesupport (>= 4.2)
language_server-protocol (3.17.0.3)
loofah (2.22.0)
crass (~> 1.0.2)
Expand Down Expand Up @@ -323,6 +325,7 @@ PLATFORMS
aarch64-linux
arm-linux
arm64-darwin-22
arm64-darwin-23
x86-linux
x86_64-darwin
x86_64-linux
Expand All @@ -337,6 +340,7 @@ DEPENDENCIES
faraday
figaro
geocoder
jsonapi-serializer
money-rails
pg (~> 1.1)
pry-rails
Expand All @@ -354,7 +358,7 @@ DEPENDENCIES
webmock

RUBY VERSION
ruby 3.3.0p0
ruby 3.2.0p0

BUNDLED WITH
2.5.13
10 changes: 10 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
# frozen_string_literal: true

class ApplicationController < ActionController::API
private def pagination_params
params.permit(:limit, :offset)
end
private def limit
pagination_params[:limit] || 2
end

private def offset
pagination_params[:offset] || 0
end
end
19 changes: 19 additions & 0 deletions app/controllers/drivers/selectable_rides_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module Drivers
class SelectableRidesController < ApplicationController
def index
rides = Rides::Commands::RankRides.call(driver:)[offset, limit]
opts = { include: %i[from_address to_address] }
render json: RidePojoSerializer.new(rides, opts)
end

private def driver
@driver ||= Driver.find(ride_params[:driver_id])
end

private def ride_params
params.permit(:driver_id, :limit, :offset)
end
end
end
12 changes: 12 additions & 0 deletions app/controllers/drivers_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class DriversController < ApplicationController
def index
drivers = Driver.limit(limit).offset(offset)
render json: DriverSerializer.new(drivers, is_collection: true)
end

private def pagination_params
params.permit(:limit, :offset)
end
end
4 changes: 4 additions & 0 deletions app/models/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ class Driver < ApplicationRecord

validates :first_name, :last_name, presence: true

def full_name
[first_name, last_name].join(" ")
end

def origin_place_id
current_address.place_id
end
Expand Down
16 changes: 14 additions & 2 deletions app/models/ride.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,26 @@ class Ride < ApplicationRecord
}

scope :by_address, ->(address_id) {
where(from_address_id: address_id).or(where(to_address_id: address_id))
}
where(from_address_id: address_id).or(where(to_address_id: address_id))
}

scope :selectable, -> {
includes(:from_address, :to_address)
.select(:id, :from_address_id, :to_address_id)
.where(driver_id: nil, duration: nil, distance: nil, commute_duration: nil, amount_cents: 0)
}

scope :nearby_driver, ->(driver) {
current_driver_address = driver.current_address
# Due to the manner in which Geocoder computes and orders this, we need to first
# get the addresses within the driver's desired radius and _then_ find the rides
# with corresponding from_address_id
addresses = Address.where.not(id: current_driver_address.id)
.near([current_driver_address.latitude, current_driver_address.longitude], driver.max_radius)

selectable.where(from_address_id: addresses.map(&:id))
}

def origin_place_id
from_address.place_id
end
Expand Down
7 changes: 7 additions & 0 deletions app/serializers/address_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddressSerializer
include JSONAPI::Serializer
set_type :address
attributes :id, :line_1, :line_2, :city, :state, :zip_code, :full_address
end
8 changes: 8 additions & 0 deletions app/serializers/driver_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

class DriverSerializer
include JSONAPI::Serializer
set_type :driver
attributes :id, :full_name
has_one :current_address, serializer: AddressSerializer
end
32 changes: 32 additions & 0 deletions app/serializers/ride_pojo_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

class RidePojoSerializer
include JSONAPI::Serializer
extend ActionView::Helpers::DateHelper
extend ActionView::Helpers::NumberHelper

set_id do |struct, _params|
struct.ride_id
end

set_type "pre-ride"

attribute :distance do |struct, _params|
number_to_human((struct.distance_meters / 1609.34).round(2), units: :distance)
end

attribute :duration do |struct, _params|
distance_of_time_in_words(struct.duration.to_f)
end

attribute :commute_duration do |struct, _params|
distance_of_time_in_words(struct.commute_duration.to_f)
end

attribute :ride_earnings do |struct, _params|
struct.ride_amount.format
end

belongs_to :from_address, serializer: AddressSerializer
belongs_to :to_address, serializer: AddressSerializer
end
6 changes: 5 additions & 1 deletion config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
# since you don't have to restart the web server when you make code changes.
config.enable_reloading = true

# preserve response format with errors
config.debug_exception_response_format = :api


# Do not eager load code on boot.
config.eager_load = false

# Show full error reports.
config.consider_all_requests_local = true
config.consider_all_requests_local = false

# Enable server timing
config.server_timing = true
Expand Down
2 changes: 1 addition & 1 deletion config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
}

# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = false
config.cache_store = :null_store

Expand Down
2 changes: 1 addition & 1 deletion config/initializers/geocoder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
api_key: ENV["GOOGLE_API_KEY"],

# geocoding service request timeout, in seconds (default 3):
timeout: 5,
timeout: 15,

# set default units to kilometers:
units: :mi,
Expand Down
7 changes: 7 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,10 @@

en:
hello: "Hello world"
distance:
centi:
one: "foot"
other: "feet"
unit:
one: "mile"
other: "miles"
7 changes: 6 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@
get "up" => "rails/health#show", as: :rails_health_check

# Defines the root path route ("/")
# root "posts#index"
# root "drivers#index"

resources :drivers, only: %i[index] do
# get "rides", on: :member
resources :selectable_rides, only: :index, module: :drivers
end
end
8 changes: 8 additions & 0 deletions db/migrate/20240704202204_add_max_radius_to_driver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

class AddMaxRadiusToDriver < ActiveRecord::Migration[7.1]
def change
add_column :drivers, :max_radius, :integer, default: 10
add_index :drivers, :max_radius
end
end
4 changes: 3 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1a39ffb

Please sign in to comment.