summaryrefslogblamecommitdiffstats
path: root/lib/contact.rb
blob: 5cf42e6f1240cc0d7b630f7af1e662934127daad (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16















                                                                             
             
                                                              
 


                              


                                    


                              
     
 



                                       
            
                            


                       
               
                
                                       
                                                

       

                                                 


                                       
                                         

       

                                                         

       

                                                         

       






                                       
     





                                                                           
                                           
     
   
# Band registration form for Norsk Urskog Metal Sampler
# Copyright (C) 2015-2018  Harald Eilersen <haraldei@anduin.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

class Contact
  attr_reader :name, :street, :postcode, :city, :phone, :email

  def initialize(params = nil)
    if params
      @name = params['name']
      @street = params['street']
      @postcode = params['postcode']
      @city = params['city']
      @phone = params['phone']
      @email = params['email']
    end
  end

  def addr
    "#{@street}, #{@postcode} #{@city}"
  end

  def valid?
    validate! unless @errors
    @errors.length == 0
  end

  def validate!
    @errors = []
    if @name.nil? || @name.strip.empty?
      @errors << "Du må oppgi en kontaktperson"
    end

    unless valid_postcode
      @errors << "Du må oppgi et gyldig postnr."
    end

    if @city.nil? || @city.strip.empty?
      @errors << "Du må oppgi poststed."
    end

    unless valid_phone?
      @errors << "Telefonnummer mangler eller er ugyldig"
    end

    unless valid_email?
      @errors << "E-postadresse mangler eller er ugyldig"
    end

    @errors
  end

  private

  def valid_postcode
    @postcode && @postcode =~ /^\d{4}$/
  end

  def valid_email?
    @email && @email =~ /^[A-Z0-9._%+-]+@(?:[A-Z0-9-]{1,63}\.)+[A-Z]{2,}$/i
  end

  def valid_phone?
    @phone && @phone =~ /^\+*[0-9\- ]{8,}$/
  end
end