Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring code and testing Set Up- Icalialabs::Firebase Gem. #1

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper
128 changes: 47 additions & 81 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,122 +2,54 @@ PATH
remote: .
specs:
icalialabs-firebase (0.1.0)
rails (>= 7.0.4)
rspec
rspec-rails
rubocop

GEM
remote: https://rubygems.org/
specs:
actioncable (7.0.4)
actionpack (= 7.0.4)
activesupport (= 7.0.4)
nio4r (~> 2.0)
websocket-driver (>= 0.6.1)
actionmailbox (7.0.4)
actionpack (= 7.0.4)
activejob (= 7.0.4)
activerecord (= 7.0.4)
activestorage (= 7.0.4)
activesupport (= 7.0.4)
mail (>= 2.7.1)
net-imap
net-pop
net-smtp
actionmailer (7.0.4)
actionpack (= 7.0.4)
actionview (= 7.0.4)
activejob (= 7.0.4)
activesupport (= 7.0.4)
mail (~> 2.5, >= 2.5.4)
net-imap
net-pop
net-smtp
rails-dom-testing (~> 2.0)
actionpack (7.0.4)
actionview (= 7.0.4)
activesupport (= 7.0.4)
rack (~> 2.0, >= 2.2.0)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.2.0)
actiontext (7.0.4)
actionpack (= 7.0.4)
activerecord (= 7.0.4)
activestorage (= 7.0.4)
activesupport (= 7.0.4)
globalid (>= 0.6.0)
nokogiri (>= 1.8.5)
actionview (7.0.4)
activesupport (= 7.0.4)
builder (~> 3.1)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.1, >= 1.2.0)
activejob (7.0.4)
activesupport (= 7.0.4)
globalid (>= 0.3.6)
activemodel (7.0.4)
activesupport (= 7.0.4)
activerecord (7.0.4)
activemodel (= 7.0.4)
activesupport (= 7.0.4)
activestorage (7.0.4)
actionpack (= 7.0.4)
activejob (= 7.0.4)
activerecord (= 7.0.4)
activesupport (= 7.0.4)
marcel (~> 1.0)
mini_mime (>= 1.1.0)
activesupport (7.0.4)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
ast (2.4.2)
builder (3.2.4)
concurrent-ruby (1.1.10)
crass (1.0.6)
diff-lcs (1.5.0)
erubi (1.11.0)
globalid (1.0.0)
activesupport (>= 5.0)
i18n (1.12.0)
concurrent-ruby (~> 1.0)
json (2.6.2)
loofah (2.19.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
mail (2.7.1)
mini_mime (>= 0.1.1)
marcel (1.0.2)
method_source (1.0.0)
mini_mime (1.1.2)
minitest (5.16.3)
net-imap (0.3.1)
net-protocol
net-pop (0.1.2)
net-protocol
net-protocol (0.1.3)
timeout
net-smtp (0.3.2)
net-protocol
nio4r (2.5.8)
nokogiri (1.13.8-x86_64-linux)
racc (~> 1.4)
parallel (1.22.1)
parser (3.1.2.1)
ast (~> 2.4.1)
racc (1.6.0)
rack (2.2.4)
rack-test (2.0.2)
rack (>= 1.3)
rails (7.0.4)
actioncable (= 7.0.4)
actionmailbox (= 7.0.4)
actionmailer (= 7.0.4)
actionpack (= 7.0.4)
actiontext (= 7.0.4)
actionview (= 7.0.4)
activejob (= 7.0.4)
activemodel (= 7.0.4)
activerecord (= 7.0.4)
activestorage (= 7.0.4)
activesupport (= 7.0.4)
bundler (>= 1.15.0)
railties (= 7.0.4)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
Expand All @@ -130,7 +62,44 @@ GEM
rake (>= 12.2)
thor (~> 1.0)
zeitwerk (~> 2.5)
rainbow (3.1.1)
rake (13.0.6)
regexp_parser (2.6.0)
rexml (3.2.5)
rspec (3.11.0)
rspec-core (~> 3.11.0)
rspec-expectations (~> 3.11.0)
rspec-mocks (~> 3.11.0)
rspec-core (3.11.0)
rspec-support (~> 3.11.0)
rspec-expectations (3.11.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.11.0)
rspec-mocks (3.11.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.11.0)
rspec-rails (6.0.0)
actionpack (>= 6.1)
activesupport (>= 6.1)
railties (>= 6.1)
rspec-core (~> 3.11)
rspec-expectations (~> 3.11)
rspec-mocks (~> 3.11)
rspec-support (~> 3.11)
rspec-support (3.11.1)
rubocop (1.36.0)
json (~> 2.3)
parallel (~> 1.10)
parser (>= 3.1.2.1)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.20.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.21.0)
parser (>= 3.1.1.0)
ruby-progressbar (1.11.0)
sprockets (4.1.1)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
Expand All @@ -139,12 +108,9 @@ GEM
activesupport (>= 5.2)
sprockets (>= 3.0.0)
thor (1.2.1)
timeout (0.3.0)
tzinfo (2.0.5)
concurrent-ruby (~> 1.0)
websocket-driver (0.7.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
unicode-display_width (2.3.0)
zeitwerk (2.6.1)

PLATFORMS
Expand Down
6 changes: 5 additions & 1 deletion icalialabs-firebase.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,9 @@ Gem::Specification.new do |spec|
Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.md"]
end

spec.add_dependency "rails", ">= 7.0.4"
spec.add_dependency "rubocop"
spec.add_dependency "rspec"
spec.add_dependency "rspec-rails"
spec.add_dependency "capybara"
spec.add_dependency "cuprite"
end
68 changes: 41 additions & 27 deletions lib/generators/icalialabs_firebase/icalialabs_firebase_generator.rb
Original file line number Diff line number Diff line change
@@ -1,41 +1,55 @@
class IcalialabsFirebaseGenerator < Rails::Generators::NamedBase
source_root File.expand_path("../../../", __dir__)
source_root File.expand_path('../../../', __dir__)

def create_views
# ---------- Install argument. -------------
if class_name == 'Install'
template 'lib/generators/icalialabs_firebase/templates/firebase_controller.rb',
File.join('app/controllers', class_path, "firebase_controller.rb")
route "get '/login', to: 'firebase#login'"
route "get '/signup', to: 'firebase#signup'"
route "post '/create_user', to: 'firebase#create_user'"
route "post '/create_session', to: 'firebase#create_session'"
route "delete '/logout', to: 'firebase#logout', as: 'logout'"
inject_into_file 'app/controllers/application_controller.rb',
after: "class ApplicationController < ActionController::Base\n" do <<-'RUBY'
case class_name
when 'Install'
copy_templates_with_install
firebase_routes
inject_code_with_install
when 'Views'
directory 'app/views/firebase/', 'app/views/firebase/'
views_readme
else
error_readme
end
end

private

def copy_templates_with_install
template 'lib/generators/icalialabs_firebase/templates/firebase_controller.rb',
File.join('app/controllers', class_path, 'firebase_controller.rb')
template 'lib/generators/icalialabs_firebase/templates/firebase_spec.rb',
File.join('app/spec/system', class_path, 'firebase_spec.rb')
end

def firebase_routes
route "get '/login', to: 'firebase#login', as: 'login'"
route "get '/signup', to: 'firebase#signup', as: 'signup'"
route "post '/create_user', to: 'firebase#create_user'"
route "post '/create_session', to: 'firebase#create_session'"
route "delete '/logout', to: 'firebase#logout', as: 'logout'"
end

def inject_code_with_install
inject_into_file 'app/controllers/application_controller.rb',
after: "class ApplicationController < ActionController::Base\n" do <<-'RUBY'
helper_method :current_user

def current_user
@current_user ||= session[:user_id]
end
RUBY
RUBY
end
end

# ---------- Views argument. -------------
elsif class_name == 'Views'
directory 'app/views/firebase/', 'app/views/firebase/'
inject_into_file 'app/views/layouts/application.html.erb',
after: "<body>\n" do <<-"RUBY"
<%= notice if notice %>
<% if current_user.present? %>
<%= button_to "Log Out", logout_path, to: "pages#logout", method: :delete %>
<% else %>
<%= button_to 'Log in', login_path, method: :get %>
<%= button_to 'Sign up', signup_path, method: :get %>
<% end %>
RUBY
def views_readme
readme "lib/generators/icalialabs_firebase/templates/views_readme.md"
end
end

def error_readme
readme "lib/generators/icalialabs_firebase/templates/error_readme.md"
end
end

Expand Down
11 changes: 11 additions & 0 deletions lib/generators/icalialabs_firebase/templates/error_readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

======================================

Sorry, that command is not recognized.
Try one of the commands below:

- rails g icalialabs_firebase install
- rails g icalialabs_firebase views

======================================

Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,36 @@ def login; end
def signup; end

def create_user
uri = URI("https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=HERE_GOES_MY_API_KEY")
uri = URI('https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=HERE_GOES_MY_API_KEY')
response = Net::HTTP.post_form(uri, "email": @email, "password": @password)

data = JSON.parse(response.body)
session[:user_id] = data["localId"]
session[:user_id] = data['localId']
session[:data] = data

redirect_to root_path, notice: "Sign Up successfully." if response.is_a?(Net::HTTPSuccess)
if response.is_a?(Net::HTTPSuccess)
redirect_to root_path, notice: 'Sign Up successfully.'
else
redirect_to signup_path, notice: 'Something happend, try again.'
end
end

def create_session
uri = URI("https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=HERE_GOES_MY_API_KEY")
uri = URI('https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=HERE_GOES_MY_API_KEY')
response = Net::HTTP.post_form(uri, "email": @email, "password": @password)

data = JSON.parse(response.body)

if response.is_a?(Net::HTTPSuccess)
session[:user_id] = data["localId"]
session[:user_id] = data['localId']
session[:data] = data
redirect_to root_path, notice: "Log in successfully."
redirect_to root_path, notice: 'Log in successfully.'
end
end

def logout
session.clear
redirect_to root_path, notice: "Successfully log out"
redirect_to root_path, notice: 'Successfully log out'
end

private
Expand Down
15 changes: 15 additions & 0 deletions lib/generators/icalialabs_firebase/templates/firebase_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

require 'system_helper'

RSpec.describe 'Firebase', type: :system do
scenario "User can see Sign Up page" do
visit signup_path
expect(page).to have_content 'Sign up'
end

scenario "User can see Login page" do
visit login_path
expect(page).to have_content 'Log in'
end
end
20 changes: 20 additions & 0 deletions lib/generators/icalialabs_firebase/templates/views_readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

=======================================================

Insert this code below your body tag in 'app/views/layouts/application.html.erb':

--------
<%= notice if notice %>

<% if current_user.present? %>
<%= button_to 'Log Out', logout_path, to: 'pages#logout', method: :delete, id: 'logout_button' %>
<% else %>
<%= button_to 'Log in', login_path, method: :get, id: 'login_button' %>
<%= button_to 'Sign up', signup_path, method: :get, id: 'signup_button' %>
<% end %>
---------

- Notice for showing messages.
- Buttons for Log in, Sign up and Log out.

=======================================================
Loading