-
Notifications
You must be signed in to change notification settings - Fork 65
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
Support for Short Codes and Alphameric sender ID #39
Conversation
Gemfile
Outdated
@@ -1,3 +1,5 @@ | |||
source 'https://rubygems.org' | |||
|
|||
gemspec | |||
|
|||
gem 'pry' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure if pry should be here, or add it as development dependency.
lib/textris/message.rb
Outdated
if (matches = from.to_s.match(/(.*)\<(.*)\>\s*$/).to_a).size == 3 && | ||
Phony.plausible?(matches[2]) | ||
matches = from.to_s.match(/(.*)\<(.*)\>\s*$/).to_a | ||
return if matches.size != 3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
magic number, what 3 represents here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Lackoftactics https://github.com/visualitypl/textris/blame/ab770cb966218d6a4c2225a7a7ab360d590698ae/lib/textris/message.rb#L88
But:
irb(main):001:0> "Jan Matusz <+48123456789>".match(/(.*)\<(.*)\>\s*$/).to_a
=> ["Jan Matusz <+48123456789>", "Jan Matusz ", "+48123456789"]
So basicly it "splits" the sender string (which allows us to normalize/set the sender ID and sender name explicitly).
cabfe92
to
dddc47b
Compare
lib/textris/message.rb
Outdated
@@ -81,15 +81,20 @@ def parse_from(from) | |||
end | |||
|
|||
def parse_from_dual(from) | |||
if (matches = from.to_s.match(/(.*)\<(.*)\>\s*$/).to_a).size == 3 && | |||
Phony.plausible?(matches[2]) | |||
matches = from.to_s.match(/(.*)\<(.*)\>\s*$/).to_a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use captures instead of converting to array
from.to_s.match(/(.*)\<(.*)\>\s*$/).captures
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Lackoftactics done, but this required me to use the short try notation (&.
).
99a3bda
to
33537d6
Compare
lib/textris/message.rb
Outdated
Phony.plausible?(matches[2]) | ||
matches = from.to_s.match(/(.*)\<(.*)\>\s*$/).to_a | ||
return if matches.size != 3 | ||
if Phony.plausible?(matches[2]) || PhoneFormatter.is_a_short_code?(matches[2]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe some simple helper method, that will be readable for getting name and number from the dual field
.idea folder contains JetBrains' IDE data and configurations, it should not be added to the project.
261ff84
to
ab5ffd9
Compare
lib/textris/message.rb
Outdated
Phony.plausible?(matches[2]) | ||
[matches[1].strip, Phony.normalize(matches[2])] | ||
matches = from.match(/(.*)\<(.*)\>\s*$/) | ||
return if matches.nil? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
checking nil? maybe better to
return if !matches
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Lackoftactics maybe return unless matches
?
`&.` breaks backward compatibility - instead add explicit calls that check if there are any matches; and if there are - select the captures.
ab5ffd9
to
3b66337
Compare
lib/textris/phone_formatter.rb
Outdated
# that cannot be resolved as a short code or a phone | ||
# number, then it is Alphameric Sender ID | ||
def is_alphameric?(phone) | ||
!is_a_phone_number?(phone) && !is_a_short_code?(phone) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The rules for an alphanumeric ID are:
You may use any combination of 1 to 11 letters, A-Z and numbers, 0-9. Both lowercase and uppercase characters are supported as well as spaces. 1 letter and no more than 11 alphanumeric characters may be used.
I messed around with a regex to match this constraint and came up with:
\A(?=.*[a-zA-Z])[a-zA-z\d]{1,11}\z
Broken down, this is:
\A # Start of the string
(?=.*[a-zA-Z]) # Lookahead to ensure there is at least one letter in the entire string
[a-zA-z\d]{1,11} # Between 1 and 11 characters in the string
\z # End of the string
So we could rewrite this as:
def is_alphanumeric?(phone)
!!phone.to_s.match(/\A(?=.*[a-zA-Z])[a-zA-z\d]{1,11}\z/)
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome!
spec/textris/phone_formatter_spec.rb
Outdated
end | ||
|
||
it 'treat non-short code non-phone number numbers as alphamerics' do | ||
['8945', '894', '89', '8', '8945467', '89454678', '894546789'].each do |number| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests are incorrect, alphanumeric sender IDs are required to have at least one letter.
I'd recommend tests that ensured these numbers were not considered as alphanumeric sender IDs, but that the strings: a
, 1a
, 1aaaaaaaaaa
and ZZZZZZZZZZ9
are all considered valid alphanumeric IDs.
We should also ensure that alphanumeric sender IDs are not more than 11 characters long.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test case was not supposed to check whether it's classified as alphameric, but rather 'treated' as it was like it (so no additional formatting), if I remember correctly :) but yes, this should be replaced with a proper alphamerics tests, I agree.
Tested on example app:
Keep in mind that in order to use Alphanumeric sender ID you have to declare it this way:
from: 'My Team <Team Inc.>'
, where 'My Team' will be equal to name, andTeam Inc.
to Sender ID (.from_phone
).