11defmodule Xcribe.Helpers.Formatter do
22 @ moduledoc false
33
4- @ content_type_regex ~r{ ^(\w *\/ \w *(\. \w *\+ \w *)?);?.*}
5-
4+ @ content_type_regex ~r/ ^(\w *\/ [ \w -] *(\. \w *\+ \w *)?);?.*/
5+ @ content_type_boundary_regex ~r / boundary=(.*);? /
66 @ doc """
77 return the content type by a list of header params
88
99 ### Options:
1010 * `default`: a value to be returned when not found content-type header.
1111 """
1212 def content_type ( headers , opts \\ [ ] ) when is_list ( headers ) do
13- Enum . reduce_while ( headers , Keyword . get ( opts , :default ) , & find_content_type / 2 )
13+ headers
14+ |> Enum . find_value ( Keyword . get ( opts , :default ) , & find_content_type / 1 )
15+ |> handle_regex_match ( )
1416 end
1517
1618 @ doc """
17- return the authorization header from a list of headers
19+ return the content type boundary
1820 """
19- def authorization ( headers ) when is_list ( headers ) do
20- Enum . reduce_while ( headers , nil , & find_authorization / 2 )
21+
22+ def content_type_boundary ( headers ) when is_list ( headers ) do
23+ headers
24+ |> Enum . find_value ( & find_content_type_boundary / 1 )
25+ |> handle_regex_match ( )
2126 end
2227
28+ @ doc """
29+ return the authorization header from a list of headers
30+ """
31+ def authorization ( headers ) when is_list ( headers ) ,
32+ do: Enum . reduce_while ( headers , nil , & find_authorization / 2 )
33+
2334 @ doc """
2435 Format the path params.
2536
@@ -29,18 +40,19 @@ defmodule Xcribe.Helpers.Formatter do
2940 def format_path_parameter ( name ) ,
3041 do: " #{ name } " |> Macro . camelize ( ) |> String . replace_prefix ( " " , "" )
3142
32- defp find_content_type ( { "content-type" , value } , _default ) do
33- @ content_type_regex
34- |> Regex . run ( value , capture: :all_but_first )
35- |> handle_content_type_regex ( )
36- end
43+ defp find_content_type ( { "content-type" , value } ) ,
44+ do: Regex . run ( @ content_type_regex , value , capture: :all_but_first )
45+
46+ defp find_content_type ( _tuple ) , do: nil
47+
48+ defp find_content_type_boundary ( { "content-type" , value } ) ,
49+ do: Regex . run ( @ content_type_boundary_regex , value , capture: :all_but_first )
3750
38- defp find_content_type ( _header , default ) , do: { :cont , default }
51+ defp find_content_type_boundary ( _tuple ) , do: nil
3952
4053 defp find_authorization ( { "authorization" , value } , _acc ) , do: { :halt , value }
4154 defp find_authorization ( _header , _acc ) , do: { :cont , nil }
4255
43- defp handle_content_type_regex ( nil ) , do: { :halt , nil }
44- defp handle_content_type_regex ( [ content_type ] ) , do: { :halt , content_type }
45- defp handle_content_type_regex ( [ content_type | _vnd_spec ] ) , do: { :halt , content_type }
56+ defp handle_regex_match ( [ value | _vnd_spec ] ) , do: value
57+ defp handle_regex_match ( value ) , do: value
4658end
0 commit comments