Class: Jensen::Product

Inherits:
Flowlink::Product
  • Object
show all
Defined in:
lib/jensen.rb

Defined Under Namespace

Classes: NoPriceError, ProductError

Instance Method Summary collapse

Constructor Details

#initialize(product) ⇒ Product

Returns a new instance of Product.



9
10
11
# File 'lib/jensen.rb', line 9

def initialize(product)
  @product = product.to_hash
end

Instance Method Details

#brandObject



61
62
63
# File 'lib/jensen.rb', line 61

def brand
  @product['brand']
end

#cost_priceObject



41
42
43
# File 'lib/jensen.rb', line 41

def cost_price
  @product['cost'].to_f
end

#descriptionObject



25
26
27
# File 'lib/jensen.rb', line 25

def description
  @product['description'].to_s
end

#idObject



13
14
15
# File 'lib/jensen.rb', line 13

def id
  @product['upc'].to_s
end

#imagesObject



91
92
93
94
95
96
97
# File 'lib/jensen.rb', line 91

def images
  image_fields = @product.select { |(k, _)| k =~ /\Aimage_.+/ }
  group_by_index(image_fields)
    .map { |g| Hash[g.map { |k, v| [rename_image_key(k), v] }] }
    .reject { |image| image['title'].nil? || image['title'].empty? } # Should be? #to_f#zero?
    .reject { |image| image['url'].nil? || image['url'].empty? }
end

#meta_descriptionObject



49
50
51
# File 'lib/jensen.rb', line 49

def meta_description
  nil
end

#meta_keywordsObject



53
54
55
# File 'lib/jensen.rb', line 53

def meta_keywords
  nil
end

#nameObject



17
18
19
# File 'lib/jensen.rb', line 17

def name
  @product['title'].to_s
end

#optionsObject



69
70
71
72
# File 'lib/jensen.rb', line 69

def options
  # There are no Jensen options, so just return empty array
  []
end


45
46
47
# File 'lib/jensen.rb', line 45

def permalink
  name.parameterize
end

#priceObject

Raises:



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/jensen.rb', line 29

def price
  map, msrp = @product['map'], @product['msrp']

  # TODO: optional block to set price
  price = nil
  price = map unless map.to_f.zero? # assigns if map isn't: 0, 0.0, '', nil
  price ||= msrp unless msrp.to_f.zero?
  raise(NoPriceError) if price.nil?

  price.to_f
end

#propertiesObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/jensen.rb', line 74

def properties
  # return attributes merged with misc_properties, or if no attributes,
  # just misc properties
  if attributes.nil?
    misc_properties
  else
    # Because of the format of CSV input, attributes can contain keys with
    # empty or nil names. These need to be filtered out.
    # Normally I would break out anything that handles input formatting out
    # into something else, but this is a small piece that I don't think
    # will interfere with anything else. If it grows any bigger, or starts
    # showing bugs, it would be a good idea to put this elsewhere -CDL
    attributes.reject { |(k, _)| k.nil? || k.empty? }
      .merge(misc_properties)
  end
end

#shipping_categoryObject



57
58
59
# File 'lib/jensen.rb', line 57

def shipping_category
  'default'
end

#skuObject



21
22
23
# File 'lib/jensen.rb', line 21

def sku
  @product['sku'].to_s
end

#taxonsObject



65
66
67
# File 'lib/jensen.rb', line 65

def taxons
  @product['category'].split('&&').map { |group| group.split('|') }
end