patchwork_csv_utils 0.1.3-x86_64-linux → 0.1.4-x86_64-linux

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
  SHA256:
3
- metadata.gz: d6e5fcb622c827b9824a0369af583ce409c968ab47e0eed41d4b5ae474907b11
4
- data.tar.gz: 87f48b77673c6bb56ca442f13a5b67d7d3e7de994160cbe7a8bc655734b76b13
3
+ metadata.gz: b9d62eb73016a46a3ec9caac2d43fd5db562a0856831c7f0e544e9c570e8d550
4
+ data.tar.gz: 1262e0f45c2b8cfe896be3d4bd6f1074db61aa2b252049f5090cec5feba05c1a
5
5
  SHA512:
6
- metadata.gz: 596afffb37c434613379f79b3236cb0bedbbfd93005d812aa77ad54eccf8f2e804d384fa3bd5cf82dc3fdf062d7b2ccad616b52c22cfca773db1767b7420b2e1
7
- data.tar.gz: 1b62c532de204e216ba975e83ef0b53af34be282b7f9e7b988ece6319df61542e00a7ee0c448ca8cc0120b9fd8359a181442b4476d1912313da52c079a8fe3cc
6
+ metadata.gz: 4fca3c59b6ff0b5cfd1dcc2ad0ae6efdf8005716980c0a1d111bff0190a4fae3c9bc6b560086416c794be89e3eab46ae6ffc34bdfc408a806c4ac07fe1660608
7
+ data.tar.gz: 28454948d621cd2272b9a6790d735ce7e2d95da51202accb76a3d56f1a5b2b6bc9a274bf7473073bbc99fac25a64de818ed0a1b9839b94dc0893241ff0290c24
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- patchwork_csv_utils (0.1.3)
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();
@@ -29,17 +31,16 @@ fn write_csv<W: Write>(ruby: &Ruby, dest: &mut W, range: &Range<Data>, header_ma
29
31
  for (ri, r) in range.rows().enumerate() {
30
32
  let mut date_value = Utc::now().naive_utc();
31
33
 
32
- if r.iter().all(|c| c == &Data::Empty) {
33
- continue;
34
- }
34
+ if skip_empty_rows(r) { continue; }
35
+ if skip_rows_with_no_request_id(&header_map, r) { continue; }
35
36
 
36
- for (i, c) in r.iter().enumerate() {
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"))?;
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
42
 
43
+ for (i, c) in r.iter().enumerate() {
43
44
  match *c {
44
45
  Data::Empty => Ok(()),
45
46
  Data::String(ref s) | Data::DateTimeIso(ref s) | Data::DurationIso(ref s) => {
@@ -68,6 +69,22 @@ fn write_csv<W: Write>(ruby: &Ruby, dest: &mut W, range: &Range<Data>, header_ma
68
69
  Ok(())
69
70
  }
70
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
+
71
88
  fn missing_header(ruby: &Ruby, header: &str) -> magnus::Error {
72
89
  magnus::Error::new(ruby.exception_standard_error(), format!("Missing '{}' header in xls", header))
73
90
  }
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.3'
4
+ VERSION = '0.1.4'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: patchwork_csv_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: x86_64-linux
6
6
  authors:
7
7
  - kingsley.hendrickse