patchwork_csv_utils 0.1.2-arm64-darwin → 0.1.4-arm64-darwin

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5d48484a389b0f61fa40b9ff495a9529d5b6672531ec8a46058fcb031ff58e1f
4
- data.tar.gz: c817216b32c72b30887582774d31057547beec706679080905ad8c8b0a724e24
3
+ metadata.gz: f2ad80848ade16844e3fca6c549a43738f7ac7a40241073725ce7bdc0ecdcf25
4
+ data.tar.gz: 4dd8f233d6c0716064365cfcb9b565acdc9169b5a02ea3482375c26d24f72d29
5
5
  SHA512:
6
- metadata.gz: 95eca6a03e0ff76c85764e18b8e59c48e2c4e3dc30ce7921fbfa264f7aab9b5c9cbf431c9c912232bb84d2a3343bd7e91ddc97124a80b525095b2e7b74b8945b
7
- data.tar.gz: 3d9c7bedc83f9f6d8e4cf79b40f1dd647e75a1ad78b8a4f006d252f7e2c76344304e9502bc32867a66309233118332e774d6001dac1221e865ca15aa59c83402
6
+ metadata.gz: 23babd70402e83a357c685a6a16d5a6b3e4b0eed3dbee34ec274865fd223bceb0fbc9cb96985399abdd4ec9c6ad465f5d22541ed0807cb74208c53a2618435aa
7
+ data.tar.gz: 710b787a80d6e73c5c242330fa2864c7f0201799227536e4691e61b1a1bd689f21b855f176d65baf2ebfd65ec5177ddb8d2fa8d8c0ef91f7506e4c2ef05f3b36
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- patchwork_csv_utils (0.1.2)
4
+ patchwork_csv_utils (0.1.4)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -8,6 +8,6 @@ pub mod utils;
8
8
  fn init() -> Result<(), magnus::Error> {
9
9
  let module = define_module("CsvUtils")?;
10
10
  module.define_singleton_method("dedup", function!(dedup, 3))?;
11
- module.define_singleton_method("to_csv", function!(to_csv, 3))?;
11
+ module.define_singleton_method("to_csv", function!(to_csv, 2))?;
12
12
  Ok(())
13
13
  }
@@ -8,13 +8,15 @@ use magnus::Ruby;
8
8
 
9
9
  use crate::utils::{FileExtension, magnus_err};
10
10
 
11
- pub fn to_csv(ruby: &Ruby, xls_path: String, target_path: String, sheet_name: String) -> magnus::error::Result<()> {
11
+ pub fn to_csv(ruby: &Ruby, xls_path: String, target_path: String) -> magnus::error::Result<()> {
12
12
  if !xls_path.has_extension(&["xls"]) {
13
13
  return Err(magnus::Error::new(ruby.exception_standard_error(), "xls_path must be an xls file".to_string()));
14
14
  }
15
15
 
16
16
  let mut workbook: Xls<_> = open_workbook(xls_path.clone()).map_err(|e| magnus_err(ruby, e, format!("could not open xls: {}", xls_path).as_str()))?;
17
- let range = workbook.worksheet_range(sheet_name.as_str()).map_err(|e| magnus_err(ruby, e, "Error"))?;
17
+ let range = workbook.worksheet_range_at(0)
18
+ .ok_or(magnus::Error::new(ruby.exception_standard_error(), "no worksheet found in xls".to_string()))
19
+ .and_then(|r| r.map_err(|e| magnus_err(ruby, e, "could not read worksheet range")))?;
18
20
 
19
21
  let headers = range.headers().ok_or(magnus::Error::new(ruby.exception_standard_error(), "no headers found in xls".to_string()))?;
20
22
  let header_map: HashMap<String, usize> = headers.iter().enumerate().map(|(i, h)| (h.to_string(), i)).collect();
@@ -28,13 +30,17 @@ fn write_csv<W: Write>(ruby: &Ruby, dest: &mut W, range: &Range<Data>, header_ma
28
30
  let n = range.get_size().1 - 1;
29
31
  for (ri, r) in range.rows().enumerate() {
30
32
  let mut date_value = Utc::now().naive_utc();
31
- for (i, c) in r.iter().enumerate() {
32
- let date = header_map.get("Date").ok_or(missing_header(ruby, "Date"))?;
33
- let start = header_map.get("Start").ok_or(missing_header(ruby, "Start"))?;
34
- let end = header_map.get("End").ok_or(missing_header(ruby, "End"))?;
35
- let actual_start = header_map.get("Actual Start").ok_or(missing_header(ruby, "Actual Start"))?;
36
- let actual_end = header_map.get("Actual End").ok_or(missing_header(ruby, "Actual End"))?;
37
33
 
34
+ if skip_empty_rows(r) { continue; }
35
+ if skip_rows_with_no_request_id(&header_map, r) { continue; }
36
+
37
+ let date = header_map.get("Date").ok_or(missing_header(ruby, "Date"))?;
38
+ let start = header_map.get("Start").ok_or(missing_header(ruby, "Start"))?;
39
+ let end = header_map.get("End").ok_or(missing_header(ruby, "End"))?;
40
+ let actual_start = header_map.get("Actual Start").ok_or(missing_header(ruby, "Actual Start"))?;
41
+ let actual_end = header_map.get("Actual End").ok_or(missing_header(ruby, "Actual End"))?;
42
+
43
+ for (i, c) in r.iter().enumerate() {
38
44
  match *c {
39
45
  Data::Empty => Ok(()),
40
46
  Data::String(ref s) | Data::DateTimeIso(ref s) | Data::DurationIso(ref s) => {
@@ -63,6 +69,22 @@ fn write_csv<W: Write>(ruby: &Ruby, dest: &mut W, range: &Range<Data>, header_ma
63
69
  Ok(())
64
70
  }
65
71
 
72
+ fn skip_empty_rows(r: &[Data]) -> bool {
73
+ if r.iter().all(|c| c == &Data::Empty) {
74
+ return true;
75
+ }
76
+ false
77
+ }
78
+
79
+ fn skip_rows_with_no_request_id(header_map: &HashMap<String, usize>, r: &[Data]) -> bool {
80
+ if let Some(request_id) = header_map.get("Request Id") {
81
+ if r[*request_id] == Data::Empty {
82
+ return true;
83
+ }
84
+ }
85
+ false
86
+ }
87
+
66
88
  fn missing_header(ruby: &Ruby, header: &str) -> magnus::Error {
67
89
  magnus::Error::new(ruby.exception_standard_error(), format!("Missing '{}' header in xls", header))
68
90
  }
@@ -83,4 +105,5 @@ fn clean_strings(s: &str) -> String {
83
105
  s.replace("\n", " ")
84
106
  .replace("\r", " ")
85
107
  .replace("\"", "")
108
+ .replace("'", "")
86
109
  }
Binary file
Binary file
Binary file
Binary file
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CsvUtils
4
- VERSION = '0.1.2'
4
+ VERSION = '0.1.4'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: patchwork_csv_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.4
5
5
  platform: arm64-darwin
6
6
  authors:
7
7
  - kingsley.hendrickse
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-07-31 00:00:00.000000000 Z
11
+ date: 2024-08-01 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Deduplication of CSV files and XLS to CSV conversion.
14
14
  email: