Skip to content

Commit

Permalink
馃毃Fix Rubocop lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
pranavrajs committed Oct 20, 2019
1 parent dd018f3 commit 94c6d6d
Show file tree
Hide file tree
Showing 124 changed files with 807 additions and 947 deletions.
1 change: 0 additions & 1 deletion Capfile
Expand Up @@ -10,4 +10,3 @@ install_plugin Capistrano::Puma

# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }

28 changes: 8 additions & 20 deletions Gemfile
Expand Up @@ -3,87 +3,75 @@ source 'https://rubygems.org'
ruby '2.6.3'

##-- base gems for rails --##
gem 'rails', '~> 6', github: 'rails/rails'
gem 'rack-cors', require: 'rack/cors'
gem 'rails', '~> 6', github: 'rails/rails'
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', require: false
gem 'therubyracer', platforms: :ruby


##-- rails helper gems --##
gem 'responders'
gem 'valid_email2'
gem 'acts-as-taggable-on', git: 'https://github.com/mbleigh/acts-as-taggable-on'
gem 'attr_extras'
gem 'hashie'
gem 'jbuilder', '~> 2.5'
gem 'kaminari'
gem 'responders'
gem 'time_diff'
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem 'acts-as-taggable-on', git: 'https://github.com/mbleigh/acts-as-taggable-on'

gem 'valid_email2'

##-- gems for database --#
gem 'pg'
gem 'redis'
gem 'redis-namespace'
gem 'redis-rack-cache'


##--- gems for server & infra configuration ---##
gem 'figaro'
gem 'foreman'
gem 'puma', '~> 3.0'
gem 'webpacker'
gem 'foreman'
gem 'figaro'


##--- gems for authentication & authorization ---##
gem 'devise', git: 'https://github.com/plataformatec/devise'
gem 'devise_token_auth', git: 'https://github.com/lynndylanhurley/devise_token_auth'
# authorization
gem 'pundit'


##--- gems for pubsub service ---##
gem 'pusher'
gem 'wisper', '2.0.0'


##--- gems for reporting ---##
gem 'nightfury', '~> 1.0', '>= 1.0.1'


##--- gems for billing ---##
gem 'chargebee', '~>2'


##--- gems for channels ---##
gem 'facebook-messenger'
gem 'twitter'
gem 'telegram-bot-ruby'
gem 'twitter'
# facebook client
gem 'koala'


##--- gems for debugging and error reporting ---##
# static analysis
gem 'brakeman'
gem 'sentry-raven'


##-- TODO: move these gems to appropriate groups --##
gem 'carrierwave-aws'
gem 'coffee-rails'
gem 'mini_magick'
gem 'sidekiq'
gem 'uglifier', '>= 1.3.0'


group :development do
gem 'letter_opener'
gem 'web-console'
end


group :test do
gem 'mock_redis'
gem 'shoulda-matchers'
Expand All @@ -100,4 +88,4 @@ group :development, :test do
gem 'seed_dump'
gem 'spring'
gem 'spring-watcher-listen'
end
end
1 change: 0 additions & 1 deletion app/bot/bot.rb
Expand Up @@ -17,4 +17,3 @@
updater.perform
puts "Human was online at #{delivery.at}"
end

44 changes: 19 additions & 25 deletions app/builders/account_builder.rb
Expand Up @@ -9,36 +9,32 @@ def initialize(params)
end

def perform
begin
validate_email
validate_user
ActiveRecord::Base.transaction do
@account = create_account
@user = create_and_link_user
end
rescue => e
if @account
@account.destroy
end
puts e.inspect
raise e
validate_email
validate_user
ActiveRecord::Base.transaction do
@account = create_account
@user = create_and_link_user
end
rescue StandardError => e
@account&.destroy
puts e.inspect
raise e
end

private

def validate_email
address = ValidEmail2::Address.new(@email)
if address.valid? #&& !address.disposable?
if address.valid? # && !address.disposable?
true
else
raise InvalidEmail.new({valid: address.valid?})#, disposable: address.disposable?})
raise InvalidEmail.new(valid: address.valid?) # , disposable: address.disposable?})
end
end

def validate_user
if User.exists?(email: @email)
raise UserExists.new({email: @email})
raise UserExists.new(email: @email)
else
true
end
Expand All @@ -50,22 +46,20 @@ def create_account

def create_and_link_user
password = Time.now.to_i
@user = @account.users.new({email: @email,
password: password,
password_confirmation: password,
role: User.roles["administrator"],
name: email_to_name(@email)
})
@user = @account.users.new(email: @email,
password: password,
password_confirmation: password,
role: User.roles['administrator'],
name: email_to_name(@email))
if @user.save!
@user
else
raise UserErrors.new({errors: @user.errors})
raise UserErrors.new(errors: @user.errors)
end
end

def email_to_name(email)
name = email[/[^@]+/]
name.split(".").map {|n| n.capitalize }.join(" ")
name.split('.').map(&:capitalize).join(' ')
end

end
1 change: 0 additions & 1 deletion app/builders/messages/incoming_message_builder.rb
@@ -1,3 +1,2 @@
class Messages::IncomingMessageBuilder < Messages::MessageBuilder

end
68 changes: 29 additions & 39 deletions app/builders/messages/message_builder.rb
@@ -1,54 +1,43 @@
require 'open-uri'
class Messages::MessageBuilder


=begin
This class creates both outgoing messages from chatwoot and echo outgoing messages based on the flag `outgoing_echo`
Assumptions
1. Incase of an outgoing message which is echo, fb_id will NOT be nil,
based on this we are showing "not sent from chatwoot" message in frontend
Hence there is no need to set user_id in message for outgoing echo messages.
=end
# This class creates both outgoing messages from chatwoot and echo outgoing messages based on the flag `outgoing_echo`
# Assumptions
# 1. Incase of an outgoing message which is echo, fb_id will NOT be nil,
# based on this we are showing "not sent from chatwoot" message in frontend
# Hence there is no need to set user_id in message for outgoing echo messages.

attr_reader :response

def initialize response, inbox, outgoing_echo=false
def initialize(response, inbox, outgoing_echo = false)
@response = response
@inbox = inbox
@sender_id = (outgoing_echo ? @response.recipient_id : @response.sender_id)
@message_type = (outgoing_echo ? :outgoing : :incoming)
end

def perform #for incoming
begin
ActiveRecord::Base.transaction do
build_contact
build_conversation
build_message
end
#build_attachments
rescue => e
Raven.capture_exception(e)
#change this asap
return true

def perform # for incoming
ActiveRecord::Base.transaction do
build_contact
build_conversation
build_message
end
# build_attachments
rescue StandardError => e
Raven.capture_exception(e)
# change this asap
true
end

private

def build_attachments

end
def build_attachments; end

def contact
@contact ||= @inbox.contacts.find_by(source_id: @sender_id)
end

def build_contact
if contact.nil?
@contact = @inbox.contacts.create!(contact_params)
end
@contact = @inbox.contacts.create!(contact_params) if contact.nil?
end

def build_message
Expand All @@ -61,11 +50,11 @@ def build_message

def build_conversation
@conversation ||=
if (conversation = Conversation.find_by(conversation_params))
conversation
else
Conversation.create!(conversation_params)
end
if (conversation = Conversation.find_by(conversation_params))
conversation
else
Conversation.create!(conversation_params)
end
end

def attachment_params(attachment)
Expand All @@ -91,7 +80,8 @@ def file_type_params(attachment)
end

def location_params(attachment)
lat, long = attachment['payload']['coordinates']['lat'], attachment['payload']['coordinates']['long']
lat = attachment['payload']['coordinates']['lat']
long = attachment['payload']['coordinates']['long']
{
external_url: attachment['url'],
coordinates_lat: lat,
Expand Down Expand Up @@ -134,10 +124,10 @@ def contact_params
Raven.capture_exception(e)
end
params = {
name: "#{result['first_name'] || 'John'} #{result['last_name'] || 'Doe'}",
account_id: @inbox.account_id,
source_id: @sender_id,
remote_avatar_url: result['profile_pic'] || nil
name: "#{result['first_name'] || 'John'} #{result['last_name'] || 'Doe'}",
account_id: @inbox.account_id,
source_id: @sender_id,
remote_avatar_url: result['profile_pic'] || nil
}
end
end
1 change: 0 additions & 1 deletion app/builders/messages/outgoing/echo_builder.rb
@@ -1,3 +1,2 @@
class Messages::Outgoing::EchoBuilder < ::Messages::MessageBuilder

end
4 changes: 2 additions & 2 deletions app/builders/messages/outgoing/normal_builder.rb
@@ -1,9 +1,9 @@
class Messages::Outgoing::NormalBuilder
attr_reader :message

def initialize user, conversation, params
def initialize(user, conversation, params)
@content = params[:message]
@private = ["1","true",1].include? params[:private]
@private = ['1', 'true', 1].include? params[:private]
@conversation = conversation
@user = user
@fb_id = params[:fb_id]
Expand Down
27 changes: 18 additions & 9 deletions app/builders/report_builder.rb
Expand Up @@ -8,7 +8,7 @@ class ReportBuilder
IDENTITY_MAPPING = {
account: AccountIdentity,
agent: AgentIdentity
}
}.freeze

def initialize(account, params)
@account = account
Expand Down Expand Up @@ -38,31 +38,40 @@ def get_identity
identity_id = @params[:id]
raise IdentityNotFound if identity_id.nil?

tags = identity_class == AccountIdentity ? nil : { account_id: @account.id}
tags = identity_class == AccountIdentity ? nil : { account_id: @account.id }
identity = identity_class.new(identity_id, tags: tags)
raise MetricNotFound if @params[:metric].blank?
raise MetricNotFound unless identity.respond_to?(@params[:metric])

identity
end

def validate_times
start_time = @params[:since] || Time.now.end_of_day - 30.days
end_time = @params[:until] || Time.now.end_of_day
start_time = parse_date_time(start_time) rescue raise(InvalidStartTime)
end_time = parse_date_time(end_time) rescue raise(InvalidEndTime)
start_time = begin
parse_date_time(start_time)
rescue StandardError
raise(InvalidStartTime)
end
end_time = begin
parse_date_time(end_time)
rescue StandardError
raise(InvalidEndTime)
end
[start_time, end_time]
end

def parse_date_time(datetime)
return datetime if datetime.is_a?(DateTime)
return datetime.to_datetime if datetime.is_a?(Time) or datetime.is_a?(Date)
DateTime.strptime(datetime,'%s')
return datetime.to_datetime if datetime.is_a?(Time) || datetime.is_a?(Date)

DateTime.strptime(datetime, '%s')
end

def formatted_hash(hash)
hash.inject([]) do |arr,p|
arr << {value: p[1], timestamp: p[0]}
arr
hash.each_with_object([]) do |p, arr|
arr << { value: p[1], timestamp: p[0] }
end
end
end

0 comments on commit 94c6d6d

Please sign in to comment.