Skip to content

Commit

Permalink
Builder pattern for robots policies
Browse files Browse the repository at this point in the history
  • Loading branch information
versecafe committed Aug 11, 2024
1 parent bcc0a99 commit 3c09095
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Further documentation can be found at <https://hexdocs.pm/webls>.
| ---------- | --------- | ----------------- | ---------- |
| Sitemap | Complete | Complete | None |
| RSS v2.0 | Complete | None | None |
| Robots.txt | Complete | None | None |
| Robots.txt | Complete | Complete | None |

## Development

Expand Down
2 changes: 1 addition & 1 deletion gleam.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name = "webls"
version = "1.2.0"
version = "1.3.0"

description = "A simple web utility library for RSS feeds, Sitemaps, Robots.txt, etc."
licences = ["Apache-2.0"]
Expand Down
18 changes: 18 additions & 0 deletions src/webls/robots.gleam
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import gleam/list
import gleam/result

// Stringify ------------------------------------------------------------------
//
pub fn to_string(config: RobotsConfig) -> String {
"Sitemap: "
<> config.sitemap_url
Expand All @@ -26,6 +28,22 @@ fn robot_to_string(robot: Robot) -> String {
|> result.unwrap("")
}

// Builder Patern -------------------------------------------------------------

pub fn robot(user_agent: String) -> Robot {
Robot(user_agent, [], [])
}

pub fn allowed_routes(robot: Robot, routes: List(String)) -> Robot {
Robot(..robot, allowed_routes: routes)
}

pub fn disallowed_routes(robot: Robot, routes: List(String)) -> Robot {
Robot(..robot, disallowed_routes: routes)
}

// Types ----------------------------------------------------------------------

/// The configuration for a robots.txt file
pub type RobotsConfig {
RobotsConfig(
Expand Down
29 changes: 29 additions & 0 deletions test/webls_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,32 @@ pub fn robots_to_string_test() -> Nil {
{ length > 200 }
|> should.be_true
}

pub fn robots_builder_test() -> Nil {
let built =
RobotsConfig("https://example.com/sitemap.xml", [
robots.robot("googlebot")
|> robots.allowed_routes(["/posts/", "/contact/"]),
robots.robot("bingbot")
|> robots.allowed_routes(["/posts/", "/contact/", "/private/"]),
robots.robot("yandex")
|> robots.disallowed_routes(["/"]),
])

let manual =
RobotsConfig("https://example.com/sitemap.xml", [
Robot(
user_agent: "googlebot",
allowed_routes: ["/posts/", "/contact/"],
disallowed_routes: [],
),
Robot(
user_agent: "bingbot",
allowed_routes: ["/posts/", "/contact/", "/private/"],
disallowed_routes: [],
),
Robot(user_agent: "yandex", allowed_routes: [], disallowed_routes: ["/"]),
])

built |> should.equal(manual)
}

0 comments on commit 3c09095

Please sign in to comment.