Skip to content

Commit

Permalink
Merge pull request #192 from berkos/upgrade_Ruby_versions_on_travis
Browse files Browse the repository at this point in the history
Upgrade Ruby versions and Relax dependancies
  • Loading branch information
Joost Hietbrink committed Mar 18, 2019
2 parents 22d06c8 + 572a552 commit 12a6cb3
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 156 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
coverage/*
/.bundle
/vendor
Gemfile.lock
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ language: ruby
rvm:
- 2.3.7
- 2.4.4
- 2.5.1
- 2.5.5
- 2.6.2
script:
- bundle exec rspec spec
- bundle exec rubocop
Expand Down
3 changes: 0 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,3 @@ gem 'guard-rspec' # , '~> 4.2.0'
gem 'rake'
gem 'rspec' # , '~> 2.14.0'
gem 'rubocop'

# For testing
gem 'sqlite3'
143 changes: 0 additions & 143 deletions Gemfile.lock

This file was deleted.

19 changes: 12 additions & 7 deletions lib/phony_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,18 @@ def self.country_codes_hash
# http://www.redguava.com.au/2011/06/rails-convert-phone-numbers-to-international-format-for-sms/
def self.normalize_number(number, options = {}, current_instance = nil)
return if number.nil?

original_number = number
number = number.dup # Just to be sure, we don't want to change the original.
number, ext = extract_extension(number)
number.gsub!(/[^\(\)\d\+]/, '') # Strips weird stuff from the number
return if number.blank?

if _country_number = options[:country_number] || country_number_for(options[:country_code])
options[:add_plus] = true if options[:add_plus].nil?
# (Force) add country_number if missing
# NOTE: do we need to force adding country code? Otherwise we can share logic with next block
if !Phony.plausible?(number) || _country_number != country_code_from_number(number)
number = "#{_country_number}#{number}"
end
number = "#{_country_number}#{number}" if !Phony.plausible?(number) || _country_number != country_code_from_number(number)
elsif _default_country_number = extract_default_country_number(options, current_instance)
options[:add_plus] = true if options[:add_plus].nil?
number = normalize_number_default_country(number, _default_country_number)
Expand Down Expand Up @@ -101,12 +101,14 @@ def self.extract_default_country_number(options = {}, current_instance = nil)
# Should probably be named 'country_number_from_number'.
def self.country_code_from_number(number)
return nil unless Phony.plausible?(number)

Phony.split(Phony.normalize(number)).first
end

# Returns the country (eg. 'NL') for a number (eg. +31612341234).
def self.country_from_number(number)
return nil unless Phony.plausible?(number)

country_codes_hash.select { |_country, hash| hash['country_code'] == country_code_from_number(number) }.keys[0]
end

Expand All @@ -115,6 +117,7 @@ def self.country_from_number(number)
# It uses the 'cc' option for Phony. This was a required param before?
def self.plausible_number?(number, options = {})
return false if number.blank?

number = extract_extension(number).first
number = normalize_number(number, options)
country_number = options[:country_number] || country_number_for(options[:country_code]) ||
Expand All @@ -126,10 +129,11 @@ def self.plausible_number?(number, options = {})
false
end

COMMON_EXTENSIONS = /[ ]*(ext|ex|x|xt|#|:)+[^0-9]*\(?([-0-9]{1,})\)?#?$/i
COMMON_EXTENSIONS = /[ ]*(ext|ex|x|xt|#|:)+[^0-9]*\(?([-0-9]{1,})\)?#?$/i.freeze

def self.extract_extension(number_and_ext)
return [nil, nil] if number_and_ext.nil?

subbed = number_and_ext.sub(COMMON_EXTENSIONS, '')
[subbed, Regexp.last_match(2)]
end
Expand All @@ -156,9 +160,8 @@ def set_phony_normalized_numbers(current_instance, attributes, options = {})
attributes.each do |attribute|
attribute_name = options[:as] || attribute
raise("No attribute #{attribute_name} found on #{self.class.name} (PhonyRails)") unless self.class.attribute_method?(attribute_name)
if options[:normalize_when_valid]
cache_original_attribute(current_instance, attribute)
end

cache_original_attribute(current_instance, attribute) if options[:normalize_when_valid]
new_value = PhonyRails.normalize_number(send(attribute), options, current_instance)
current_instance.public_send("#{attribute_name}=", new_value) if new_value || attribute_name != attribute
end
Expand Down Expand Up @@ -221,10 +224,12 @@ def phony_normalized_method(*attributes)
main_options.assert_valid_keys :country_code, :default_country_code
attributes.each do |attribute|
raise(StandardError, "Instance method normalized_#{attribute} already exists on #{name} (PhonyRails)") if method_defined?(:"normalized_#{attribute}")

define_method :"normalized_#{attribute}" do |*args|
options = main_options.merge(args.first || {})
assign_values_for_phony_symbol_options(options)
raise(ArgumentError, "No attribute/method #{attribute} found on #{self.class.name} (PhonyRails)") unless respond_to?(attribute)

options[:country_code] ||= country_code if respond_to?(:country_code)
PhonyRails.normalize_number(send(attribute), options)
end
Expand Down
5 changes: 5 additions & 0 deletions lib/phony_rails/string_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class String
# "(0)30 1234 123".phony_normalized(country_code: 'NL') # => '301234123'
def phony_normalized(options = {})
raise ArgumentError, "Expected options to be a Hash, got #{options.inspect}" unless options.is_a?(Hash)

options = options.dup
PhonyRails.normalize_number(self, options)
end
Expand All @@ -24,21 +25,25 @@ def phony_normalized(options = {})
# "somestring".phone_formatted(raise: true)
def phony_formatted(options = {})
raise ArgumentError, "Expected options to be a Hash, got #{options.inspect}" unless options.is_a?(Hash)

options = options.dup
normalize_country_code = options.delete(:normalize)
s, ext = PhonyRails.extract_extension(self)
s = (normalize_country_code ? PhonyRails.normalize_number(s, default_country_code: normalize_country_code.to_s, add_plus: false) : s.gsub(/\D/, ''))
return if s.blank?
return if options[:strict] && !Phony.plausible?(s)

PhonyRails.format_extension(Phony.format(s, options.reverse_merge(format: :national)), ext)
rescue StandardError
raise if options[:raise]

s
end

# The bang method
def phony_formatted!(options = {})
raise ArgumentError, 'The :strict options is only supported in the phony_formatted (non bang) method.' if options[:strict]

replace(phony_formatted(options))
end
end
6 changes: 4 additions & 2 deletions phony_rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ Gem::Specification.new do |gem|
gem.require_paths = ['lib']
gem.version = PhonyRails::VERSION

gem.post_install_message = 'PhonyRails v0.10.0 changes the way numbers are stored!'
gem.post_install_message = "It now adds a '+' to the normalized number when it starts with a country number!"
gem.post_install_message = "PhonyRails v0.10.0 changes the way numbers are stored!\nIt now adds a ' + ' to the normalized number when it starts with a country number!"

gem.add_runtime_dependency 'activesupport', '>= 3.0'
gem.add_runtime_dependency 'phony', '> 2.15'
gem.add_development_dependency 'activerecord', '>= 3.0'
gem.add_development_dependency 'mongoid', '>= 3.0'

# For testing
gem.add_development_dependency 'sqlite3', '~> 1.3.6'
end

0 comments on commit 12a6cb3

Please sign in to comment.