-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from softinio/add-rss-fetch
Add ability to fetch blogs using rss links
- Loading branch information
Showing
11 changed files
with
351 additions
and
50 deletions.
There are no files selected for viewing
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
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
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
43 changes: 43 additions & 0 deletions
43
core/src/main/scala/com/softinio/scalanews/HttpClient.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2023 Salar Rahmanian | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.softinio.scalanews | ||
|
||
import cats.effect._ | ||
import org.http4s.client.Client | ||
import org.http4s.ember.client.EmberClientBuilder | ||
import java.io.InputStream | ||
import org.http4s.Request | ||
import org.http4s.Method | ||
import org.http4s.Uri | ||
import fs2._ | ||
|
||
object HttpClient { | ||
|
||
def fetchRss(feedUrl: String): Resource[IO, InputStream] = { | ||
val request = Request[IO](Method.GET, Uri.unsafeFromString(feedUrl)) | ||
client.flatMap { client => | ||
client | ||
.run(request) | ||
.flatMap(response => io.toInputStreamResource(response.body)) | ||
} | ||
} | ||
|
||
private lazy val client: Resource[IO, Client[IO]] = | ||
EmberClientBuilder | ||
.default[IO] | ||
.build | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright 2023 Salar Rahmanian | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.softinio.scalanews | ||
|
||
import java.io.InputStream | ||
|
||
import cats.effect._ | ||
|
||
import com.rometools.rome.feed.synd.SyndFeed | ||
import com.rometools.rome.io.SyndFeedInput | ||
import com.rometools.rome.io.XmlReader | ||
|
||
object Rome { | ||
final private def parseFeed( | ||
feedStream: InputStream | ||
): IO[Either[Throwable, SyndFeed]] = | ||
IO.blocking { | ||
val input: SyndFeedInput = new SyndFeedInput() | ||
input.build(new XmlReader(feedStream)) | ||
}.attempt | ||
|
||
def fetchFeed(feedUrl: String): IO[Either[Throwable, SyndFeed]] = | ||
HttpClient.fetchRss(feedUrl).use(parseFeed(_)) | ||
} |
22 changes: 22 additions & 0 deletions
22
core/src/main/scala/com/softinio/scalanews/algebra/Article.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright 2023 Salar Rahmanian | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.softinio.scalanews.algebra | ||
|
||
import java.util.Date | ||
import org.http4s.Uri | ||
|
||
case class Article(title: String, url: Uri, publishedDate: Date) |
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
35 changes: 35 additions & 0 deletions
35
core/src/test/scala/com/softinio/scalanews/HttpClientSuite.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 2023 Salar Rahmanian | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.softinio.scalanews | ||
|
||
import cats.effect._ | ||
import munit.CatsEffectSuite | ||
|
||
import cats.effect.unsafe.IORuntime | ||
|
||
class HttpClientSuite extends CatsEffectSuite { | ||
|
||
implicit val runtime: IORuntime = cats.effect.unsafe.IORuntime.global | ||
test("Fetch Rss") { | ||
val result = HttpClient.fetchRss("https://www.softinio.com/index.xml") | ||
val obtained = result.use { res => | ||
val resultStr = new String(res.readAllBytes) | ||
IO(resultStr.contains("lightening-talks-at-pybay-2018")) | ||
} | ||
assertIO(obtained, true) | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
core/src/test/scala/com/softinio/scalanews/RomeSuite.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2023 Salar Rahmanian | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.softinio.scalanews | ||
|
||
import cats.effect._ | ||
|
||
import munit.CatsEffectSuite | ||
|
||
class RomeSuite extends CatsEffectSuite { | ||
|
||
test("Fetch Feed") { | ||
val obtained: IO[Boolean] = for { | ||
result <- Rome.fetchFeed("https://www.softinio.com/index.xml") | ||
} yield { | ||
result match { | ||
case Right(feed) => { | ||
val title = feed.getTitle | ||
title == "Salar Rahmanian" | ||
} | ||
case _ => false | ||
} | ||
} | ||
assertIO(obtained, true) | ||
} | ||
} |
Oops, something went wrong.