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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0d3ac999d514d6a7934b57f20d9b0348cbf53511
4
- data.tar.gz: ef204f4103e100e7be136c9ab076432e5c8652d2
3
+ metadata.gz: a125a73e6d91278dc3758a499d66e43ffa32339c
4
+ data.tar.gz: 252a99be14e0aab383afc5b24fcb73c6f1357d4f
5
5
  SHA512:
6
- metadata.gz: 480ffa22ca733b1fc31fdb447a729d3bb3c6431e171c66f8c9d680153d81b849a4cea05a58a4e4e7b119861b72a381f8f65724f8c1ff03dafba472bdcd4b132c
7
- data.tar.gz: 5c2d7b3705625808e4a70fbc9f254494beb6e750814548aa1eb371005d6c1f2e348c3d4b51b18bbe4cabee179733ee7f2363a1f70be0678c4689f854bd3417df
6
+ metadata.gz: c4ba69d23ad4e905ba14b3ee7e13be375df5a3eb2b8ae38900dcafb6a43ba1bf3d8c2f90310bf4a333045fac385c7d2a198cdedec40f0c55d6124cd191e27730
7
+ data.tar.gz: 4cea71894f8a8b9a362656a374d2df5dbfe840c7ced8af3034c195236d2f4b7773c0ea82c1f145f86e69af29ebca2492cf6e12e4e3acf193213086ac1adaf8b2
@@ -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 all JSON in current directory, to standard format."
8
- def json_to_json
9
- KMExport.json_to_json
10
- puts "Processing JSON files in this directory, and outputting new combined file"
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 "json_to_csv [JSONFILE]", "Takes a standard JSON file as input, and converts it to CSV"
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
@@ -1,22 +1,28 @@
1
1
  module KMExport
2
2
  def self.alias_parser(jsonfile)
3
- file = File.open(jsonfile)
4
- alias_file = File.open(Time.now.to_i.to_s + "_aliases.json", "w+")
5
- json = JSON.parse(file.read)
6
- aliases = []
7
- data = []
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
- json.each do |row|
10
- if row["_p2"]
11
- aliases << row
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
- data << row
17
+ data_output.write(JSON.generate(line))
18
+ data_output.write("\n")
14
19
  end
15
20
  end
16
- new_file = File.open(jsonfile, "w+")
17
- new_file.write(JSON.pretty_generate(data))
18
- alias_file.write(JSON.pretty_generate(aliases))
19
- new_file.close
20
- alias_file.close
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
- file = File.open(jsonfile)
6
- json = JSON.parse(file.read)
7
- results = []
8
- json.each do |row|
9
- headers = headers | row.keys
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
- json.each do |row|
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] = row[header] || ""
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
- files = Dir["*.json"]
4
-
2
+ def self.json_to_json(filename)
3
+ input = IO.open(IO.sysopen(filename))
5
4
  result = []
6
-
7
- files.each do |file|
8
- json = File.open(file, "r")
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 + "_result.json", "w+")
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
@@ -8,5 +8,6 @@ end
8
8
 
9
9
  require_relative 'converters/json_to_json'
10
10
  require_relative 'converters/json_to_csv'
11
+ require_relative 'converters/json_compiler'
11
12
  require_relative 'reimporter'
12
13
  require_relative 'alias_parser'
@@ -1,15 +1,16 @@
1
1
  class JSONQueue < Queue
2
2
  def initialize(opts)
3
3
  super()
4
- @file = opts[:file]
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
- json = JSON.parse(File.read(@file))
10
- json.each do |line|
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.2.1
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