csv_to_products 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.txt +22 -0
- data/README.md +47 -0
- data/bin/csv_to_products +21 -0
- data/lib/csv_to_products.rb +64 -0
- data/lib/pairs.rb +42 -0
- metadata +63 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 733c30bb4373296ea1d8f6679cdcf574b3692709
|
4
|
+
data.tar.gz: 0a175e31df96210a6a65ef1af490eae96478a5cd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 865c9b1baed471873c5a35912eb75e551b32c9f84dd05488677a5b9d8af8aed164664bf5d51aafe6e63e404bf32f5d1efa1fc0ba4862516235861c596f183f29
|
7
|
+
data.tar.gz: 377db736f3b275f609e408143505675f765b5e697cf19f7c00ecc8c6e58a51540b0568cbd22684111a204eb9f74f3d28f8751c4de2a8f66d4ea7bd78fea1a1f3
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Max White
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# CSV to Products
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/jekyll-store/csv_to_products.svg?branch=master)](https://travis-ci.org/jekyll-store/csv_to_products)
|
4
|
+
|
5
|
+
A command line tool for parsing a csv of product data into markdown files with front matter suitable for use with [Jekyll-Store Front](https://github.com/jekyll-store/front).
|
6
|
+
|
7
|
+
## Usage
|
8
|
+
|
9
|
+
To install:
|
10
|
+
|
11
|
+
```
|
12
|
+
$ gem install csv_to_products
|
13
|
+
```
|
14
|
+
|
15
|
+
To parse:
|
16
|
+
|
17
|
+
```
|
18
|
+
$ csv_to_products my-products.csv my-products-folder
|
19
|
+
```
|
20
|
+
|
21
|
+
If the arguments are missing, it will try to parse `products.csv` into the `_products` folder.
|
22
|
+
|
23
|
+
Each product must have a unique name and a numeric price.
|
24
|
+
|
25
|
+
## Collection
|
26
|
+
|
27
|
+
Any field name of the form `collection[Member]` with be treated as a collection. For example a csv with fields like this,
|
28
|
+
|
29
|
+
| available_sizes[Small] | available_sizes[Medium] | available_sizes[Large] |
|
30
|
+
| ---------------------- | ----------------------- | ---------------------- |
|
31
|
+
| y | n | y |
|
32
|
+
|
33
|
+
Will be collected into the front matter as:
|
34
|
+
|
35
|
+
```yaml
|
36
|
+
available_sizes: Small Large
|
37
|
+
```
|
38
|
+
|
39
|
+
All values will be treated as false, except for: `y`, `Y`, `1`, `true`, `yes`, and `Yes`.
|
40
|
+
|
41
|
+
## Contributing
|
42
|
+
|
43
|
+
1. [Fork it](https://github.com/jekyll-store/csv_to_products/fork)
|
44
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
45
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
46
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
47
|
+
5. Create a new Pull Request
|
data/bin/csv_to_products
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require_relative '../lib/csv_to_products'
|
4
|
+
|
5
|
+
ARGV[0] ||= 'products.csv'
|
6
|
+
ARGV[1] ||= '_products'
|
7
|
+
ARGV.map! { |f| File.expand_path(f) }
|
8
|
+
|
9
|
+
begin
|
10
|
+
CSVToProducts.parse(*ARGV)
|
11
|
+
rescue FileNotFound
|
12
|
+
puts 'File can not be found'
|
13
|
+
rescue NameNotFound
|
14
|
+
puts 'Must have "name" field.'
|
15
|
+
rescue PriceNotFound
|
16
|
+
puts 'Must have "price" field.'
|
17
|
+
rescue NameNotUnique
|
18
|
+
puts 'All names must be unique.'
|
19
|
+
rescue PriceNotNumeric
|
20
|
+
puts 'All prices must be numeric'
|
21
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'csv'
|
2
|
+
require 'fileutils'
|
3
|
+
require_relative 'pairs'
|
4
|
+
|
5
|
+
module CSVToProducts
|
6
|
+
class << self
|
7
|
+
def parse(file, folder)
|
8
|
+
@folder = folder
|
9
|
+
@names = []
|
10
|
+
|
11
|
+
file_exists?(file)
|
12
|
+
create_folder
|
13
|
+
each_row(file) { check; create_product }
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
attr_reader :folder, :row
|
19
|
+
|
20
|
+
def file_exists?(file)
|
21
|
+
fail(FileNotFound) unless File.exists?(file)
|
22
|
+
end
|
23
|
+
|
24
|
+
def create_folder
|
25
|
+
FileUtils.mkdir_p(folder) unless File.directory?(folder)
|
26
|
+
end
|
27
|
+
|
28
|
+
def each_row(file)
|
29
|
+
CSV.foreach(file, headers: true) { |row| @row = row; yield }
|
30
|
+
end
|
31
|
+
|
32
|
+
def check
|
33
|
+
fail(NameNotFound) unless row['name']
|
34
|
+
fail(PriceNotFound) unless row['price']
|
35
|
+
fail(NameNotUnique) if @names.include?(row['name'])
|
36
|
+
fail(PriceNotNumeric) unless is_number?(row['price'])
|
37
|
+
@names << row['name']
|
38
|
+
end
|
39
|
+
|
40
|
+
def create_product
|
41
|
+
pairs = Pairs.create(row)
|
42
|
+
description = row['description']
|
43
|
+
File.write(product_path, "---\n#{pairs}---\n#{description}")
|
44
|
+
end
|
45
|
+
|
46
|
+
def product_path
|
47
|
+
File.expand_path("#{dasherize(row['name'])}.md", folder)
|
48
|
+
end
|
49
|
+
|
50
|
+
def dasherize(s)
|
51
|
+
s.to_s.gsub(' ', '-').downcase
|
52
|
+
end
|
53
|
+
|
54
|
+
def is_number?(s)
|
55
|
+
Float(s) rescue false
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
class FileNotFound < StandardError; end
|
61
|
+
class NameNotFound < StandardError; end
|
62
|
+
class PriceNotFound < StandardError; end
|
63
|
+
class NameNotUnique < StandardError; end
|
64
|
+
class PriceNotNumeric < StandardError; end
|
data/lib/pairs.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
module Pairs
|
2
|
+
class << self
|
3
|
+
def create(row)
|
4
|
+
@pairs = {}
|
5
|
+
|
6
|
+
row.each do |k, v|
|
7
|
+
d_k = dehumanize(k)
|
8
|
+
next if d_k == 'description'
|
9
|
+
next pairs[d_k] = v unless k.include?('[')
|
10
|
+
next unless affirmative?(v)
|
11
|
+
add_to_array(k, v)
|
12
|
+
end
|
13
|
+
|
14
|
+
pretty_pairs
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
attr_reader :pairs
|
20
|
+
|
21
|
+
def affirmative?(v)
|
22
|
+
%w(y Y 1 true yes Yes).include?(v)
|
23
|
+
end
|
24
|
+
|
25
|
+
def add_to_array(k, v)
|
26
|
+
k, v = k.scan(/[^\[\]]+/)
|
27
|
+
pairs[k] ||= []
|
28
|
+
pairs[k] << v
|
29
|
+
end
|
30
|
+
|
31
|
+
def pretty_pairs
|
32
|
+
pairs.map do |k, v|
|
33
|
+
v = v.join(' ') if v.is_a?(Array)
|
34
|
+
"#{k}: #{v}\n"
|
35
|
+
end.join
|
36
|
+
end
|
37
|
+
|
38
|
+
def dehumanize(s)
|
39
|
+
s.to_s.gsub(' ', '_').downcase
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: csv_to_products
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Max White
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-04-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3'
|
27
|
+
description:
|
28
|
+
email: mushishi78@gmail.com
|
29
|
+
executables:
|
30
|
+
- csv_to_products
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- LICENSE.txt
|
35
|
+
- README.md
|
36
|
+
- bin/csv_to_products
|
37
|
+
- lib/csv_to_products.rb
|
38
|
+
- lib/pairs.rb
|
39
|
+
homepage: https://github.com/jekyll-store/csv_to_products
|
40
|
+
licenses:
|
41
|
+
- MIT
|
42
|
+
metadata: {}
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
requirements: []
|
58
|
+
rubyforge_project:
|
59
|
+
rubygems_version: 2.2.2
|
60
|
+
signing_key:
|
61
|
+
specification_version: 4
|
62
|
+
summary: Jekyll-Store cmdline tool to translate CSV file to products.
|
63
|
+
test_files: []
|