Class: Feeds::FacebookRestaurantsWorker

Inherits:
ApplicationWorker
  • Object
show all
Defined in:
app/workers/feeds/facebook_restaurants_worker.rb

Overview

typed: ignore frozen_string_literal: true

Constant Summary collapse

FILE_NAME =
'facebook_catalog.tsv'.freeze
FILE_PATH =
Rails.root.join('tmp', FILE_NAME).to_s.freeze
DESTINATION_FILE =
"#{Rails.env}_#{FILE_NAME}".freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.downloadObject



82
83
84
85
86
# File 'app/workers/feeds/facebook_restaurants_worker.rb', line 82

def self.download
  s3 = Aws::S3::Resource.new(region: Figaro.env.aws_region!)
  target_obj = s3.bucket(Figaro.env.AWS_DOCS_BUCKET!).object(DESTINATION_FILE)
  target_obj.download_file(FILE_PATH)
end

Instance Method Details

#performObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/workers/feeds/facebook_restaurants_worker.rb', line 12

def perform
  restaurants = [
    [
      'id',
      'title',
      'description',
      'availability',
      'condition',
      'price',
      'link',
      'image_link',
      'brand',
      'google_product_category'
    ]
  ]

  Restaurant.not_expired.active.find_each do |restaurant|
    operation = RestaurantCpt::Operations::Seo.call('restaurant' => restaurant)
    obj = operation.success? ? operation['result'] : nil
    next if obj.nil?

    id = obj[:product][:retailer_item_id]
    title = obj[:title].first(150).titleize.strip_html_tags.presence || nil
    description = obj[:description].to_s.strip_html_tags.gsub("\n", " ").presence || nil
    link = obj[:canonical].presence || nil
    image_link = obj[:image_src].presence || nil

    # skip if there is required field missing
    attrs = [id, title, description, link, image_link, obj[:product][:"price:amount"]]
    required_fields = attrs.select do |attr|
      case attr
      when String
        attr.present?
      when Integer, BigDecimal
        attr > 0
      else
        false
      end
    end

    next if required_fields.count < attrs.count
    # end skip

    price = "#{obj[:product][:"price:amount"]} #{obj[:product][:"price:currency"]}"

    restaurants.push([
      id,
      title,
      description,
      obj[:product][:availability],
      obj[:product][:condition],
      price,
      link,
      image_link,
      'Hungryhub',
      'Business & Industrial > Food Service'
    ])
  end


  File.open(FILE_PATH, 'w') do |f|
    f.write(restaurants.map { |r| r.join("\t") }.join("\n"))
    f.close
  end

  s3 = Aws::S3::Resource.new(region: Figaro.env.aws_region!)
  target_obj = s3.bucket(Figaro.env.AWS_DOCS_BUCKET!).object(DESTINATION_FILE)
  target_obj.upload_file(FILE_PATH)
end