Fix easy rubocop issues in spree/api/base_controller

This commit is contained in:
luisramos0
2019-07-18 17:41:12 +01:00
parent 9d40ee49e6
commit a941280982

View File

@@ -28,14 +28,14 @@ module Spree
def set_jsonp_format
if params[:callback] && request.get?
self.response_body = "#{params[:callback]}(#{self.response_body})"
self.response_body = "#{params[:callback]}(#{response_body})"
headers["Content-Type"] = 'application/javascript'
end
end
def map_nested_attributes_keys(klass, attributes)
nested_keys = klass.nested_attributes_options.keys
attributes.inject({}) do |h, (k,v)|
attributes.inject({}) do |h, (k, v)|
key = nested_keys.include?(k.to_sym) ? "#{k}_attributes" : k
h[key] = v
h
@@ -46,43 +46,43 @@ module Spree
def set_content_type
content_type = case params[:format]
when "json"
"application/json"
when "xml"
"text/xml"
end
when "json"
"application/json"
when "xml"
"text/xml"
end
headers["Content-Type"] = content_type
end
def check_for_user_or_api_key
# User is already authenticated with Spree, make request this way instead.
return true if @current_api_user = try_spree_current_user || !Spree::Api::Config[:requires_authentication]
return true if @current_api_user = try_spree_current_user ||
!Spree::Api::Config[:requires_authentication]
if api_key.blank?
render "spree/api/errors/must_specify_api_key", :status => 401 and return
end
return if api_key.present?
render "spree/api/errors/must_specify_api_key", status: :unauthorized && return
end
def authenticate_user
unless @current_api_user
if requires_authentication? || api_key.present?
unless @current_api_user = Spree.user_class.find_by_spree_api_key(api_key.to_s)
render "spree/api/errors/invalid_api_key", :status => 401 and return
end
else
# An anonymous user
@current_api_user = Spree.user_class.new
return if @current_api_user
if requires_authentication? || api_key.present?
unless @current_api_user = Spree.user_class.find_by_spree_api_key(api_key.to_s)
render "spree/api/errors/invalid_api_key", status: :unauthorized && return
end
else
# An anonymous user
@current_api_user = Spree.user_class.new
end
end
def unauthorized
render "spree/api/errors/unauthorized", :status => 401 and return
render "spree/api/errors/unauthorized", status: :unauthorized && return
end
def error_during_processing(exception)
render :text => { :exception => exception.message }.to_json,
:status => 422 and return
render text: { exception: exception.message }.to_json,
status: :unprocessable_entity && return
end
def requires_authentication?
@@ -90,7 +90,7 @@ module Spree
end
def not_found
render "spree/api/errors/not_found", :status => 404 and return
render "spree/api/errors/not_found", status: :not_foun && return
end
def current_ability
@@ -99,7 +99,7 @@ module Spree
def invalid_resource!(resource)
@resource = resource
render "spree/api/errors/invalid_resource", :status => 422
render "spree/api/errors/invalid_resource", status: :unprocessable_entity
end
def api_key
@@ -108,11 +108,9 @@ module Spree
helper_method :api_key
def find_product(id)
begin
product_scope.find_by_permalink!(id.to_s)
rescue ActiveRecord::RecordNotFound
product_scope.find(id)
end
product_scope.find_by_permalink!(id.to_s)
rescue ActiveRecord::RecordNotFound
product_scope.find(id)
end
def product_scope
@@ -127,7 +125,6 @@ module Spree
scope.includes(:master)
end
end
end
end