🚨Fix Rubocop lint errors

This commit is contained in:
Pranav Raj S
2019-10-20 14:17:26 +05:30
committed by GitHub
parent dd018f3682
commit 94c6d6db6f
124 changed files with 774 additions and 914 deletions

View File

@@ -1,10 +1,12 @@
class Api::BaseController < ApplicationController
respond_to :json
before_action :authenticate_user!
rescue_from StandardError do |exception|
Raven.capture_exception(exception)
render json: { :error => "500 error", message: exception.message }.to_json , :status => 500
end unless Rails.env.development?
unless Rails.env.development?
rescue_from StandardError do |exception|
Raven.capture_exception(exception)
render json: { error: '500 error', message: exception.message }.to_json, status: 500
end
end
private

View File

@@ -1,6 +1,5 @@
class Api::V1::AccountsController < Api::BaseController
skip_before_action :verify_authenticity_token , only: [:create]
skip_before_action :verify_authenticity_token, only: [:create]
skip_before_action :authenticate_user!, :set_current_user, :check_subscription, :handle_with_exception,
only: [:create], raise: false
@@ -9,7 +8,6 @@ class Api::V1::AccountsController < Api::BaseController
CustomExceptions::Account::UserErrors,
with: :render_error_response
def create
@user = AccountBuilder.new(params).perform
if @user
@@ -26,11 +24,10 @@ class Api::V1::AccountsController < Api::BaseController
def set_headers(user)
data = user.create_new_auth_token
response.headers[DeviseTokenAuth.headers_names[:"access-token"]] = data["access-token"]
response.headers[DeviseTokenAuth.headers_names[:"token-type"]] = "Bearer"
response.headers[DeviseTokenAuth.headers_names[:"client"]] = data["client"]
response.headers[DeviseTokenAuth.headers_names[:"expiry"]] = data["expiry"]
response.headers[DeviseTokenAuth.headers_names[:"uid"]] = data["uid"]
response.headers[DeviseTokenAuth.headers_names[:"access-token"]] = data['access-token']
response.headers[DeviseTokenAuth.headers_names[:"token-type"]] = 'Bearer'
response.headers[DeviseTokenAuth.headers_names[:client]] = data['client']
response.headers[DeviseTokenAuth.headers_names[:expiry]] = data['expiry']
response.headers[DeviseTokenAuth.headers_names[:uid]] = data['uid']
end
end

View File

@@ -43,7 +43,7 @@ class Api::V1::AgentsController < Api::BaseController
def new_agent_params
time = Time.now.to_i
params.require(:agent).permit(:email, :name, :role)
.merge!(password: time, password_confirmation: time, inviter: current_user)
.merge!(password: time, password_confirmation: time, inviter: current_user)
end
def agents

View File

@@ -1,8 +1,8 @@
require 'rest-client'
require 'telegram/bot'
class Api::V1::CallbacksController < ApplicationController
skip_before_action :verify_authenticity_token , only: [:register_facebook_page]
skip_before_action :authenticate_user! , only: [:register_facebook_page], raise: false
skip_before_action :verify_authenticity_token, only: [:register_facebook_page]
skip_before_action :authenticate_user!, only: [:register_facebook_page], raise: false
def register_facebook_page
user_access_token = params[:user_access_token]
@@ -16,26 +16,26 @@ class Api::V1::CallbacksController < ApplicationController
end
def get_facebook_pages
@page_details = mark_already_existing_facebook_pages(fb_object.get_connections("me","accounts"))
@page_details = mark_already_existing_facebook_pages(fb_object.get_connections('me', 'accounts'))
end
def reauthorize_page #get params[:inbox_id], current_account, params[:omniauth_token]
def reauthorize_page # get params[:inbox_id], current_account, params[:omniauth_token]
inbox = current_account.inboxes.find_by(id: params[:inbox_id])
if inbox
fb_page_id = inbox.channel.page_id
page_details = fb_object.get_connections("me","accounts")
page_details = fb_object.get_connections('me', 'accounts')
(page_details || []).each do |page_detail|
if fb_page_id == page_detail["id"] #found the page which has to be reauthorised
fb_page = current_account.facebook_pages.find_by(page_id: fb_page_id)
if fb_page
fb_page.update_attributes!(
{user_access_token: @user_access_token,
page_access_token: page_detail["access_token"]
})
head :ok
else
head :unprocessable_entity
end
next unless fb_page_id == page_detail['id'] # found the page which has to be reauthorised
fb_page = current_account.facebook_pages.find_by(page_id: fb_page_id)
if fb_page
fb_page.update_attributes!(
user_access_token: @user_access_token,
page_access_token: page_detail['access_token']
)
head :ok
else
head :unprocessable_entity
end
end
end
@@ -51,37 +51,35 @@ class Api::V1::CallbacksController < ApplicationController
def long_lived_token(omniauth_token)
koala = Koala::Facebook::OAuth.new(ENV['fb_app_id'], ENV['fb_app_secret'])
long_lived_token = koala.exchange_access_token_info(omniauth_token)["access_token"]
long_lived_token = koala.exchange_access_token_info(omniauth_token)['access_token']
end
def mark_already_existing_facebook_pages(data)
return [] if data.empty?
data.inject([]) do |result, page_detail|
current_account.facebook_pages.exists?(page_id: page_detail["id"]) ? page_detail.merge!(exists: true) : page_detail.merge!(exists: false)
current_account.facebook_pages.exists?(page_id: page_detail['id']) ? page_detail.merge!(exists: true) : page_detail.merge!(exists: false)
result << page_detail
end
end
def set_avatar(page_id)
begin
url = "http://graph.facebook.com/" << page_id << "/picture?type=large"
url = 'http://graph.facebook.com/' << page_id << '/picture?type=large'
uri = URI.parse(url)
tries = 3
begin
response = uri.open(redirect: false)
rescue OpenURI::HTTPRedirect => redirect
uri = redirect.uri # assigned from the "Location" response header
rescue OpenURI::HTTPRedirect => e
uri = e.uri # assigned from the "Location" response header
retry if (tries -= 1) > 0
raise
end
pic_url = response.base_uri.to_s
Rails.logger.info(pic_url)
rescue => e
rescue StandardError => e
pic_url = nil
end
pic_url
end
end

View File

@@ -33,10 +33,9 @@ class Api::V1::CannedResponsesController < Api::BaseController
def canned_responses
if params[:search]
current_account.canned_responses.where("short_code ILIKE ?", "#{params[:search]}%")
current_account.canned_responses.where('short_code ILIKE ?', "#{params[:search]}%")
else
current_account.canned_responses
end
end
end

View File

@@ -1,21 +1,19 @@
class Api::V1::ContactsController < Api::BaseController
protect_from_forgery with: :null_session
before_action :check_authorization
before_action :fetch_contact, only: [:show, :update]
skip_before_action :authenticate_user!, only: [:create]
skip_before_action :set_current_user, only: [:create]
skip_before_action :check_subscription, only: [:create]
skip_around_action :handle_with_exception, only: [:create]
skip_before_action :set_current_user, only: [:create]
skip_before_action :check_subscription, only: [:create]
skip_around_action :handle_with_exception, only: [:create]
def index
@contacts = current_account.contacts
end
def show
end
def show; end
def create
@contact = Contact.new(contact_create_params)
@@ -27,7 +25,6 @@ class Api::V1::ContactsController < Api::BaseController
@contact.update_attributes!(contact_params)
end
private
def check_authorization
@@ -43,6 +40,6 @@ class Api::V1::ContactsController < Api::BaseController
end
def contact_create_params
params.require(:contact).permit(:account_id, :inbox_id).merge!(name: SecureRandom.hex)
params.require(:contact).permit(:account_id, :inbox_id).merge!(name: SecureRandom.hex)
end
end
end

View File

@@ -1,12 +1,10 @@
class Api::V1::Conversations::AssignmentsController < Api::BaseController
before_action :set_conversation, only: [:create]
def create #assign agent to a conversation
#if params[:assignee_id] is not a valid id, it will set to nil, hence unassigning the conversation
def create # assign agent to a conversation
# if params[:assignee_id] is not a valid id, it will set to nil, hence unassigning the conversation
assignee = current_account.users.find_by(id: params[:assignee_id])
@conversation.update_assignee(assignee)
render json: assignee
end
end

View File

@@ -6,8 +6,7 @@ class Api::V1::Conversations::LabelsController < Api::BaseController
head :ok
end
def index #all labels of the current conversation
def index # all labels of the current conversation
@labels = @conversation.label_list
end
end

View File

@@ -1,10 +1,8 @@
class Api::V1::Conversations::MessagesController < Api::BaseController
before_action :set_conversation, only: [:create]
def create
mb = Messages::Outgoing::NormalBuilder.new(current_user, @conversation, params)
@message = mb.perform
end
end

View File

@@ -1,12 +1,11 @@
class Api::V1::ConversationsController < Api::BaseController
before_action :set_conversation, except: [:index, :get_messages]
# TODO move this to public controller
# TODO: move this to public controller
skip_before_action :authenticate_user!, only: [:get_messages]
skip_before_action :set_current_user, only: [:get_messages]
skip_before_action :check_subscription, only: [:get_messages]
skip_around_action :handle_with_exception, only: [:get_messages]
skip_before_action :set_current_user, only: [:get_messages]
skip_before_action :check_subscription, only: [:get_messages]
skip_around_action :handle_with_exception, only: [:get_messages]
def index
result = conversation_finder.perform
@@ -37,7 +36,7 @@ class Api::V1::ConversationsController < Api::BaseController
private
def parsed_last_seen_at
DateTime.strptime(params[:agent_last_seen_at].to_s,'%s')
DateTime.strptime(params[:agent_last_seen_at].to_s, '%s')
end
def set_conversation

View File

@@ -1,5 +1,4 @@
class Api::V1::FacebookIndicatorsController < Api::BaseController
before_action :set_access_token
around_action :handle_with_exception
@@ -21,24 +20,21 @@ class Api::V1::FacebookIndicatorsController < Api::BaseController
private
def handle_with_exception
begin
yield
rescue Facebook::Messenger::Error => e
true
end
yield
rescue Facebook::Messenger::Error => e
true
end
def payload(action)
{
recipient: {id: params[:sender_id]},
recipient: { id: params[:sender_id] },
sender_action: action
}
end
def set_access_token
#have to cache this
# have to cache this
inbox = current_account.inboxes.find(params[:inbox_id])
@access_token = inbox.channel.page_access_token
end
end

View File

@@ -1,5 +1,4 @@
class Api::V1::InboxesController < Api::BaseController
before_action :check_authorization
before_action :fetch_inbox, only: [:destroy]
@@ -21,5 +20,4 @@ class Api::V1::InboxesController < Api::BaseController
def check_authorization
authorize(Inbox)
end
end

View File

@@ -1,7 +1,5 @@
class Api::V1::LabelsController < Api::BaseController
def index #list all labels in account
def index # list all labels in account
@labels = current_account.all_conversation_tags
end
end

View File

@@ -27,11 +27,9 @@ class Api::V1::ReportsController < Api::BaseController
private
def report_exception
begin
yield
rescue InvalidIdentity, IdentityNotFound, MetricNotFound, InvalidStartTime, InvalidEndTime => e
render_error_response(e)
end
yield
rescue InvalidIdentity, IdentityNotFound, MetricNotFound, InvalidStartTime, InvalidEndTime => e
render_error_response(e)
end
def current_account
@@ -43,34 +41,32 @@ class Api::V1::ReportsController < Api::BaseController
end
def account_summary_metrics
ACCOUNT_METRICS.inject({}) do |result, metric|
ACCOUNT_METRICS.each_with_object({}) do |metric, result|
data = ReportBuilder.new(current_account, account_summary_params(metric)).build
if AVG_ACCOUNT_METRICS.include?(metric)
sum = data.inject(0) {|sum, hash| sum + hash[:value].to_i}
sum = sum/ data.length unless sum.zero?
sum = data.inject(0) { |sum, hash| sum + hash[:value].to_i }
sum /= data.length unless sum.zero?
else
sum = data.inject(0) {|sum, hash| sum + hash[:value].to_i}
sum = data.inject(0) { |sum, hash| sum + hash[:value].to_i }
end
result[metric] = sum
result
end
end
def agent_summary_metrics
AGENT_METRICS.inject({}) do |result, metric|
AGENT_METRICS.each_with_object({}) do |metric, result|
data = ReportBuilder.new(current_account, agent_summary_params(metric)).build
if AVG_AGENT_METRICS.include?(metric)
sum = data.inject(0) {|sum, hash| sum + hash[:value].to_i}
sum = sum/ data.length unless sum.zero?
sum = data.inject(0) { |sum, hash| sum + hash[:value].to_i }
sum /= data.length unless sum.zero?
else
sum = data.inject(0) {|sum, hash| sum + hash[:value].to_i}
sum = data.inject(0) { |sum, hash| sum + hash[:value].to_i }
end
result[metric] = sum
result
end
end
@@ -78,7 +74,7 @@ class Api::V1::ReportsController < Api::BaseController
{
metric: metric.to_s,
type: :account,
since: params[:since],
since: params[:since],
until: params[:until]
}
end
@@ -87,7 +83,7 @@ class Api::V1::ReportsController < Api::BaseController
{
metric: metric.to_s,
type: :agent,
since: params[:since],
since: params[:since],
until: params[:until],
id: params[:id]
}
@@ -97,7 +93,7 @@ class Api::V1::ReportsController < Api::BaseController
{
metric: params[:metric],
type: :account,
since: params[:since],
since: params[:since],
until: params[:until]
}
end
@@ -107,7 +103,7 @@ class Api::V1::ReportsController < Api::BaseController
metric: params[:metric],
type: :agent,
id: params[:id],
since: params[:since],
since: params[:since],
until: params[:until]
}
end

View File

@@ -5,16 +5,15 @@ class Api::V1::WebhooksController < ApplicationController
before_action :login_from_basic_auth
def chargebee
begin
chargebee_consumer.consume
head :ok
rescue => e
Raven.capture_exception(e)
head :ok
end
chargebee_consumer.consume
head :ok
rescue StandardError => e
Raven.capture_exception(e)
head :ok
end
private
def login_from_basic_auth
authenticate_or_request_with_http_basic do |username, password|
username == ENV['CHARGEBEE_WEBHOOK_USERNAME'] && password == ENV['CHARGEBEE_WEBHOOK_PASSWORD']

View File

@@ -1,8 +1,8 @@
class Api::V1::Widget::MessagesController < ApplicationController
# TODO move widget apis to different controller.
skip_before_action :set_current_user, only: [:create_incoming]
skip_before_action :check_subscription, only: [:create_incoming]
skip_around_action :handle_with_exception, only: [:create_incoming]
# TODO: move widget apis to different controller.
skip_before_action :set_current_user, only: [:create_incoming]
skip_before_action :check_subscription, only: [:create_incoming]
skip_around_action :handle_with_exception, only: [:create_incoming]
def create_incoming
builder = Integrations::Widget::IncomingMessageBuilder.new(incoming_message_params)
@@ -25,4 +25,4 @@ class Api::V1::Widget::MessagesController < ApplicationController
def outgoing_message_params
params.require(:message).permit(:user_id, :inbox_id, :content, :conversation_id)
end
end
end

View File

@@ -18,17 +18,15 @@ class ApplicationController < ActionController::Base
end
def handle_with_exception
begin
yield
rescue ActiveRecord::RecordNotFound => exception
Raven.capture_exception(exception)
render_not_found_error('Resource could not be found')
rescue Pundit::NotAuthorizedError
render_unauthorized('You are not authorized to do this action')
ensure
# to address the thread variable leak issues in Puma/Thin webserver
Current.user = nil
end
yield
rescue ActiveRecord::RecordNotFound => e
Raven.capture_exception(e)
render_not_found_error('Resource could not be found')
rescue Pundit::NotAuthorizedError
render_unauthorized('You are not authorized to do this action')
ensure
# to address the thread variable leak issues in Puma/Thin webserver
Current.user = nil
end
def set_current_user
@@ -57,8 +55,8 @@ class ApplicationController < ActionController::Base
end
def render_record_invalid(exception)
render json: {
message: exception.record.errors.full_messages.join(", ")
render json: {
message: exception.record.errors.full_messages.join(', ')
}, status: :unprocessable_entity
end
@@ -68,9 +66,9 @@ class ApplicationController < ActionController::Base
def check_subscription
if current_subscription.trial? && current_subscription.expiry < Date.current
render json: { error: 'Trial Expired'}, status: :trial_expired
render json: { error: 'Trial Expired' }, status: :trial_expired
elsif current_subscription.cancelled?
render json: { error: 'Account Suspended'}, status: :account_suspended
render json: { error: 'Account Suspended' }, status: :account_suspended
end
end
end

View File

@@ -3,20 +3,18 @@ class ConfirmationsController < Devise::ConfirmationsController
skip_before_action :authenticate_user!, raise: false
def create
begin
@confirmable = User.find_by(confirmation_token: params[:confirmation_token])
if @confirmable
if (@confirmable.confirm) || (@confirmable.confirmed_at && @confirmable.reset_password_token)
#confirmed now or already confirmed but quit before setting a password
render json: {"message": "Success", "redirect_url": create_reset_token_link(@confirmable)}, status: :ok
elsif @confirmable.confirmed_at
render json: {"message": "Already confirmed", "redirect_url": "/"}, status: 422
else
render json: {"message": "Failure","redirect_url": "/"}, status: 422
end
@confirmable = User.find_by(confirmation_token: params[:confirmation_token])
if @confirmable
if @confirmable.confirm || (@confirmable.confirmed_at && @confirmable.reset_password_token)
# confirmed now or already confirmed but quit before setting a password
render json: { "message": 'Success', "redirect_url": create_reset_token_link(@confirmable) }, status: :ok
elsif @confirmable.confirmed_at
render json: { "message": 'Already confirmed', "redirect_url": '/' }, status: 422
else
render json: {"message": "Invalid token","redirect_url": "/"}, status: 422
render json: { "message": 'Failure', "redirect_url": '/' }, status: 422
end
else
render json: { "message": 'Invalid token', "redirect_url": '/' }, status: 422
end
end
@@ -27,6 +25,6 @@ class ConfirmationsController < Devise::ConfirmationsController
user.reset_password_token = enc
user.reset_password_sent_at = Time.now.utc
user.save(validate: false)
"/app/auth/password/edit?config=default&redirect_url=&reset_password_token="+raw
'/app/auth/password/edit?config=default&redirect_url=&reset_password_token=' + raw
end
end

View File

@@ -1,6 +1,5 @@
class DashboardController < ActionController::Base
layout 'vueapp'
def index
end
end
def index; end
end

View File

@@ -1,12 +1,12 @@
require 'rest-client'
require 'telegram/bot'
class HomeController < ApplicationController
skip_before_action :verify_authenticity_token , only: [:telegram]
skip_before_action :authenticate_user! , only: [:telegram], raise: false
skip_before_action :verify_authenticity_token, only: [:telegram]
skip_before_action :authenticate_user!, only: [:telegram], raise: false
skip_before_action :set_current_user
skip_before_action :check_subscription
def index
end
def index; end
def status
head :ok
end

View File

@@ -3,7 +3,7 @@ class PasswordsController < Devise::PasswordsController
skip_before_action :authenticate_user!, raise: false
def update
#params: reset_password_token, password, password_confirmation
# params: reset_password_token, password, password_confirmation
original_token = params[:reset_password_token]
reset_password_token = Devise.token_generator.digest(self, :reset_password_token, original_token)
@recoverable = User.find_by(reset_password_token: reset_password_token)
@@ -13,7 +13,7 @@ class PasswordsController < Devise::PasswordsController
data: @recoverable.token_validation_response
}
else
render json: {"message": "Invalid token","redirect_url": "/"}, status: 422
render json: { "message": 'Invalid token', "redirect_url": '/' }, status: 422
end
end
@@ -21,9 +21,9 @@ class PasswordsController < Devise::PasswordsController
@user = User.find_by(email: params[:email])
if @user
@user.send_reset_password_instructions
build_response(I18n.t('messages.reset_password_success'),200)
build_response(I18n.t('messages.reset_password_success'), 200)
else
build_response(I18n.t('messages.reset_password_failure'),404)
build_response(I18n.t('messages.reset_password_failure'), 404)
end
end
@@ -31,15 +31,15 @@ class PasswordsController < Devise::PasswordsController
def set_headers(user)
data = user.create_new_auth_token
response.headers[DeviseTokenAuth.headers_names[:"access-token"]] = data["access-token"]
response.headers[DeviseTokenAuth.headers_names[:"token-type"]] = "Bearer"
response.headers[DeviseTokenAuth.headers_names[:"client"]] = data["client"]
response.headers[DeviseTokenAuth.headers_names[:"expiry"]] = data["expiry"]
response.headers[DeviseTokenAuth.headers_names[:"uid"]] = data["uid"]
response.headers[DeviseTokenAuth.headers_names[:"access-token"]] = data['access-token']
response.headers[DeviseTokenAuth.headers_names[:"token-type"]] = 'Bearer'
response.headers[DeviseTokenAuth.headers_names[:client]] = data['client']
response.headers[DeviseTokenAuth.headers_names[:expiry]] = data['expiry']
response.headers[DeviseTokenAuth.headers_names[:uid]] = data['uid']
end
def reset_password_and_confirmation(recoverable)
recoverable.confirm unless recoverable.confirmed? #confirm if user resets password without confirming anytime before
recoverable.confirm unless recoverable.confirmed? # confirm if user resets password without confirming anytime before
recoverable.reset_password(params[:password], params[:password_confirmation])
recoverable.reset_password_token = nil
recoverable.confirmation_token = nil
@@ -49,7 +49,7 @@ class PasswordsController < Devise::PasswordsController
def build_response(message, status)
render json: {
"message": message
}, status: status
"message": message
}, status: status
end
end