Skip to content

Commit

Permalink
added smsifier with smsspec
Browse files Browse the repository at this point in the history
  • Loading branch information
Ana Marjanica committed Apr 12, 2012
1 parent 6f62dd3 commit 8f23ff6
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 2 deletions.
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ organization := "hr.element.etb"

name := "etb-slug"

version := "0.0.1"
version := "0.0.2"

// ### Build settings ###

Expand Down Expand Up @@ -33,4 +33,4 @@ publishArtifact in (Compile, packageDoc) := false

// ### Misc ###

initialCommands := "import hr.element.doit.slug._"
//initialCommands := "import hr.element.doit.slug._"
49 changes: 49 additions & 0 deletions src/main/scala/hr/element/etb/slug/SMSify.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package hr.element.etb
package slug

import com.ibm.icu.text.Transliterator
import java.util.regex.Pattern



object SMSifier {
protected val SMSUnsafeChars = """[^!-/0-9:-@A-Za-z£¥\n\s]+"""

private val default =
new SMSifier(TransliterateRules.latinToASCII, "-")

def apply(text: String) = default(text)
}


case class SMSifier (transRules: String, replacement: String) {
private val Trans = Transliterator.getInstance(transRules)

private val SMSUnsafeReplacePattern =
"(%s|%s)+" format(SMSifier.SMSUnsafeChars, Pattern.quote(replacement)) r

private val SMSUnsafeTrimPattern =
"^%s|%1$s$" format(SMSifier.SMSUnsafeChars) r

private val SMSWhiteSpace =
"""\s+"""r

// convert latin letters to ASCII (ex. đ->d)
protected val transliterate =
Trans.transliterate(_: String)

// trim all SMS non-safe characters from the beginning and the end
protected val trim =
SMSUnsafeTrimPattern.replaceAllIn(_: String, "")

protected val trimWhiteSpaceToOne =
SMSWhiteSpace.replaceAllIn(_: String, " ")

// replace all SMS non-safe characters
protected val sanitize =
SMSUnsafeReplacePattern.replaceAllIn(_: String, replacement)

def apply(text: String) = {
(transliterate andThen trim andThen trimWhiteSpaceToOne andThen sanitize)(text)
}
}
42 changes: 42 additions & 0 deletions src/test/scala/hr/element/etb/slug/SMSSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package hr.element.etb.slug
package test

import org.scalatest._
import org.scalatest.matchers._



class SMSSpec extends FeatureSpec with GivenWhenThen with MustMatchers{

feature("SMS URL sanitization"){
info("SMS must convert every evil character combination into a pretty URL-safe string")

scenario("SMS unsafe character removal"){
val in = "aZ{}[]! a";
given ("a string containing: %s" format in)
val res = "aZ-! a"
then ("it should return a string: %s" format res)
val out = SMSifier(in)
out must equal (res)
}

scenario("SMS unsafe character trimming"){
val in = "!aćsčš ";
given ("a string containing: %s" format in)
val res = "!acscs "
then ("it should return a string: %s" format res)
val out = SMSifier(in)
out must equal (res)
}

scenario("SMS whitespace trimming to one character"){
val in = "Helooooooooooooo oooo";
given ("a string containing: %s" format in)
val res = "Helooooooooooooo oooo";
then ("it should return a string: %s" format res)
val out = SMSifier(in)
out must equal (res)
}
}

}
9 changes: 9 additions & 0 deletions src/test/scala/hr/element/etb/slug/SluggifySpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,14 @@ class SluggifySpec extends FeatureSpec with GivenWhenThen with MustMatchers {
val out = Sluggifier(in)
out must equal (res)
}

scenario("URL unsafe character trimming2"){
val in = """~ $Hajduk ! Is better!!!Slayer rules$ ~""" mkString;
given ("an URL unsafe string: %s" format in)
val res = "hajduk-is-better-slayer-rules"
then ("the resulting slug must be trimmed: %s" format res)
val out = Sluggifier(in)
out must equal (res)
}
}
}

0 comments on commit 8f23ff6

Please sign in to comment.