Created
March 1, 2015 16:05
-
-
Save frgomes/549f675ffae65dd8c581 to your computer and use it in GitHub Desktop.
Scala - Convert String to URI or Proxy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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