Skip to content

Instantly share code, notes, and snippets.

@madwork
Last active December 14, 2015 01:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madwork/5004883 to your computer and use it in GitHub Desktop.
Save madwork/5004883 to your computer and use it in GitHub Desktop.
Simple CSV Exporter

Usage

exporter = Exporter.new
exporter.header = ["Firstname", "Lastname", "Email"]
[%w(Vincent Durand vincent@foo.com), %w(Bob Marley bob@foo.com)].each do |user|
  exporter.add user
end
exporter.to_csv('ISO-8859-15//IGNORE', 'UTF-8')
require 'iconv'
class Exporter
def initialize(separator = ';')
@separator = separator
@header = []
@body = []
end
def header=(columns)
@header = columns.flatten
end
# Add value to body
#
# @param [Array] values
# @return [Array] @body
def add(values)
@body << values.flatten.join(@separator)
end
alias_method :<<, :add
# Build CSV
# Iconv (http://www.ruby-doc.org/stdlib-1.9.3/libdoc/iconv/rdoc/Iconv.html)
#
# @param [String] to
# @param [String] from
# @return [String]
def to_csv(to, from)
csv = Iconv.new(to, from)
csv.iconv header_as_string + body_as_string
end
private
def body_as_string
@body.join("\n")
end
def header_as_string
@header.join(@separator).concat("\n")
end
end
require 'minitest/autorun'
require File.join(File.expand_path(File.dirname(__FILE__)), 'exporter.rb')
class TestExporter < MiniTest::Unit::TestCase
def setup
@exporter = Exporter.new
@exporter.header = ["Foo", "Bar"]
@exporter.add [1, 2]
end
def test_should_set_header
assert_equal ["Foo", "Bar"], @exporter.instance_variable_get('@header')
end
def test_should_set_body
assert_equal ["1;2"], @exporter.instance_variable_get('@body')
end
def test_should_add_to_body
@exporter.add [3, 4]
assert_equal ["1;2", "3;4"], @exporter.instance_variable_get('@body')
end
def test_should_add_to_body_with_aliased_method
@exporter << [3, 4]
assert_equal ["1;2", "3;4"], @exporter.instance_variable_get('@body')
end
def test_should_export_to_csv
assert_equal "Foo;Bar\n1;2", @exporter.to_csv('ISO-8859-15//IGNORE', 'UTF-8')
end
def test_should_export_to_csv_with_empty
assert_equal "\n", Exporter.new.to_csv('ISO-8859-15//IGNORE', 'UTF-8')
end
def test_should_set_header_with_non_flatten_array
@exporter.header = ["Foo", ["Name1", "Name2"], "Bar"]
assert_equal ["Foo", "Name1", "Name2", "Bar"], @exporter.instance_variable_get('@header')
end
def test_should_raise_error_with_invalid_header_paramter
assert_raises(NoMethodError) { @exporter.header = "Foo" }
end
def test_should_raise_error_with_invalid_body_paramter
assert_raises(NoMethodError) { @exporter << "Foo" }
end
def test_should_get_header_as_string
assert_equal "Foo;Bar\n", @exporter.send(:header_as_string)
end
def test_should_get_body_as_string
@exporter.add [2, 3]
assert_equal "1;2\n2;3", @exporter.send(:body_as_string)
end
def test_should_customize_separator
@exporter = Exporter.new(':')
@exporter.header = ["Foo", "Bar"]
@exporter.add [1, 2]
assert_equal "Foo:Bar\n1:2", @exporter.to_csv('ISO-8859-15//IGNORE', 'UTF-8')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment