Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract a module for HTTP Date strings
This parsing is used in two different modules now, and there was a bug in it, which tells me it should be in its own module and tested separately.
- Loading branch information
Showing
5 changed files
with
106 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
defmodule Liberator.HTTPDateTime do | ||
@moduledoc false | ||
@moduledoc since: "1.2" | ||
|
||
@strftime_format "%a, %d %b %Y %H:%M:%S GMT" | ||
|
||
@doc """ | ||
Checks to see if a string is a valid HTTP date. | ||
## Examples | ||
iex> Liberator.HTTPDateTime.valid?("Wed, 21 Oct 2015 07:28:00 GMT") | ||
true | ||
iex> Liberator.HTTPDateTime.valid?("2015-10-21 07:28:00Z") | ||
false | ||
""" | ||
@doc since: "1.2" | ||
def valid?(str) do | ||
case parse(str) do | ||
{:ok, _time} -> true | ||
_ -> false | ||
end | ||
end | ||
|
||
@doc """ | ||
Parses an HTTP Date string into an Elixir `DateTime` object. | ||
## Examples | ||
iex> Liberator.HTTPDateTime.parse("Wed, 21 Oct 2015 07:28:00 GMT") | ||
{:ok, ~U[2015-10-21 07:28:00Z]} | ||
iex> Liberator.HTTPDateTime.parse("2015-10-21 07:28:00Z") | ||
{:error, "Expected `weekday abbreviation` at line 1, column 1."} | ||
""" | ||
@doc since: "1.2" | ||
def parse(str) do | ||
case Timex.parse(str, @strftime_format, :strftime) do | ||
{:ok, datetime} -> {:ok, DateTime.from_naive!(datetime, "Etc/UTC")} | ||
err -> err | ||
end | ||
end | ||
|
||
@doc """ | ||
Like `parse/1` except will raise an error if the string cannot be parsed. | ||
## Examples | ||
iex> Liberator.HTTPDateTime.parse!("Wed, 21 Oct 2015 07:28:00 GMT") | ||
~U[2015-10-21 07:28:00Z] | ||
""" | ||
@doc since: "1.2" | ||
def parse!(str) do | ||
Timex.parse!(str, @strftime_format, :strftime) | ||
|> DateTime.from_naive!("Etc/UTC") | ||
end | ||
|
||
@doc """ | ||
Formats a datetime into an HTTP Date string, and raises if there is an error. | ||
## Examples | ||
iex> Liberator.HTTPDateTime.format!(~U[2015-10-21 07:28:00Z]) | ||
"Wed, 21 Oct 2015 07:28:00 GMT" | ||
""" | ||
def format!(datetime) do | ||
Timex.format!(datetime, @strftime_format, :strftime) | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
defmodule Liberator.HTTPDateTimeTest do | ||
use ExUnit.Case | ||
doctest Liberator.HTTPDateTime | ||
|
||
describe "parse!/1" do | ||
test "raises on error" do | ||
assert_raise Timex.Parse.ParseError, "Expected `weekday abbreviation` at line 1, column 1.", fn -> | ||
Liberator.HTTPDateTime.parse!("asdf") | ||
end | ||
end | ||
end | ||
|
||
describe "format!/1" do | ||
test "raises on error" do | ||
assert_raise ArgumentError, fn -> | ||
Liberator.HTTPDateTime.format!("asdf") | ||
end | ||
end | ||
end | ||
end |
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