Skip to content

Instantly share code, notes, and snippets.

@frgomes
Created March 1, 2015 16:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frgomes/549f675ffae65dd8c581 to your computer and use it in GitHub Desktop.
Save frgomes/549f675ffae65dd8c581 to your computer and use it in GitHub Desktop.
Scala - Convert String to URI or Proxy
object UriSchemaConverters extends UriSchemaConverters
trait UriSchemaConverters {
import java.net.{InetSocketAddress, Proxy}
import com.netaporter.uri.Uri
implicit class RichOptionString2OptionUri(s: Option[String]) {
implicit def asUri: Option[Uri] = Option(s.getOrElse("/").asUri)
}
implicit class RichString2Uri(s: String) {
implicit def asUri: Uri = Uri(s)
}
implicit class RichOptionString2OptionProxy(s: Option[String]) {
implicit def asProxy: Option[Proxy] = Option(s.getOrElse("/").asProxy)
}
implicit class RichString2Proxy(s: String) {
implicit def asProxy: Proxy = {
val uri = Uri(s)
new Proxy(
uri.protocol.fold(Proxy.Type.HTTP) {
proto =>
if("socks" == proto.toLowerCase)
Proxy.Type.SOCKS
else
Proxy.Type.HTTP },
new InetSocketAddress(
uri.host.getOrElse("localhost"),
uri.port.getOrElse(3128)))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment