Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for silent failures: added phone number formatting & validation #85

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions lib/plivo/resources/calls.rb
Original file line number Diff line number Diff line change
@@ -282,8 +282,8 @@ def create(from, to, answer_url, answer_method = 'POST', options = nil)
true, %w[GET POST])

params = {
from: from,
to: to.join('<'),
from: format_phone_number(from),
to: to.map{|x| format_phone_number(x)}.join('<'),
answer_url: answer_url,
answer_method: answer_method
}
@@ -397,7 +397,6 @@ def each
# - To filter out those numbers that contain a particular number sequence, use to_number={ sequence}
# - To filter out a number that matches an exact number, use to_number={ exact_number}
def list_live(options = nil)

if options.nil?
options = {}
else
4 changes: 2 additions & 2 deletions lib/plivo/resources/messages.rb
Original file line number Diff line number Diff line change
@@ -83,8 +83,8 @@ def create(src, dst, text, options = nil, powerpack_uuid = nil)
end

params = {
src: src,
dst: dst.join('<'),
src: format_phone_number(src),
dst: dst.map{|x| format_phone_number(x)}.join('<'),
text: text,
powerpack_uuid: powerpack_uuid
}
23 changes: 23 additions & 0 deletions lib/plivo/utils.rb
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ module Plivo
module Utils
module_function

VANITY_4_LETTERS_KEYS_REGEX = /[SVYZ]/.freeze
TYPE_WHITELIST = [Integer]
TYPE_WHITELIST.push(Fixnum, Bignum) unless 1.class == Integer

@@ -107,5 +108,27 @@ def valid_signature?(uri, nonce, signature, auth_token)
sha256_digest = OpenSSL::Digest.new('sha256')
Base64.encode64(OpenSSL::HMAC.digest(sha256_digest, auth_token, data_to_sign)).strip() == signature
end

def format_phone_number(number)
return if number.nil?
formatted_num = vanity_conversion(number)
formatted_num = formatted_num.gsub(/[^0-9]/, '')
if formatted_num.length >=7 && formatted_num.length <= 14
return '+' + formatted_num.to_s
else
raise_invalid_request("Please enter a valid phone number")
end
end

def vanity_conversion(phone)
return phone.gsub(Regexp.new('[a-zA-Z]')) do |c|
c.upcase!
# subtract "A"
n = (c.ord - 65) / 3
# account for #7 & #9 which have 4 chars
n -= 1 if c.match(VANITY_4_LETTERS_KEYS_REGEX)
(n + 2).to_s
end
end
end
end
8 changes: 4 additions & 4 deletions spec/resource_messages_spec.rb
Original file line number Diff line number Diff line change
@@ -103,8 +103,8 @@ def to_json_list(list_object)
compare_requests(uri: '/v1/Account/MAXXXXXXXXXXXXXXXXXX/Message/',
method: 'POST',
data: {
src: '9898989890',
dst: '9090909090<9898989898',
src: '+9898989890',
dst: '+9090909090<+9898989898',
text: 'message',
powerpack_uuid: nil,
type: 'sms',
@@ -120,8 +120,8 @@ def to_json_list(list_object)
expect do
@api.messages
.create(
'9898989898',
%w[9090909090 9898989898],
'+9898989898',
%w[+9090909090 +9898989898],
'message',
type: 'sms',
url: 'http://url.com',