diff --git a/.env.rb.sample b/.env.rb.sample index f149a87b..595a44bc 100644 --- a/.env.rb.sample +++ b/.env.rb.sample @@ -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'] = '' \ No newline at end of file diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index 6e4efc93..00000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "lib/generators/haml"] - path = lib/generators/haml - url = git://github.com/psynix/rails3_haml_scaffold_generator.git diff --git a/Gemfile b/Gemfile index fb098d17..ada39d8b 100644 --- a/Gemfile +++ b/Gemfile @@ -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' diff --git a/Gemfile.lock b/Gemfile.lock index ff22b255..ec72971b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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/ @@ -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) @@ -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) @@ -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) @@ -304,6 +304,12 @@ 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) @@ -311,6 +317,37 @@ GEM 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) @@ -318,6 +355,9 @@ GEM 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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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 diff --git a/app/controllers/trips_controller.rb b/app/controllers/trips_controller.rb index f6067d05..9378d878 100644 --- a/app/controllers/trips_controller.rb +++ b/app/controllers/trips_controller.rb @@ -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 diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 7d8426f0..02d1f2e4 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -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 diff --git a/app/models/ride.rb b/app/models/ride.rb index 3964c1dd..89e667ae 100644 --- a/app/models/ride.rb +++ b/app/models/ride.rb @@ -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) diff --git a/app/models/trip.rb b/app/models/trip.rb index d8ccce87..8534cf82 100644 --- a/app/models/trip.rb +++ b/app/models/trip.rb @@ -45,6 +45,34 @@ def to_s "#{origin} → #{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')) diff --git a/app/models/user.rb b/app/models/user.rb index f3903fdb..d98cda41 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 @@ -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 diff --git a/app/views/devise/registrations/new.html.haml b/app/views/devise/registrations/new.html.haml index c6b8fe01..87af05e3 100644 --- a/app/views/devise/registrations/new.html.haml +++ b/app/views/devise/registrations/new.html.haml @@ -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' diff --git a/app/views/users/geomap.json.jbuilder b/app/views/users/geomap.json.jbuilder index 8bf683ae..8487a71e 100644 --- a/app/views/users/geomap.json.jbuilder +++ b/app/views/users/geomap.json.jbuilder @@ -1 +1,2 @@ -json.array! user.to_geomap +json.distances @data["distances"] +json.trip_count @data["trip_count"] diff --git a/lib/generators/haml b/lib/generators/haml deleted file mode 160000 index 285c910f..00000000 --- a/lib/generators/haml +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 285c910f183e514da2a535039b7f431390e2171b diff --git a/lib/tasks/firebase.rake b/lib/tasks/firebase.rake new file mode 100644 index 00000000..deb1e09d --- /dev/null +++ b/lib/tasks/firebase.rake @@ -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 \ No newline at end of file diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index d6777610..8e6d114a 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -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 diff --git a/spec/models/ride_spec.rb b/spec/models/ride_spec.rb index 8fe844e6..a365e5e3 100644 --- a/spec/models/ride_spec.rb +++ b/spec/models/ride_spec.rb @@ -42,4 +42,28 @@ expect(ride.markdown_story).to eq("