-
Notifications
You must be signed in to change notification settings - Fork 109
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 #9 from oltarasenko/csv_encoder
Add CSV encoder pipeline
- Loading branch information
Showing
4 changed files
with
142 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
defmodule Crawly.Pipelines.CSVEncoder do | ||
@moduledoc """ | ||
Encodes a given item (map) into CSV | ||
""" | ||
@behaviour Crawly.Pipeline | ||
|
||
@impl Crawly.Pipeline | ||
def run(item, state) do | ||
case Application.get_env(:crawly, :item) do | ||
:undefined -> | ||
{false, state} | ||
|
||
fields -> | ||
new_item = | ||
Enum.reduce(fields, "", fn | ||
field, "" -> | ||
"#{inspect(Map.get(item, field, ""))}" | ||
|
||
field, acc -> | ||
acc <> "," <> "#{inspect(Map.get(item, field, ""))}" | ||
end) | ||
|
||
{new_item, state} | ||
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