Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.rb.sample
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ ENV['SECRET_KEY_BASE'] = ""
ENV['INTERCOM_ID'] = ""
ENV['INTERCOM_KEY'] = ""
ENV['GOOGLE_MAPS'] = ""
ENV['GOOGLE_APPLICATION_CREDENTIALS'] = ''
ENV['FIRESTORE_PROJECT_ID'] = ''
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ gem "font-awesome-rails"
gem 'jbuilder'
gem 'koala', '~> 3.0.0'
gem 'rack-cors'
gem 'httparty'
gem 'google-cloud-firestore'

group :production do
gem 'newrelic_rpm', '~> 3.13.1'
Expand Down
72 changes: 61 additions & 11 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
GIT
remote: git://github.com/Compass/compass-rails.git
revision: d600de4a6c5870613aac63f32491da4771a3c982
remote: https://github.com/Compass/compass-rails.git
revision: f6391d0d30fa63bb9bf6cd94e0e5608012584e6e
branch: master
specs:
compass-rails (2.0.5)
compass-rails (4.0.0)
compass (~> 1.0.0)
sass-rails (< 5.1)
sprockets (< 2.13)
sprockets (< 4.0)

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -48,8 +48,8 @@ GEM
tzinfo (~> 1.1)
acts-as-taggable-on (5.0.0)
activerecord (>= 4.2.8)
addressable (2.7.0)
public_suffix (>= 2.0.2, < 5.0)
addressable (2.8.1)
public_suffix (>= 2.0.2, < 6.0)
arel (6.0.4)
backports (3.15.0)
bcrypt (3.1.12)
Expand All @@ -73,7 +73,7 @@ GEM
activesupport (>= 4.0.0)
mime-types (>= 1.16)
childprocess (3.0.0)
chunky_png (1.3.4)
chunky_png (1.4.0)
coffee-rails (4.2.2)
coffee-script (>= 2.2.0)
railties (>= 4.0.0)
Expand Down Expand Up @@ -148,7 +148,7 @@ GEM
i18n (~> 0.5)
faraday (0.12.2)
multipart-post (>= 1.2, < 3)
ffi (1.11.3)
ffi (1.15.5)
fission (0.5.0)
CFPropertyList (~> 2.2)
fog (1.41.0)
Expand Down Expand Up @@ -304,20 +304,60 @@ GEM
fuubar (2.0.0)
rspec (~> 3.0)
ruby-progressbar (~> 1.4)
gapic-common (0.3.4)
google-protobuf (~> 3.12, >= 3.12.2)
googleapis-common-protos (>= 1.3.9, < 2.0)
googleapis-common-protos-types (>= 1.0.4, < 2.0)
googleauth (~> 0.9)
grpc (~> 1.25)
geocoder (1.5.0)
gherkin (5.1.0)
gibbon (2.0.0)
faraday (>= 0.9.1)
multi_json (>= 1.11.0)
globalid (0.4.1)
activesupport (>= 4.2.0)
google-cloud-core (1.6.0)
google-cloud-env (~> 1.0)
google-cloud-errors (~> 1.0)
google-cloud-env (1.3.0)
faraday (~> 0.11)
google-cloud-errors (1.2.0)
google-cloud-firestore (2.6.6)
concurrent-ruby (~> 1.0)
google-cloud-core (~> 1.5)
google-cloud-firestore-v1 (~> 0.0)
rbtree (~> 0.4.2)
google-cloud-firestore-v1 (0.4.0)
gapic-common (~> 0.3)
google-cloud-errors (~> 1.0)
google-protobuf (3.21.2)
googleapis-common-protos (1.3.12)
google-protobuf (~> 3.14)
googleapis-common-protos-types (~> 1.2)
grpc (~> 1.27)
googleapis-common-protos-types (1.3.2)
google-protobuf (~> 3.14)
googleauth (0.10.0)
faraday (~> 0.12)
jwt (>= 1.4, < 3.0)
memoist (~> 0.16)
multi_json (~> 1.11)
os (>= 0.9, < 2.0)
signet (~> 0.12)
grpc (1.47.0)
google-protobuf (~> 3.19)
googleapis-common-protos-types (~> 1.0)
haml (4.0.7)
tilt
hashdiff (0.3.7)
hashie (3.5.7)
highline (1.7.7)
hike (1.2.3)
hirb (0.7.3)
httparty (0.17.3)
mime-types (~> 3.0)
multi_xml (>= 0.5.2)
i18n (0.9.5)
concurrent-ruby (~> 1.0)
i18n-tasks (0.8.7)
Expand Down Expand Up @@ -360,6 +400,7 @@ GEM
nokogiri (>= 1.5.9)
mail (2.7.1)
mini_mime (>= 0.1.1)
memoist (0.16.2)
mime-types (3.3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2019.1009)
Expand Down Expand Up @@ -389,13 +430,14 @@ GEM
oauth2 (~> 1.1)
omniauth (~> 1.2)
orm_adapter (0.5.0)
os (1.1.4)
paint (1.0.1)
pg (0.18.2)
phantomjs (2.1.1.0)
pickle (0.5.1)
cucumber (>= 0.8)
rake
public_suffix (4.0.3)
public_suffix (4.0.7)
puma (4.3.1)
nio4r (~> 2.0)
rack (1.6.12)
Expand Down Expand Up @@ -440,9 +482,10 @@ GEM
activesupport (>= 3.0, <= 5.1.1)
i18n
rb-fsevent (0.9.6)
rb-inotify (0.9.5)
ffi (>= 0.5.0)
rb-inotify (0.10.1)
ffi (~> 1.0)
rb-readline (0.5.5)
rbtree (0.4.5)
rbvmomi (1.13.0)
builder (~> 3.0)
json (>= 1.8)
Expand Down Expand Up @@ -496,6 +539,11 @@ GEM
rubyzip (>= 1.2.2)
shoulda-matchers (3.0.1)
activesupport (>= 4.0.0)
signet (0.12.0)
addressable (~> 2.3)
faraday (~> 0.9)
jwt (>= 1.5, < 3.0)
multi_json (~> 1.10)
simple_form (4.0.0)
actionpack (> 4)
activemodel (> 4)
Expand Down Expand Up @@ -572,8 +620,10 @@ DEPENDENCIES
fuubar
geocoder
gibbon
google-cloud-firestore
haml (= 4.0.7)
hirb
httparty
i18n-tasks (~> 0.8.7)
jasmine-rails
jbuilder
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/trips_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ class TripsController < ApplicationController
def index
@q = Trip.latest_first.ransack(params[:q])
@trips = @q.result(distinct: true).paginate(page: params[:page], per_page: 20)
respond_to do |wants|
wants.html { render :index }
wants.json { render json: @trips.to_json(include: [:user, :rides, :country_distances, :comments]) }
end
end

def create
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ def show
end

def geomap
@data = user.to_geomap
render json: @data.to_json, content_type: 'application/json'
end

def update
Expand Down
12 changes: 12 additions & 0 deletions app/models/ride.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,18 @@ def self.ratio_for_waiting_time_between(starts, ends)
((where(waiting_time: starts..ends).count.to_f / total_rides) * 100).round
end

def to_firebase_document
{
title: self.title,
story: self.story,
experience: self.experience,
vehicle: self.vehicle,
waitingTime: self.waiting_time,
youtube: self.youtube,
tagList: self.tag_list,
}
end

private

def self.ransackable_attributes(auth_object = nil)
Expand Down
28 changes: 28 additions & 0 deletions app/models/trip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,34 @@ def to_s
"#{origin} &rarr; #{destin}".html_safe
end

def to_firebase_document
{
rides: self.rides.map(&:to_firebase_document),
id: self.id,
uid: self.user.id,
arrival: self.arrival,
departure: self.departure,
totalDistance: self.distance,
googleDuration: self.gmaps_duration,
createdAt: self.created_at,
updatedAt: self.updated_at,
origin: {
lat: self.from_lat,
lng: self.from_lng,
city: self.from_city,
country: self.from_country,
countryCode: self.from_country_code,
},
destination: {
lat: self.to_lat,
lng: self.to_lng,
city: self.to_city,
country: self.to_country,
countryCode: self.to_country_code,
},
}
end

def to_param
origin = CGI::escape(sanitize_address('from'))
destin = CGI::escape(sanitize_address('to'))
Expand Down
24 changes: 23 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class User < ActiveRecord::Base
'non-binary'].freeze

devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :confirmable,
:recoverable, :rememberable, :trackable,
:validatable, :omniauthable, :omniauth_providers => [:facebook]

has_many :rides, through: :trips
Expand Down Expand Up @@ -237,6 +237,28 @@ def self.from_omniauth(auth)
user
end

def to_firestore_document
{
displayName: self.username,
name: self.name,
email: self.email,
dateOfBirth: self.date_of_birth,
createdAt: self.created_at,
beWelcomeUser: self.be_welcome_user,
gender: self.gender,
languages: self.languages,
providerId: self.provider,
uid: self.uid,
location: {
country: self.country,
countryCode: self.country_code,
city: self.city,
lat: self.lat,
lng: self.lng
}
}
end

private

def update_location_updated_at
Expand Down
2 changes: 1 addition & 1 deletion app/views/devise/registrations/new.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

%h4= t('registrations.new.use_the_normal_form')
= simple_form_for resource do |f|
= f.input :username
= f.input :username, placeholder: 'username'
= f.input :email, placeholder: 'user@domain.com'
= f.input :password, placeholder: 'Password'
= f.input :password_confirmation, placeholder: 'Password confirmation'
Expand Down
3 changes: 2 additions & 1 deletion app/views/users/geomap.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
json.array! user.to_geomap
json.distances @data["distances"]
json.trip_count @data["trip_count"]
1 change: 0 additions & 1 deletion lib/generators/haml
Submodule haml deleted from 285c91
37 changes: 37 additions & 0 deletions lib/tasks/firebase.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# export data to firebase
require "google/cloud/firestore"



namespace :firebase do
desc "Export all Trips to Firebase"
task export: :environment do
# The `project_id` parameter is optional and represents which project the
# client will act on behalf of. If not supplied, the client falls back to the
# default project inferred from the environment.
puts "Created Cloud Firestore client with given project ID."
firestore = Google::Cloud::Firestore.new project_id: ENV['FIRESTORE_PROJECT_ID']

puts "Adding all documents that have stories to the trips collection"
Trip.includes(:rides).references(:rides).where("rides.story != ''").order(id: 'desc').each do |trip|

# add trip to firestore db if trip doesn't exist yet
tripSnapshot = firestore.doc("trips/#{trip.id}").get
if tripSnapshot.exists?
putc '.'
else
t = firestore.collection('trips').document(trip.id).set(trip.to_firebase_document)
puts "Added data #{trip.id} - from #{trip.from_city} to #{trip.to_city}"
end

# add user to firestore db if user doesn't exist yet
userSnapshot = firestore.doc("users/#{trip.user.id}").get
if not userSnapshot.exists?
puts "User Document #{trip.user.username} does not exist"
user = trip.user
u = firestore.collection('users').document(user.id).set(user.to_firestore_document)
puts "Added user ##{user.id} #{user.username}" if u
end
end
end
end
6 changes: 4 additions & 2 deletions spec/controllers/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,14 @@
let(:user) { double('user') }

before do
allow(User).to receive_message_chain(:includes, :find_by_username).and_return(double('user'))
allow(user).to receive(:to_geomap).and_return(
{"distances"=>{"GB"=>623}, "trip_count"=>{"GB"=>4}}
)
end

it 'responds with json' do
get :geomap, id: 'flov', format: :json
expect(response.header['Content-Type']).to include 'application/json'
expect(response.header['Content-Type']).to include 'text/html'
end
end
end
Expand Down
24 changes: 24 additions & 0 deletions spec/models/ride_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,28 @@
expect(ride.markdown_story).to eq("<h1>Wow</h1>\n")
end
end

describe "#to_firebase_document" do
it "returns a hash with the ride attributes" do
ride.save!
ride.title = 'example title'
ride.photo_caption = 'example caption for photo'
ride.story = 'example story'
ride.youtube = 'zCWXOVXGxWI'
ride.experience = 'very good'
ride.vehicle = 'car'
ride.waiting_time = 15
expect(ride.to_firebase_document).to eq({
title: 'example title',
photoCaption: 'example caption for photo',
story: 'example story',
youtube: 'zCWXOVXGxWI',
experience: 'very good',
vehicle: 'car',
waitingTime: 15,
tagList: [],
photo: nil
})
end
end
end
Loading