Last active
December 26, 2015 19:19
-
-
Save anthony-ryan/7200582 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
require 'pry' | |
require 'csv' | |
class FileReader | |
def self.read_file(text, record_delim, field_delim, del_col = false) | |
ending = -1 | |
@result = [] | |
lines_array = text.split(record_delim) | |
lines_array.each do |line| | |
fields = line.split(field_delim) | |
fields.each_index do |index| | |
fields[index] = fields[index].force_encoding('UTF-8').encode('BINARY', :undef => :replace, :replace => "") | |
fields[index] = fields[index].gsub(/"(.*)"/, '\1') #removes beginning and end quotes | |
fields[index] = fields[index].gsub(/ /, '\1') #removes spaces | |
end | |
if del_col == true | |
fields.delete_at(-1) | |
end | |
fields[3].replace(fields[4]) unless fields.length < 5 && | |
@result << fields unless fields.length < 5 | |
lines_array | |
end | |
@result | |
end | |
def self.write_file(file_out) | |
CSV.open(file_out, "w") do |csv| | |
# here loop over the array elements (rows) | |
@result.each do |row| | |
csv << row # here you push the row onto the csv stack | |
end | |
end | |
end | |
end |
This file contains hidden or 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
require 'rest_client' | |
require 'pry' | |
response = RestClient.get('http://www.google.com') | |
binding.pry | |
response = RestClient.post | |
auth_token: | |
import_type: 'product', | |
file: File.new('.csv')) | |
binding.pry |
This file contains hidden or 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
require './file_reader.rb' | |
File.open(fname, 'r:ISO-8859-1:UTF-8') do |f| | |
text = f.read | |
FileReader.read_file(text, "\n", ",", true) | |
end | |
FileReader.write_file(file_out) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment