HttpProxy.Utils.File

This module provides several paths associated with HttpProxy and file operation.

Summary

Functions

Structure associated with file paths

Export json data into path/file

Generate json file name with :rand.uniform

Get paths defined on config/"#{Mix.env}.exs"

Get paths in particular directory as list

Get decoded map data by JSX.decode/2

Functions

__struct__()

Structure associated with file paths.

Example

iex> HttpProxy.Utils.File.__struct__
%HttpProxy.Utils.File{export_path: "test/example", mapping_files: "mappings",
  play_path: "test/data", response_files: "__files"}
export(json, path, file)

Specs

export(binary, String.t, String.t) ::
  :ok |
  {:error, binary}

Export json data into path/file.

filename(path_info)

Specs

filename([String.t]) :: String.t

Generate json file name with :rand.uniform

Example

iex> HttpProxy.Utils.File.filename(["path-info-path"]) |> String.match?(~r(\Apath-info-path.*\.json\z))
true

iex> HttpProxy.Utils.File.filename("path-info-path") |> String.match?(~r(\Apath-info-path.*\.json\z))
true
get_export_binary_path()
get_export_binary_path(port)

Specs

get_export_binary_path(integer | binary) :: String.t
get_export_path()

Get paths defined on config/"#{Mix.env}.exs"

Exmaple

iex> HttpProxy.Utils.File.get_export_path
"test/example/mappings"

iex> HttpProxy.Utils.File.get_export_path(8080)
"test/example/8080/mappings"

iex> HttpProxy.Utils.File.get_export_path("8080")
"test/example/8080/mappings"

iex> HttpProxy.Utils.File.get_export_binary_path
"test/example/__files"

iex> HttpProxy.Utils.File.get_export_binary_path(8080)
"test/example/8080/__files"

iex> HttpProxy.Utils.File.get_export_binary_path("8080")
"test/example/8080/__files"

iex> HttpProxy.Utils.File.get_response_path
"test/data/__files"

iex> HttpProxy.Utils.File.get_mapping_path
"test/data/mappings"
get_export_path(port)

Specs

get_export_path(integer | binary) :: String.t
get_mapping_path()

Specs

get_mapping_path :: String.t
get_response_path()

Specs

get_response_path :: String.t
json_files(dir \\ ".")

Specs

json_files(String.t) ::
  {:ok, binary} |
  {:error, binary}
json_files(String.t) :: binary
json_files!(dir)

Get paths in particular directory as list.

Example

iex> HttpProxy.Utils.File.json_files!("test/data/mappings")
["test/data/mappings/sample.json", "test/data/mappings/sample2.json", "test/data/mappings/sample3.json"]

iex> HttpProxy.Utils.File.json_files("test/data/mappings")
{:ok, ["test/data/mappings/sample.json", "test/data/mappings/sample2.json", "test/data/mappings/sample3.json"]}
read_json_file(path)

Specs

read_json_file(String.t) ::
  {:ok, binary} |
  {:error, binary}
read_json_file!(path)

Specs

read_json_file!(String.t) :: binary

Get decoded map data by JSX.decode/2

Example

iex> HttpProxy.Utils.File.read_json_file!("test/data/mappings/sample.json")
%{"request" => %{"method" => "GET", "path" => "/request/path", "port" => 8080},
       "response" => %{"body" => "<html>hello world</html>", "cookies" => %{},
         "headers" => %{"Content-Type" => "text/html; charset=UTF-8", "Server" => "GFE/2.0"}, "status_code" => 200}}

iex> HttpProxy.Utils.File.read_json_file("test/data/mappings/sample.json")
{:ok,
      %{"request" => %{"method" => "GET", "path" => "/request/path", "port" => 8080},
        "response" => %{"body" => "<html>hello world</html>", "cookies" => %{},
          "headers" => %{"Content-Type" => "text/html; charset=UTF-8", "Server" => "GFE/2.0"}, "status_code" => 200}}}