km-export-processor 0.2.1 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bin/km-export-processor +15 -9
- data/lib/alias_parser.rb +20 -14
- data/lib/converters/json_compiler.rb +16 -0
- data/lib/converters/json_to_csv.rb +13 -16
- data/lib/converters/json_to_json.rb +6 -10
- data/lib/km-export-processor.rb +1 -0
- data/lib/reimporter.rb +6 -4
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a125a73e6d91278dc3758a499d66e43ffa32339c
|
4
|
+
data.tar.gz: 252a99be14e0aab383afc5b24fcb73c6f1357d4f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c4ba69d23ad4e905ba14b3ee7e13be375df5a3eb2b8ae38900dcafb6a43ba1bf3d8c2f90310bf4a333045fac385c7d2a198cdedec40f0c55d6124cd191e27730
|
7
|
+
data.tar.gz: 4cea71894f8a8b9a362656a374d2df5dbfe840c7ced8af3034c195236d2f4b7773c0ea82c1f145f86e69af29ebca2492cf6e12e4e3acf193213086ac1adaf8b2
|
data/bin/km-export-processor
CHANGED
@@ -4,25 +4,31 @@ require 'thor'
|
|
4
4
|
require 'km-export-processor'
|
5
5
|
|
6
6
|
class CLI < Thor
|
7
|
-
desc "json_to_json", "Processes
|
8
|
-
def json_to_json
|
9
|
-
|
10
|
-
|
7
|
+
desc "json_to_json [JSONFILE]", "Processes a non-standard KM JSON file, into a standard format"
|
8
|
+
def json_to_json(jsonfile)
|
9
|
+
puts "Processing nonstandard JSON file into standard format"
|
10
|
+
KMExport.json_to_json(jsonfile)
|
11
11
|
end
|
12
12
|
|
13
|
-
desc "
|
13
|
+
desc "json_compiler", "Compiles all KISSmetrics JSON files located in current directory, into a single (nonstandard) file"
|
14
|
+
def json_compiler
|
15
|
+
KMExport.json_compiler
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
desc "json_to_csv [JSONFILE]", "Takes a KISSmetrics JSON file as input, and converts it to CSV"
|
14
20
|
def json_to_csv(jsonfile)
|
15
|
-
KMExport.json_to_csv(jsonfile)
|
16
21
|
puts "Converting file #{jsonfile} to CSV"
|
22
|
+
KMExport.json_to_csv(jsonfile)
|
17
23
|
end
|
18
24
|
|
19
|
-
desc "parse_aliases [JSONFILE]", "Takes JSON as input, and separates the alias calls into their own JSON file"
|
25
|
+
desc "parse_aliases [JSONFILE]", "Takes KISSmetrics JSON as input, and separates the alias calls into their own JSON file"
|
20
26
|
def parse_aliases(jsonfile)
|
21
|
-
KMExport.alias_parser(jsonfile)
|
22
27
|
puts "Parsing aliases out of #{jsonfile} and into their own JSON file"
|
28
|
+
KMExport.alias_parser(jsonfile)
|
23
29
|
end
|
24
30
|
|
25
|
-
desc "reimport [JSONFILE] [APIKEY]", "Takes JSON, and a KM API key as input, and sends the data to KISSmetrics"
|
31
|
+
desc "reimport [JSONFILE] [APIKEY]", "Takes KISSmetrics JSON, and a KM API key as input, and sends the data to KISSmetrics"
|
26
32
|
def reimport(jsonfile, apikey)
|
27
33
|
puts "Sending data from #{jsonfile} to KISSmetrics product with key: #{apikey}"
|
28
34
|
reimporter = KMExport::Reimporter.new
|
data/lib/alias_parser.rb
CHANGED
@@ -1,22 +1,28 @@
|
|
1
1
|
module KMExport
|
2
2
|
def self.alias_parser(jsonfile)
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
3
|
+
input = IO.open(IO.sysopen(jsonfile))
|
4
|
+
alias_filename = Time.now.to_i.to_s + "_aliases.json"
|
5
|
+
new_filename = Time.now.to_i.to_s + "_result.json"
|
6
|
+
File.open(alias_filename, "w+").close
|
7
|
+
File.open(new_filename, "w+").close
|
8
|
+
alias_output = IO.open(IO.sysopen(alias_filename, "w"), "w")
|
9
|
+
data_output = IO.open(IO.sysopen(new_filename, "w"), "w")
|
8
10
|
|
9
|
-
|
10
|
-
|
11
|
-
|
11
|
+
until input.eof?
|
12
|
+
line = JSON.parse(input.readline)
|
13
|
+
if line["_p2"]
|
14
|
+
alias_output.write(JSON.generate(line))
|
15
|
+
alias_output.write("\n")
|
12
16
|
else
|
13
|
-
|
17
|
+
data_output.write(JSON.generate(line))
|
18
|
+
data_output.write("\n")
|
14
19
|
end
|
15
20
|
end
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
+
|
22
|
+
input.close
|
23
|
+
alias_output.close
|
24
|
+
data_output.close
|
25
|
+
|
26
|
+
File.delete(jsonfile)
|
21
27
|
end
|
22
28
|
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module KMExport
|
2
|
+
def self.json_compiler
|
3
|
+
files = Dir["*.json"]
|
4
|
+
result = []
|
5
|
+
filename = "#{Time.now.to_i.to_s}_result.json"
|
6
|
+
File.open(filename, "w+").close
|
7
|
+
output = IO.open(IO.sysopen(filename, "w"), "w")
|
8
|
+
|
9
|
+
files.each do |file|
|
10
|
+
input = IO.open(IO.sysopen(file))
|
11
|
+
until input.eof?
|
12
|
+
output.write(input.readline)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -1,27 +1,24 @@
|
|
1
1
|
module KMExport
|
2
2
|
def self.json_to_csv(jsonfile)
|
3
|
-
default_headers = ["_n", "_p", "_t", "_p2", "_a"]
|
4
3
|
headers = []
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
4
|
+
csv_filename = Time.now.to_i.to_s + "_converter_result.csv"
|
5
|
+
input = IO.open(IO.sysopen(jsonfile))
|
6
|
+
output = CSV.open(csv_filename, "w+")
|
7
|
+
|
8
|
+
until input.eof?
|
9
|
+
headers = headers | JSON.parse(input.readline).keys
|
10
10
|
end
|
11
11
|
|
12
|
-
|
12
|
+
output << headers
|
13
|
+
input.rewind
|
14
|
+
|
15
|
+
until input.eof?
|
16
|
+
row_data = JSON.parse(input.readline)
|
13
17
|
row_result = {}
|
14
18
|
headers.each do |header|
|
15
|
-
row_result[header.to_sym] =
|
16
|
-
end
|
17
|
-
results << row_result
|
18
|
-
end
|
19
|
-
|
20
|
-
CSV.open(Time.now.to_i.to_s + "_converter_result.csv", "wb") do |csv|
|
21
|
-
csv << results.first.keys
|
22
|
-
results.each do |hash|
|
23
|
-
csv << hash.values
|
19
|
+
row_result[header.to_sym] = row_data[header] || ""
|
24
20
|
end
|
21
|
+
output << row_result.values
|
25
22
|
end
|
26
23
|
end
|
27
24
|
end
|
@@ -1,17 +1,13 @@
|
|
1
1
|
module KMExport
|
2
|
-
def self.json_to_json
|
3
|
-
|
4
|
-
|
2
|
+
def self.json_to_json(filename)
|
3
|
+
input = IO.open(IO.sysopen(filename))
|
5
4
|
result = []
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
json.readlines.each do |line|
|
10
|
-
result << JSON.parse(line.chomp)
|
11
|
-
end
|
5
|
+
|
6
|
+
until input.eof?
|
7
|
+
result << JSON.parse(input.readline)
|
12
8
|
end
|
13
9
|
|
14
|
-
output = File.open(Time.now.to_i.to_s + "
|
10
|
+
output = File.open(Time.now.to_i.to_s + "_STANDARD.json", "w+")
|
15
11
|
|
16
12
|
output.write(JSON.pretty_generate(result))
|
17
13
|
output.close
|
data/lib/km-export-processor.rb
CHANGED
data/lib/reimporter.rb
CHANGED
@@ -1,15 +1,16 @@
|
|
1
1
|
class JSONQueue < Queue
|
2
2
|
def initialize(opts)
|
3
3
|
super()
|
4
|
-
@
|
4
|
+
@io = IO.open(IO.sysopen(opts[:file]))
|
5
5
|
@counter = opts[:counter]
|
6
6
|
end
|
7
7
|
|
8
8
|
def add_to_queue
|
9
|
-
|
10
|
-
|
11
|
-
self << line
|
9
|
+
100.times do
|
10
|
+
self << JSON.parse(@io.readline)
|
12
11
|
end
|
12
|
+
@counter += 100
|
13
|
+
puts "Adding 100 lines to queue, #{counter} so far"
|
13
14
|
end
|
14
15
|
end
|
15
16
|
|
@@ -28,6 +29,7 @@ module KMExport
|
|
28
29
|
:use_cron => false,
|
29
30
|
:to_stderr => true)
|
30
31
|
until queue.empty?
|
32
|
+
queue.add_to_queue if queue.length < 50
|
31
33
|
work_unit = queue.pop(true) rescue nil
|
32
34
|
send_line_to_KM(work_unit)
|
33
35
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: km-export-processor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Clay Whitley
|
@@ -131,6 +131,7 @@ extensions: []
|
|
131
131
|
extra_rdoc_files: []
|
132
132
|
files:
|
133
133
|
- lib/alias_parser.rb
|
134
|
+
- lib/converters/json_compiler.rb
|
134
135
|
- lib/converters/json_to_csv.rb
|
135
136
|
- lib/converters/json_to_json.rb
|
136
137
|
- lib/km-export-processor.rb
|