Rubocop autocorrect

This commit is contained in:
Martin Lensment 2014-08-25 14:14:25 +03:00
parent f8c48a7456
commit 6c5c0b38c8
59 changed files with 533 additions and 546 deletions

View file

@ -12,7 +12,7 @@ module Epp::Common
end
def proxy
@svTRID = "ccReg-#{'%010d' % rand(10 ** 10)}"
@svTRID = "ccReg-#{'%010d' % rand(10**10)}"
send(params[:command])
end
@ -36,7 +36,7 @@ module Epp::Common
@current_epp_user ||= EppUser.find(epp_session[:epp_user_id]) if epp_session[:epp_user_id]
end
def handle_errors(obj=nil)
def handle_errors(obj = nil)
@errors ||= []
if obj
obj.construct_epp_errors
@ -44,7 +44,7 @@ module Epp::Common
end
# for debugging
@errors << {code: '1', msg: 'handle_errors was executed when there were actually no errors'} if @errors.blank?
@errors << { code: '1', msg: 'handle_errors was executed when there were actually no errors' } if @errors.blank?
render '/epp/error'
end
@ -55,13 +55,13 @@ module Epp::Common
def xml_attrs_present?(ph, attributes)
attributes.each do |x|
epp_errors << {code: '2003', msg: I18n.t('errors.messages.required_parameter_missing', key: x.last)} unless has_attribute(ph, x)
epp_errors << { code: '2003', msg: I18n.t('errors.messages.required_parameter_missing', key: x.last) } unless has_attribute(ph, x)
end
epp_errors.empty?
end
def has_attribute(ph, path)
path.inject(ph) do |location, key|
path.reduce(ph) do |location, key|
location.respond_to?(:keys) ? location[key] : nil
end
end

View file

@ -1,8 +1,8 @@
module Shared::UserStamper
extend ActiveSupport::Concern
def stamp obj
return false if obj.nil? || !obj.has_attribute?( :created_by_id && :updated_by_id )
def stamp(obj)
return false if obj.nil? || !obj.has_attribute?(:created_by_id && :updated_by_id)
if obj.new_record?
obj.created_by_id = current_epp_user.id
@ -10,6 +10,6 @@ module Shared::UserStamper
obj.updated_by_id = current_epp_user.id
end
return true
end
true
end
end

View file

@ -5,6 +5,7 @@ class Epp::CommandsController < ApplicationController
include Shared::UserStamper
private
def create
send("create_#{OBJECT_TYPES[params_hash['epp']['xmlns:ns2']]}")
end

View file

@ -2,7 +2,7 @@ class Epp::ErrorsController < ApplicationController
include Epp::Common
def error
epp_errors << {code: params[:code], msg: params[:msg]}
epp_errors << { code: params[:code], msg: params[:msg] }
render '/epp/error'
end
end

View file

@ -2,6 +2,7 @@ class Epp::SessionsController < ApplicationController
include Epp::Common
private
def hello
render 'greeting'
end

View file

@ -16,11 +16,12 @@ class SettingGroupsController < ApplicationController
end
private
def set_setting_group
@setting_group = SettingGroup.find(params[:id])
end
def setting_group_params
params.require(:setting_group).permit(settings_attributes: [ :value, :id ])
params.require(:setting_group).permit(settings_attributes: [:value, :id])
end
end

View file

@ -1,6 +1,6 @@
module Epp::ContactsHelper
def create_contact
@contact = Contact.new( contact_and_address_attributes )
@contact = Contact.new(contact_and_address_attributes)
render '/epp/contacts/create' and return if stamp(@contact) && @contact.save
handle_errors(@contact)
@ -29,7 +29,7 @@ module Epp::ContactsHelper
def check_contact
ph = params_hash['epp']['command']['check']['check']
@contacts = Contact.check_availability( ph[:id] )
@contacts = Contact.check_availability(ph[:id])
render '/epp/contacts/check'
end
@ -41,40 +41,41 @@ module Epp::ContactsHelper
end
## HELPER METHODS
private
## CREATE
def validate_contact_create_request
@ph = params_hash['epp']['command']['create']['create']
xml_attrs_present?(@ph, [['id'],
['authInfo', 'pw'],
['postalInfo', 'name'],
['postalInfo', 'addr', 'city'],
['postalInfo', 'addr', 'cc']])
xml_attrs_present?(@ph, [['id'],
%w(authInfo pw),
%w(postalInfo name),
%w(postalInfo addr city),
%w(postalInfo addr cc)])
end
## UPDATE
def validate_contact_update_request
@ph = params_hash['epp']['command']['update']['update']
xml_attrs_present?(@ph, [['id'] ])
xml_attrs_present?(@ph, [['id']])
end
## DELETE
def validate_contact_delete_request
@ph = params_hash['epp']['command']['delete']['delete']
xml_attrs_present?(@ph, [ ['id'] ] )
xml_attrs_present?(@ph, [['id']])
end
## CHECK
def validate_contact_check_request
@ph = params_hash['epp']['command']['check']['check']
xml_attrs_present?(@ph, [ ['id'] ])
xml_attrs_present?(@ph, [['id']])
end
## INFO
def validate_contact_info_request
@ph = params_hash['epp']['command']['info']['info']
xml_attrs_present?(@ph, [ ['id'] ])
xml_attrs_present?(@ph, [['id']])
end
## SHARED
@ -91,22 +92,22 @@ module Epp::ContactsHelper
pw = @ph.try(:[], :authInfo).try(:[], :pw) || @ph.try(:[], :chg).try(:[], :authInfo).try(:[], :pw) || []
id = @ph[:id]
return true if ( !find_contact.nil? && find_contact.auth_info_matches(pw) )
return true if !find_contact.nil? && find_contact.auth_info_matches(pw)
epp_errors << { code: '2201', msg: t('errors.messages.epp_authorization_error'), value: { obj: 'pw', val: pw } }
return false
false
end
def contact_and_address_attributes( type=:create )
def contact_and_address_attributes(type = :create)
case type
when :update
contact_hash = Contact.extract_attributes(@ph[:chg], type)
contact_hash[:address_attributes] =
Address.extract_attributes(( @ph.try(:[], :chg).try(:[], :postalInfo).try(:[], :addr) || [] ), type)
contact_hash[:address_attributes] =
Address.extract_attributes(( @ph.try(:[], :chg).try(:[], :postalInfo).try(:[], :addr) || []), type)
else
contact_hash = Contact.extract_attributes(@ph, type)
contact_hash[:address_attributes] =
Address.extract_attributes(( @ph.try(:[], :postalInfo).try(:[], :addr) || [] ), type)
contact_hash[:address_attributes] =
Address.extract_attributes(( @ph.try(:[], :postalInfo).try(:[], :addr) || []), type)
end
contact_hash[:ident_type] = ident_type unless ident_type.nil?
contact_hash
@ -118,6 +119,6 @@ module Epp::ContactsHelper
return nil unless result
Contact::IDENT_TYPES.any? { |type| return type if result.include?(type) }
return nil
nil
end
end

View file

@ -48,6 +48,7 @@ module Epp::DomainsHelper
end
### HELPER METHODS ###
private
## CREATE
@ -95,7 +96,7 @@ module Epp::DomainsHelper
def find_domain
domain = Domain.find_by(name: @ph[:name])
unless domain
epp_errors << {code: '2303', msg: I18n.t('errors.messages.epp_domain_not_found'), value: {obj: 'name', val: @ph[:name]}}
epp_errors << { code: '2303', msg: I18n.t('errors.messages.epp_domain_not_found'), value: { obj: 'name', val: @ph[:name] } }
end
domain
end

View file

@ -3,7 +3,7 @@ class Address < ActiveRecord::Base
belongs_to :country
class << self
def extract_attributes ah, type=:create
def extract_attributes(ah, _type = :create)
address_hash = {}
address_hash = ({
country_id: Country.find_by(iso: ah[:cc]).try(:id),
@ -14,7 +14,7 @@ class Address < ActiveRecord::Base
zip: ah[:pc]
}) if ah.is_a?(Hash)
address_hash.delete_if { |k, v| v.nil? }
address_hash.delete_if { |_k, v| v.nil? }
end
end
end

View file

@ -22,21 +22,21 @@ module EppErrors
values.each do |err|
if err.is_a?(Hash)
next unless code = find_epp_code(err[:msg])
err_msg = {code: code, msg: err[:msg]}
err_msg[:value] = {val: err[:val], obj: err[:obj]} if err[:val]
err_msg = { code: code, msg: err[:msg] }
err_msg[:value] = { val: err[:val], obj: err[:obj] } if err[:val]
epp_errors << err_msg
else
next unless code = find_epp_code(err)
err = {code: code, msg: err}
err = { code: code, msg: err }
# If we have setting relation, then still add the value to the error message
# If this sort of exception happens again, some other logic has to be implemented
if self.class.reflect_on_association(key) && key == :setting
err[:value] = {val: send(key).value, obj: self.class::EPP_ATTR_MAP[key]}
err[:value] = { val: send(key).value, obj: self.class::EPP_ATTR_MAP[key] }
#if the key represents other relations, skip value
# if the key represents other relations, skip value
elsif !self.class.reflect_on_association(key)
err[:value] = {val: send(key), obj: self.class::EPP_ATTR_MAP[key]}
err[:value] = { val: send(key), obj: self.class::EPP_ATTR_MAP[key] }
end
epp_errors << err

View file

@ -1,6 +1,6 @@
class Contact < ActiveRecord::Base
#TODO Foreign contact will get email with activation link/username/temp password
#TODO Phone number validation, in first phase very minimam in order to support current registries
# TODO Foreign contact will get email with activation link/username/temp password
# TODO Phone number validation, in first phase very minimam in order to support current registries
include EppErrors
@ -19,23 +19,23 @@ class Contact < ActiveRecord::Base
validate :ident_must_be_valid
validates :phone, format: /\+[0-9]{1,3}\.[0-9]{1,14}?/ #/\+\d{3}\.\d+/
validates :phone, format: /\+[0-9]{1,3}\.[0-9]{1,14}?/ # /\+\d{3}\.\d+/
validates :email, format: /@/
validates_uniqueness_of :code, message: :epp_id_taken
IDENT_TYPE_ICO = 'ico'
IDENT_TYPES = [
IDENT_TYPE_ICO, #Company registry code (or similar)
"op", #Estonian ID
"passport", #Passport number
"birthday" #Birthday date
IDENT_TYPE_ICO, # Company registry code (or similar)
'op', # Estonian ID
'passport', # Passport number
'birthday' # Birthday date
]
def ident_must_be_valid
#TODO Ident can also be passport number or company registry code.
#so have to make changes to validations (and doc/schema) accordingly
return true unless ident.present? && ident_type.present? && ident_type == "op"
# TODO Ident can also be passport number or company registry code.
# so have to make changes to validations (and doc/schema) accordingly
return true unless ident.present? && ident_type.present? && ident_type == 'op'
code = Isikukood.new(ident)
errors.add(:ident, 'bad format') unless code.valid?
end
@ -56,22 +56,22 @@ class Contact < ActiveRecord::Base
updated_by ? updated_by.username : nil
end
def auth_info_matches pw
def auth_info_matches(pw)
return true if auth_info == pw
return false
false
end
#Find a way to use self.domains with contact
# Find a way to use self.domains with contact
def domains_owned
Domain.find_by(owner_contact_id: id)
end
def relations_with_domain?
return true if domain_contacts.present? || domains_owned.present?
return false
false
end
#should use only in transaction
# should use only in transaction
def destroy_and_clean
clean_up_address
@ -84,15 +84,15 @@ class Contact < ActiveRecord::Base
def epp_code_map
{
'2302' => [ #Object exists
'2302' => [ # Object exists
[:code, :epp_id_taken]
],
'2303' => #Object does not exist
'2303' => # Object does not exist
[:not_found, :epp_obj_does_not_exist],
'2305' => [ #Association exists
'2305' => [ # Association exists
[:domains, :exist]
],
'2005' => [ #Value syntax error
],
'2005' => [ # Value syntax error
[:phone, :invalid],
[:email, :invalid]
]
@ -100,10 +100,7 @@ class Contact < ActiveRecord::Base
end
class << self
def extract_attributes ph, type=:create
def extract_attributes(ph, type = :create)
contact_hash = {
phone: ph[:voice],
ident: ph[:ident],
@ -117,7 +114,7 @@ class Contact < ActiveRecord::Base
contact_hash[:code] = ph[:id] if type == :create
contact_hash.delete_if { |k, v| v.nil? }
contact_hash.delete_if { |_k, v| v.nil? }
end
def check_availability(codes)
@ -126,9 +123,9 @@ class Contact < ActiveRecord::Base
res = []
codes.each do |x|
if Contact.find_by(code: x)
res << {code: x, avail: 0, reason: 'in use'}
res << { code: x, avail: 0, reason: 'in use' }
else
res << {code: x, avail: 1}
res << { code: x, avail: 1 }
end
end
@ -141,6 +138,4 @@ class Contact < ActiveRecord::Base
def clean_up_address
address.destroy if address
end
end

View file

@ -1,3 +1,2 @@
class Country < ActiveRecord::Base
end

View file

@ -1,6 +1,6 @@
class Domain < ActiveRecord::Base
#TODO whois requests ip whitelist for full info for own domains and partial info for other domains
#TODO most inputs should be trimmed before validatation, probably some global logic?
# TODO whois requests ip whitelist for full info for own domains and partial info for other domains
# TODO most inputs should be trimmed before validatation, probably some global logic?
include EppErrors
@ -15,18 +15,18 @@ class Domain < ActiveRecord::Base
has_many :domain_contacts
has_many :tech_contacts, -> {
where(domain_contacts: {contact_type: DomainContact::TECH})
}, through: :domain_contacts, source: :contact
has_many :tech_contacts, -> do
where(domain_contacts: { contact_type: DomainContact::TECH })
end, through: :domain_contacts, source: :contact
has_many :admin_contacts, -> {
where(domain_contacts: {contact_type: DomainContact::ADMIN})
}, through: :domain_contacts, source: :contact
has_many :admin_contacts, -> do
where(domain_contacts: { contact_type: DomainContact::ADMIN })
end, through: :domain_contacts, source: :contact
has_and_belongs_to_many :nameservers
has_many :domain_statuses, -> {
joins(:setting).where(settings: {setting_group_id: SettingGroup.domain_statuses.id})
joins(:setting).where(settings: { setting_group_id: SettingGroup.domain_statuses.id })
}
delegate :code, to: :owner_contact, prefix: true
@ -123,14 +123,14 @@ class Domain < ActiveRecord::Base
def attach_nameservers(ns_list)
ns_list.each do |ns_attrs|
self.nameservers.build(ns_attrs)
nameservers.build(ns_attrs)
end
end
def attach_statuses(status_list)
status_list.each do |x|
setting = SettingGroup.domain_statuses.settings.find_by(value: x[:value])
self.domain_statuses.build(
domain_statuses.build(
setting: setting,
description: x[:description]
)
@ -139,9 +139,9 @@ class Domain < ActiveRecord::Base
def detach_contacts(contact_list)
to_delete = []
contact_list.each do |k, v|
contact_list.each do |_k, v|
v.each do |x|
contact = domain_contacts.joins(:contact).where(contacts: {code: x[:contact]})
contact = domain_contacts.joins(:contact).where(contacts: { code: x[:contact] })
if contact.blank?
errors.add(:domain_contacts, {
obj: 'contact',
@ -154,13 +154,13 @@ class Domain < ActiveRecord::Base
end
end
self.domain_contacts.delete(to_delete)
domain_contacts.delete(to_delete)
end
def detach_nameservers(ns_list)
to_delete = []
ns_list.each do |ns_attrs|
nameserver = self.nameservers.where(ns_attrs)
nameserver = nameservers.where(ns_attrs)
if nameserver.blank?
errors.add(:nameservers, {
obj: 'hostObj',
@ -172,13 +172,13 @@ class Domain < ActiveRecord::Base
end
end
self.nameservers.delete(to_delete)
nameservers.delete(to_delete)
end
def detach_statuses(status_list)
to_delete = []
status_list.each do |x|
status = domain_statuses.joins(:setting).where(settings: {value: x[:value]})
status = domain_statuses.joins(:setting).where(settings: { value: x[:value] })
if status.blank?
errors.add(:domain_statuses, {
obj: 'status',
@ -190,19 +190,19 @@ class Domain < ActiveRecord::Base
end
end
self.domain_statuses.delete(to_delete)
domain_statuses.delete(to_delete)
end
### RENEW ###
def renew(cur_exp_date, period, unit='y')
def renew(cur_exp_date, period, unit = 'y')
# TODO Check how much time before domain exp date can it be renewed
validate_exp_dates(cur_exp_date)
return false if errors.any?
p = self.class.convert_period_to_time(period, unit)
self.valid_to = self.valid_to + p
self.valid_to = valid_to + p
self.period = period
self.period_unit = unit
save
@ -215,7 +215,7 @@ class Domain < ActiveRecord::Base
min, max = sg.setting(:ns_min_count).value.to_i, sg.setting(:ns_max_count).value.to_i
unless nameservers.length.between?(min, max)
errors.add(:nameservers, :out_of_range, {min: min, max: max})
errors.add(:nameservers, :out_of_range, { min: min, max: max })
end
end
@ -226,11 +226,11 @@ class Domain < ActiveRecord::Base
def validate_period
return unless period.present?
if period_unit == 'd'
valid_values = ['365', '366', '710', '712', '1065', '1068']
valid_values = %w(365 366 710 712 1065 1068)
elsif period_unit == 'm'
valid_values = ['12', '24', '36']
valid_values = %w(12 24 36)
else
valid_values = ['1', '2', '3']
valid_values = %w(1 2 3)
end
errors.add(:period, :out_of_range) unless valid_values.include?(period.to_s)
@ -258,7 +258,7 @@ class Domain < ActiveRecord::Base
[:valid_to, :epp_exp_dates_do_not_match]
],
'2004' => [ # Parameter value range error
[:nameservers, :out_of_range, {min: domain_validation_sg.setting(:ns_min_count).value, max: domain_validation_sg.setting(:ns_max_count).value}],
[:nameservers, :out_of_range, { min: domain_validation_sg.setting(:ns_min_count).value, max: domain_validation_sg.setting(:ns_max_count).value }],
[:period, :out_of_range]
],
'2303' => [ # Object does not exist
@ -277,7 +277,7 @@ class Domain < ActiveRecord::Base
# For domain transfer
def authenticate(pw)
errors.add(:auth_info, {msg: errors.generate_message(:auth_info, :wrong_pw)}) if pw != auth_info
errors.add(:auth_info, { msg: errors.generate_message(:auth_info, :wrong_pw) }) if pw != auth_info
errors.empty?
end
@ -351,20 +351,20 @@ class Domain < ActiveRecord::Base
res = []
domains.each do |x|
if !DomainNameValidator.validate_format(x)
res << {name: x, avail: 0, reason: 'invalid format'}
unless DomainNameValidator.validate_format(x)
res << { name: x, avail: 0, reason: 'invalid format' }
next
end
if !DomainNameValidator.validate_reservation(x)
res << {name: x, avail: 0, reason: I18n.t('errors.messages.epp_domain_reserved')}
unless DomainNameValidator.validate_reservation(x)
res << { name: x, avail: 0, reason: I18n.t('errors.messages.epp_domain_reserved') }
next
end
if Domain.find_by(name: x)
res << {name: x, avail: 0, reason: 'in use'}
res << { name: x, avail: 0, reason: 'in use' }
else
res << {name: x, avail: 1}
res << { name: x, avail: 1 }
end
end

View file

@ -20,12 +20,12 @@ class EppSession < ActiveRecord::Base
class << self
def marshal(data)
::Base64.encode64(Marshal.dump(data)) if data
::Base64.encode64(Marshal.dump(data)) if data
end
def unmarshal(data)
return data unless data.is_a? String
Marshal.load(::Base64.decode64(data)) if data
end
def unmarshal(data)
return data unless data.is_a? String
Marshal.load(::Base64.decode64(data)) if data
end
end
end

View file

@ -1,5 +1,5 @@
class EppUser < ActiveRecord::Base
#TODO should have max request limit per day
# TODO should have max request limit per day
belongs_to :registrar
has_many :contacts
end

View file

@ -1,3 +1,2 @@
class ReservedDomain < ActiveRecord::Base
end

View file

@ -1,7 +1,7 @@
class User < ActiveRecord::Base
#TODO Foreign user will get email with activation link,email,temp-password.
#After activisation, system should require to change temp password.
#TODO Estonian id validation
# TODO Foreign user will get email with activation link,email,temp-password.
# After activisation, system should require to change temp password.
# TODO Estonian id validation
belongs_to :role
end

View file

@ -1,5 +1,5 @@
class DomainNameValidator < ActiveModel::EachValidator
#TODO
# TODO
# validates lenght of 2-63
# validates/honours Estonian additional letters zäõüö
# honours punicode and all interfces honors utf8