jensen 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dfff673a9b75f33e3a4f694cfd5a1ef698534d3f
4
+ data.tar.gz: 34e05d08839c72002228e97faf95b2fb60dbe571
5
+ SHA512:
6
+ metadata.gz: 9855e78005c276e740f3827c3f7e4912da61ef849eadbef83622a877c9527b542d56392c1ec2c650a69df7d6b995597d392d0adab399314663f096bdbc5d0535
7
+ data.tar.gz: 126dd7792777bbfd1d90e9a715281831025409c9229e5ccd8ca06a3d052eeac39ecbf93cd7c9b0a947786ac54d9bd74490858fbc046194f1e9c114d86f4f6d28
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'csv'
4
+ require 'json'
5
+ require_relative '../lib/jensen.rb'
6
+
7
+ file = ARGF.file
8
+
9
+ # Get file path, Convert filepath -> CSV::Table
10
+ table = CSV.parse(file, headers: true, row_sep: :auto)
11
+
12
+ # CSV::Table -> Array of Jensen::Products
13
+ # There needs to be a way to weed out products with no stock.
14
+ products = table.map do |row|
15
+ Jensen::Product.new(row)
16
+ end
17
+
18
+ # Jenson::Products -> JSON text
19
+ json = products.map!(&:to_message).to_json
20
+
21
+ print json
@@ -0,0 +1,144 @@
1
+ require 'flowlink_data'
2
+ require 'active_support/inflector'
3
+
4
+ module Jensen
5
+ class Product < Flowlink::Product
6
+ class ProductError < StandardError; end
7
+ class NoPriceError < ProductError; end
8
+
9
+ def initialize(product)
10
+ @product = product.to_hash
11
+ end
12
+
13
+ def id
14
+ @product['upc'].to_s
15
+ end
16
+
17
+ def name
18
+ @product['title'].to_s
19
+ end
20
+
21
+ def sku
22
+ @product['sku'].to_s
23
+ end
24
+
25
+ def description
26
+ @product['description'].to_s
27
+ end
28
+
29
+ def price
30
+ map, msrp = @product['map'], @product['msrp']
31
+
32
+ # TODO: optional block to set price
33
+ price = nil
34
+ price = map unless map.to_f.zero? # assigns if map isn't: 0, 0.0, '', nil
35
+ price ||= msrp unless msrp.to_f.zero?
36
+ raise(NoPriceError) if price.nil?
37
+
38
+ price.to_f
39
+ end
40
+
41
+ def cost_price
42
+ @product['cost'].to_f
43
+ end
44
+
45
+ def permalink
46
+ name.parameterize
47
+ end
48
+
49
+ def meta_description
50
+ nil
51
+ end
52
+
53
+ def meta_keywords
54
+ nil
55
+ end
56
+
57
+ def shipping_category
58
+ 'default'
59
+ end
60
+
61
+ def brand
62
+ @product['brand']
63
+ end
64
+
65
+ def taxons
66
+ @product['category'].split('&&').map { |group| group.split('|') }
67
+ end
68
+
69
+ def options
70
+ # There are no Jensen options, so just return empty array
71
+ []
72
+ end
73
+
74
+ def properties
75
+ # return attributes merged with misc_properties, or if no attributes,
76
+ # just misc properties
77
+ if attributes.nil?
78
+ misc_properties
79
+ else
80
+ # Because of the format of CSV input, attributes can contain keys with
81
+ # empty or nil names. These need to be filtered out.
82
+ # Normally I would break out anything that handles input formatting out
83
+ # into something else, but this is a small piece that I don't think
84
+ # will interfere with anything else. If it grows any bigger, or starts
85
+ # showing bugs, it would be a good idea to put this elsewhere -CDL
86
+ attributes.reject { |(k, _)| k.nil? || k.empty? }
87
+ .merge(misc_properties)
88
+ end
89
+ end
90
+
91
+ def images
92
+ image_fields = @product.select { |(k, _)| k =~ /\Aimage_.+/ }
93
+ group_by_index(image_fields)
94
+ .map { |g| Hash[g.map { |k, v| [rename_image_key(k), v] }] }
95
+ .reject { |image| image['title'].nil? || image['title'].empty? } # Should be? #to_f#zero?
96
+ .reject { |image| image['url'].nil? || image['url'].empty? }
97
+ end
98
+
99
+ private
100
+
101
+ def rename_image_key(image_key)
102
+ lookup = { 'reference' => 'url', 'name' => 'title' }
103
+ lookup[image_key.match(/image_(\w+)_\d+/).captures[0]]
104
+ end
105
+
106
+ def group_by_index(fields)
107
+ # returns an array of hashes
108
+ fields.group_by { |k, _| /(\d+)\z/.match(k).captures[0] }
109
+ .values.map { |group| Hash[group] }
110
+ end
111
+
112
+ def attributes
113
+ # Return attribute fields from product, formated as a proper hash
114
+ # Ugly. Works for now though
115
+ attributes = @product.select { |k, _| k =~ /attribute_.+/ }
116
+ attributes = group_by_index(attributes).map do |attribute|
117
+ name = attribute.detect { |k, _| k.include? 'name' }[1]
118
+ value = attribute.detect { |k, _| k.include? 'value' }[1]
119
+ { name => value }
120
+ end.reduce(&:merge) # Array of hashes -> Hash
121
+ end
122
+
123
+ def misc_properties
124
+ prop_list = %w(condition height length width dimension_units dsco_create_date
125
+ dsco_item_id dsco_last_quantity_update_date dsco_last_update_date
126
+ dsco_product_id dsco_supplier_id dsco_supplier_name weight
127
+ weight_units min_purchase_quantity status quantity_available
128
+ quantity_on_order mpn)
129
+ # List -> Hash -> Hash
130
+ @product.select do |k, _|
131
+ prop_list.include?(k)
132
+ end
133
+ end
134
+
135
+ def available_on
136
+ Date.today.iso8601 # could use #to_s, but format is explicit this way.
137
+ end
138
+
139
+ def variants
140
+ # The Jensen product data doesn't have any sort of variants field.
141
+ []
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,3 @@
1
+ module Jensen
2
+ VERSION = '0.2.3'
3
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jensen
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.3
5
+ platform: ruby
6
+ authors:
7
+ - Cooper LeBrun
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-04-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: flowlink_data
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4'
41
+ description: 'Feed hashable product objects into Jensen::Product.new, get Flowlink
42
+ product hashes with #to_message.'
43
+ email:
44
+ - cooperlebrun@gmail.com
45
+ executables:
46
+ - convert_csv.rb
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - bin/convert_csv.rb
51
+ - lib/jensen.rb
52
+ - lib/jensen/version.rb
53
+ homepage: https://github.com/aokpower/jensen
54
+ licenses:
55
+ - MIT
56
+ metadata: {}
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubyforge_project:
73
+ rubygems_version: 2.5.1
74
+ signing_key:
75
+ specification_version: 4
76
+ summary: Get flowlink data from Jensen info.
77
+ test_files: []
78
+ has_rdoc: