diff options
author | vlad <vlad@driver.xyz> | 2017-06-13 10:25:55 -0700 |
---|---|---|
committer | vlad <vlad@driver.xyz> | 2017-06-13 10:25:55 -0700 |
commit | 0000a65ab4479a2a40e2d6468036438e9705b4aa (patch) | |
tree | 60c868828741e7e5367aa7b6d167abbdaf91d5b8 /src/main/scala/xyz/driver/common/resources/ResourcesStorage.scala | |
download | rest-query-0000a65ab4479a2a40e2d6468036438e9705b4aa.tar.gz rest-query-0000a65ab4479a2a40e2d6468036438e9705b4aa.tar.bz2 rest-query-0000a65ab4479a2a40e2d6468036438e9705b4aa.zip |
Initial extraction of Driver non-specific utilities
Diffstat (limited to 'src/main/scala/xyz/driver/common/resources/ResourcesStorage.scala')
-rw-r--r-- | src/main/scala/xyz/driver/common/resources/ResourcesStorage.scala | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/main/scala/xyz/driver/common/resources/ResourcesStorage.scala b/src/main/scala/xyz/driver/common/resources/ResourcesStorage.scala new file mode 100644 index 0000000..f52d992 --- /dev/null +++ b/src/main/scala/xyz/driver/common/resources/ResourcesStorage.scala @@ -0,0 +1,39 @@ +package xyz.driver.common.resources + +import scala.io.{Codec, Source} + +trait ResourcesStorage { + + /** + * @param resourcePath Don't forget / at start + */ + def getFirstLine(resourcePath: String): String + +} + +object RealResourcesStorage extends ResourcesStorage { + + def getFirstLine(resourcePath: String): String = { + val resourceUrl = getClass.getResource(resourcePath) + Option(resourceUrl) match { + case Some(url) => + val source = Source.fromURL(resourceUrl)(Codec.UTF8) + try { + val lines = source.getLines() + if (lines.isEmpty) throw new RuntimeException(s"'$resourcePath' is empty") + else lines.next() + } finally { + source.close() + } + case None => + throw new RuntimeException(s"Can not find the '$resourcePath'!") + } + } + +} + +object FakeResourcesStorage extends ResourcesStorage { + + def getFirstLine(resourcePath: String): String = "" + +} |