shipping_materials 0.0.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 +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +232 -0
- data/Rakefile +1 -0
- data/examples/example.rb +33 -0
- data/lib/shipping_materials.rb +19 -0
- data/lib/shipping_materials/config.rb +35 -0
- data/lib/shipping_materials/csv_dsl.rb +82 -0
- data/lib/shipping_materials/group.rb +27 -0
- data/lib/shipping_materials/mixins/sortable.rb +26 -0
- data/lib/shipping_materials/packager.rb +52 -0
- data/lib/shipping_materials/packing_slips.rb +17 -0
- data/lib/shipping_materials/s3.rb +23 -0
- data/lib/shipping_materials/sorter.rb +49 -0
- data/lib/shipping_materials/storage.rb +54 -0
- data/lib/shipping_materials/version.rb +3 -0
- data/shipping_materials.gemspec +28 -0
- data/test/data.rb +217 -0
- data/test/files/template.mustache +16 -0
- data/test/files/test.html +3 -0
- data/test/models.rb +23 -0
- data/test/suite.rb +6 -0
- data/test/test_helper.rb +9 -0
- data/test/unit/config_test.rb +14 -0
- data/test/unit/csv_dsl_test.rb +117 -0
- data/test/unit/group_test.rb +61 -0
- data/test/unit/packager_test.rb +63 -0
- data/test/unit/packing_slips_test.rb +9 -0
- data/test/unit/s3_test.rb +17 -0
- data/test/unit/sorter_test.rb +48 -0
- data/test/unit/storage_test.rb +37 -0
- metadata +162 -0
data/test/models.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
module TestModels
|
2
|
+
class Order
|
3
|
+
attr_accessor :id, :date, :name, :address, :email, :phone, :country,
|
4
|
+
:shipping_method, :line_items
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@line_items = []
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class LineItem
|
12
|
+
attr_accessor :id, :name, :quantity, :price, :type, :variant
|
13
|
+
end
|
14
|
+
|
15
|
+
class Variant
|
16
|
+
attr_accessor :name
|
17
|
+
|
18
|
+
def initialize(hash)
|
19
|
+
hash.each {|k,v| send("#{k}=", v) }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
data/test/suite.rb
ADDED
data/test/test_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ShippingMaterials
|
4
|
+
class ConfigTest < TestCase
|
5
|
+
def test_config
|
6
|
+
ShippingMaterials.config do |config|
|
7
|
+
config.save_path = './test/files/'
|
8
|
+
end
|
9
|
+
|
10
|
+
assert_equal './test/files', ShippingMaterials::Config.save_path,
|
11
|
+
"Config variables are not being set"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,117 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ShippingMaterials
|
4
|
+
class CSVDSLTest < TestCase
|
5
|
+
def test_row_array
|
6
|
+
@csv = ShippingMaterials::CSVDSL.new(orders)
|
7
|
+
array = [:hello, :goodbye, 'Hello there']
|
8
|
+
|
9
|
+
@csv.row(array)
|
10
|
+
|
11
|
+
assert_equal array, @csv.row_maps[:object],
|
12
|
+
"Passing array to CSVDSL#row does not work"
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_row_hash
|
16
|
+
@csv = ShippingMaterials::CSVDSL.new(orders, headers: true)
|
17
|
+
hash = { :order_id => :id,
|
18
|
+
:name => :name,
|
19
|
+
:static_field => 'A string' }
|
20
|
+
|
21
|
+
@csv.row(hash)
|
22
|
+
|
23
|
+
assert_equal %w(order_id name static_field), @csv.headers,
|
24
|
+
"CSVDSL headers are not being set properly"
|
25
|
+
assert_equal hash.values, @csv.row_maps[:object],
|
26
|
+
"CSVDSL row map values not being properly set"
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_row_hash_with_array
|
30
|
+
@csv = CSVDSL.new(orders)
|
31
|
+
hash = { :line_items => [:hello, :goodbye, 'Hello there'] }
|
32
|
+
|
33
|
+
@csv.row(hash)
|
34
|
+
|
35
|
+
assert_equal hash[:line_items], @csv.row_maps[:line_items],
|
36
|
+
"CSVDSL row map not being set properly when given " \
|
37
|
+
"hash with array"
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_row_hash_with_hash
|
41
|
+
@csv = CSVDSL.new(orders, headers: true)
|
42
|
+
hash = {
|
43
|
+
:line_items => {
|
44
|
+
:order_id => :id,
|
45
|
+
:name => :name,
|
46
|
+
:static_field => 'A string'
|
47
|
+
}
|
48
|
+
}
|
49
|
+
|
50
|
+
@csv.row(hash)
|
51
|
+
|
52
|
+
assert_equal %w(order_id name static_field), @csv.headers,
|
53
|
+
"CSVDSL headers should be strings"
|
54
|
+
assert_equal hash[:line_items].values, @csv.row_maps[:line_items],
|
55
|
+
"CSVDSL values should match line_item values"
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_row_multi_call
|
60
|
+
@csv = CSVDSL.new(orders, headers: true)
|
61
|
+
hash1 = {
|
62
|
+
:order_id => :id,
|
63
|
+
:name => :name,
|
64
|
+
:static_fields => 'A string'
|
65
|
+
}
|
66
|
+
|
67
|
+
hash2 = {
|
68
|
+
:line_items => {
|
69
|
+
:id => :id,
|
70
|
+
:name => :name,
|
71
|
+
:quantity => 2
|
72
|
+
}
|
73
|
+
}
|
74
|
+
|
75
|
+
@csv.row(hash1)
|
76
|
+
@csv.row(hash2)
|
77
|
+
|
78
|
+
assert_equal %w(order_id name static_fields), @csv.headers,
|
79
|
+
"Headers should not be overwritten by secondary call to row"
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_array_chain
|
83
|
+
@csv = CSVDSL.new(orders.select {|o| o.name == 'Andrew' })
|
84
|
+
hash = {
|
85
|
+
:line_items => {
|
86
|
+
:order_id => :id,
|
87
|
+
:name => :name,
|
88
|
+
:variant_name => [ :variant, :name ]
|
89
|
+
}
|
90
|
+
}
|
91
|
+
|
92
|
+
@csv.row(hash)
|
93
|
+
|
94
|
+
assert_equal "1,Plague Soundscapes,plague soundscapes\n" \
|
95
|
+
"2,Surfer Rosa,surfer rosa\n",
|
96
|
+
@csv.to_csv,
|
97
|
+
"CSV method chaining is borked"
|
98
|
+
end
|
99
|
+
|
100
|
+
def test_to_csv
|
101
|
+
@csv = CSVDSL.new(orders.select {|o| o.id == 1 }, headers: true)
|
102
|
+
@csv.row :order_id => :id,
|
103
|
+
:name => :name,
|
104
|
+
:static_fields => 'A string'
|
105
|
+
|
106
|
+
@csv.row :line_items => [:id, :name, :quantity]
|
107
|
+
|
108
|
+
output = "order_id,name,static_fields\n" \
|
109
|
+
"1,Andrew,A string\n" \
|
110
|
+
"1,Plague Soundscapes,3\n" \
|
111
|
+
"2,Surfer Rosa,1"
|
112
|
+
|
113
|
+
assert_equal output.strip, @csv.to_csv.strip,
|
114
|
+
"CSVDSL not properly converting to CSV"
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ShippingMaterials
|
4
|
+
class GroupTest < TestCase
|
5
|
+
def setup
|
6
|
+
@group = ShippingMaterials::Group.new('basename', orders)
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_filter
|
10
|
+
@group.filter { country == 'CA' }
|
11
|
+
|
12
|
+
assert_equal 4, @group.objects.size,
|
13
|
+
"The number of groups should be 4"
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_csv
|
17
|
+
@group.filter { name == 'Derek' }
|
18
|
+
assert_equal 1, @group.objects.size,
|
19
|
+
"The number of Groups should be 1"
|
20
|
+
|
21
|
+
@group.csv {
|
22
|
+
row :order_id => :id,
|
23
|
+
:name => :name,
|
24
|
+
:static => 'Hello'
|
25
|
+
}
|
26
|
+
|
27
|
+
assert_equal "3,Derek,Hello\n", @group.csvs.first.to_csv,
|
28
|
+
"The Group#csv method is borked"
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_sort_mixin
|
32
|
+
@group.filter { country == 'CA' }
|
33
|
+
|
34
|
+
@group.sort {
|
35
|
+
rule { line_items.detect {|li| li.type == 'Vinyl' }}
|
36
|
+
rule { name == 'Miller' }
|
37
|
+
}
|
38
|
+
|
39
|
+
@group.sort!
|
40
|
+
|
41
|
+
assert_equal %w( Andrew J.M. Miller Riley ),
|
42
|
+
@group.objects.map {|o| o.name },
|
43
|
+
"Sortable Mixin not working"
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_line_item_sort_mixin
|
47
|
+
@group.filter { name == 'Derek' }
|
48
|
+
|
49
|
+
@group.sort(:line_items) {
|
50
|
+
rule { type == 'Vinyl' }
|
51
|
+
rule { type == 'CD' }
|
52
|
+
}
|
53
|
+
|
54
|
+
@group.sort!
|
55
|
+
|
56
|
+
assert_equal [ 4, 12, 9, 10, 11, 15, 13, 14 ],
|
57
|
+
@group.objects.first.line_items.map {|li| li.id },
|
58
|
+
"Line items are not sorting"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ShippingMaterials
|
4
|
+
self.config do |config|
|
5
|
+
config.save_path = '/Users/andrwe/shipping_materials'
|
6
|
+
config.gzip_file_name = "shipmat#{Time.now.to_i}.tar.gz"
|
7
|
+
config.s3_bucket = 'shipmaterials.gelaskins.com'
|
8
|
+
config.s3_access_key = ENV['AWS_SECRET_ACCESS_KEY']
|
9
|
+
config.s3_secret = ENV['AWS_ACCESS_KEY']
|
10
|
+
end
|
11
|
+
|
12
|
+
class PackagerTest < TestCase
|
13
|
+
def setup
|
14
|
+
@packager = Packager.new
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_dsl
|
18
|
+
@packager.package orders do
|
19
|
+
pdf './test/files/template.mustache'
|
20
|
+
|
21
|
+
sort(:line_items) {
|
22
|
+
rule { type == 'Vinyl' }
|
23
|
+
}
|
24
|
+
|
25
|
+
group 'canada_standard_post' do
|
26
|
+
filter { shipping_method == 'std' && country == 'CA' }
|
27
|
+
|
28
|
+
csv :headers => true do
|
29
|
+
row :order_id => :id,
|
30
|
+
:name => :name,
|
31
|
+
:static_fields => 'Use a string'
|
32
|
+
|
33
|
+
row :line_items => [ 'H', :id, :name, :quantity, :price ]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
group 'international_ups_expedited' do
|
38
|
+
filter { shipping_method == 'UPSexp' && !%w(US CA).include?(country) }
|
39
|
+
|
40
|
+
csv :entension => 'txt' do
|
41
|
+
row [ :id, :name ]
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
group 'all_canadian_orders' do
|
46
|
+
filter { country == 'CA' }
|
47
|
+
|
48
|
+
csv do
|
49
|
+
row [ :id, :name ]
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
group 'all_us_orders' do
|
54
|
+
filter { country == 'US' }
|
55
|
+
|
56
|
+
csv do
|
57
|
+
row [ :id, :name ]
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
ShippingMaterials.config do |config|
|
4
|
+
config.use_s3 = true
|
5
|
+
config.s3_bucket = 'shipmaterials.gelaskins.com'
|
6
|
+
config.s3_access_key = ENV['AWS_SECRET_ACCESS_KEY']
|
7
|
+
config.s3_secret = ENV['AWS_ACCESS_KEY']
|
8
|
+
end
|
9
|
+
|
10
|
+
module ShippingMaterials
|
11
|
+
class S3Test < TestCase
|
12
|
+
def test_s3_write
|
13
|
+
html = '<html><body><h1>HELLO MR WORLD<h1></body></html>'
|
14
|
+
Storage.write_pdf('canadian_shipping', html)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ShippingMaterials
|
4
|
+
class SorterTest < TestCase
|
5
|
+
def setup
|
6
|
+
@sorter = ShippingMaterials::Sorter.new
|
7
|
+
end
|
8
|
+
|
9
|
+
def dereks_order
|
10
|
+
orders.detect {|o| o.name == 'Derek' }
|
11
|
+
end
|
12
|
+
|
13
|
+
def sort_derek(line_items = false)
|
14
|
+
if line_items
|
15
|
+
@sorter.sort(self.dereks_order.line_items)
|
16
|
+
else
|
17
|
+
@sorter.sort(self.dereks_order)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def add_multiple_rules
|
22
|
+
@sorter.rule { type == 'CD' }
|
23
|
+
@sorter.rule { type == 'Cassette' }
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_sort
|
27
|
+
add_multiple_rules
|
28
|
+
|
29
|
+
sorted = sort_derek(true)
|
30
|
+
|
31
|
+
assert_equal [9, 10, 11, 15, 13, 14],
|
32
|
+
sorted.slice(0, 6).map {|o| o.id },
|
33
|
+
"Simple one-rule sort didn't seem to work"
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_callbacks
|
37
|
+
add_multiple_rules
|
38
|
+
|
39
|
+
@sorter.each_by(:quantity)
|
40
|
+
|
41
|
+
sorted = sort_derek(true)
|
42
|
+
|
43
|
+
assert_equal [15, 10, 9, 11],
|
44
|
+
sorted.slice(0, 4).map {|o| o.id },
|
45
|
+
"Callback sort is not working"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ShippingMaterials
|
4
|
+
class StorageTest < TestCase
|
5
|
+
def setup
|
6
|
+
@filename = 'hello_world'
|
7
|
+
@content = 'Hello Mr. World'
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_write_file
|
11
|
+
filename = @filename + '.txt'
|
12
|
+
|
13
|
+
ShippingMaterials::Storage.write_file(filename, @content)
|
14
|
+
|
15
|
+
full_path = "#{ShippingMaterials::Config.save_path}/#{filename}"
|
16
|
+
assert File.exists?(full_path), "File did not get saved"
|
17
|
+
|
18
|
+
if File.exists?(full_path)
|
19
|
+
assert_equal @content, File.read(full_path),
|
20
|
+
"File contents were not properly written"
|
21
|
+
|
22
|
+
File.unlink(full_path)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_write_pdf
|
27
|
+
filename = @filename + '.pdf'
|
28
|
+
|
29
|
+
ShippingMaterials::Storage.write_pdf(filename, @content)
|
30
|
+
|
31
|
+
full_path = "#{ShippingMaterials::Config.save_path}/#{filename}"
|
32
|
+
assert File.exists?(full_path), "PDF did not get saved"
|
33
|
+
|
34
|
+
File.unlink(full_path) if File.exists?(full_path)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|