Skip to content

Commit

Permalink
changes to exporter to discourage use of strings
Browse files Browse the repository at this point in the history
  • Loading branch information
chiramiso committed Apr 23, 2015
1 parent 57d66c0 commit 0694342
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 53 deletions.
62 changes: 31 additions & 31 deletions app/objects/service/belboon_article_exporter.rb
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
class BelboonArticleExporter < Exporter::CSVExporter
mapping do
field 'Merchant_ProductNumber' => 'slug'
field 'EAN_Code' => 'gtin'
field 'Product_Title' => 'title'
field 'Manufacturer' => nil
field 'Brand' => nil
field 'Price' => "Money.new(self.price_cents).to_s.gsub(',', '.')"
field 'Price_old' => nil
field 'Currency' => "'EUR'"
field 'Valid_From' => nil
field 'Valid_To' => nil
field 'DeepLink_URL' => "'https://www.fairmondo.de/articles/' + slug"
field 'Into_Basket_URL' => nil
field 'Image_Small_URL' => "'https://www.fairmondo.de/' + title_image_url(:thumb)"
field 'Image_Small_HEIGHT' => "'200'"
field 'Image_Small_WIDTH' => "'280'"
field 'Image_Large_URL' => "'https://www.fairmondo.de/' + title_image_url(:medium)"
field 'Image_Large_HEIGHT' => nil
field 'Image_Large_WIDTH' => nil
field 'Keywords' => nil
field 'Merchant_Product_Category' => nil
field 'Product_Description_Short' => nil
field 'Product_Description_Long' => 'Sanitize.clean(content)'
field 'Last_Update' => 'updated_at'
field 'Shipping' => nil
field 'Availability' => "'sofort lieferbar'"
field 'Optional_1' => 'id'
field 'Optional_2' => nil
field 'Optional_3' => nil
field 'Optional_4' => nil
field 'Optional_5' => nil
field Merchant_ProductNumber: :slug
field EAN_Code: :gtin
field Product_Title: :title
field Manufacturer: nil
field Brand: nil
field Price: -> { Money.new(self.price_cents).to_s.gsub(',', '.') }
field Price_old: nil
field Currency: 'EUR'
field Valid_From: nil
field Valid_To: nil
field DeepLink_URL: -> { 'https://www.fairmondo.de/articles/' + slug }
field Into_Basket_URL: nil
field Image_Small_URL: -> { 'https://www.fairmondo.de/' + title_image_url(:thumb) }
field Image_Small_HEIGHT: '200'
field Image_Small_WIDTH: '280'
field Image_Large_URL: -> { 'https://www.fairmondo.de/' + title_image_url(:medium) }
field Image_Large_HEIGHT: nil
field Image_Large_WIDTH: nil
field Keywords: nil
field Merchant_Product_Category: nil
field Product_Description_Short: nil
field Product_Description_Long: -> { Sanitize.clean(content) }
field Last_Update: :updated_at
field Shipping: nil
field Availability: 'sofort lieferbar'
field Optional_1: :id
field Optional_2: nil
field Optional_3: nil
field Optional_4: nil
field Optional_5: nil
end

file_path Rails.env == 'development' ?
destination Rails.env == 'development' ?
"#{ Rails.root }/public/fairmondo_articles.csv" :
'/var/www/fairnopoly/shared/public/system/fairmondo_articles.csv'
end
26 changes: 7 additions & 19 deletions app/objects/service/exporter.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Exporter
class CSVExporter
class_attribute :export_mapping, :file_name, :csv_options
class_attribute :export_mapping, :destination, :csv_options

def export &block
CSV.open(file_name, 'wb', csv_options) do |line|
Expand All @@ -19,29 +19,17 @@ def generate_line_for item
attrs = []
export_mapping.each do |_key, value|
case
when empty_or_nil_in?(value)
attrs << value
when string_or_symbol_in?(value)
begin
attrs << item.instance_eval(value.to_s)
rescue
attrs << value
end
when value.is_a?(Symbol)
attrs << item.send(value)
when value.is_a?(Proc)
attrs << item.instance_exec(&(value))
else
attrs << value
end
end
attrs
end

def empty_or_nil_in? value
value == nil || value == ''
end

def string_or_symbol_in? value
value.is_a?(String) || value.is_a?(Symbol)
end

class << self
def build
self.new
Expand All @@ -55,7 +43,7 @@ def field hash
export_mapping.merge! hash
end

def file_path string
def destination string
file_name << string
end

Expand All @@ -72,7 +60,7 @@ def export_mapping
end

def file_name
@file_name ||= ''
@destination ||= ''
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions app/objects/service/line_item_group_exporter.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
class LineItemGroupExporter < Exporter::CSVExporter
mapping do
field id: 'id'
field id: :id
field sold_at: ->{ sold_at.strftime('%d.%m.%Y %H:%M') }
field transaction_id: :purchase_id
field article_title: 'self.articles.last.title'
field article_title: ->{ self.articles.last.title }
field created_at: ->{ created_at }
field shipping_address: ->{ transport_address.instance_exec {
addr = ''
Expand All @@ -29,5 +29,5 @@ class LineItemGroupExporter < Exporter::CSVExporter
}
end

file_path "#{ Rails.root }/public/fairmondo_articles.csv"
destination "#{ Rails.root }/public/fairmondo_articles.csv"
end

0 comments on commit 0694342

Please sign in to comment.