Class: VendorsService::Klook::ProductService

Inherits:
BaseOperationService
  • Object
show all
Defined in:
app/services/vendors_service/klook/product_service.rb

Overview

Service to save response for Klooks Products API to json file, compress it and upload to Amazon S3

Constant Summary collapse

FILE_NAME =
'klook_products.json'.freeze
FILE_PATH =
Rails.root.join('tmp', FILE_NAME).to_s.freeze
GZIP_FILE_NAME =
"#{FILE_NAME}.gz".freeze
GZIP_FILE_PATH =
Rails.root.join('tmp', GZIP_FILE_NAME).to_s.freeze
UPLOAD_FILE_PATH =
"vendors/klook/#{GZIP_FILE_NAME}".freeze
UNIQUE_CACHE_KEY =
"#{name}:klook_products_cache".freeze

Instance Method Summary collapse

Instance Method Details

#downloadObject



26
27
28
29
30
31
32
# File 'app/services/vendors_service/klook/product_service.rb', line 26

def download
  s3 = Aws::S3::Resource.new(region: Figaro.env.aws_region!)
  target_obj = s3.bucket(Figaro.env.AWS_DOCS_BUCKET!).object(UPLOAD_FILE_PATH)
  target_obj.download_file(GZIP_FILE_PATH)
rescue StandardError => e
  handle_error(e, 'download')
end

#exportObject

find cached keys keys = $redis_lru.with { |r| r.keys(“klook_products_cache”) } delete cache $redis_lru.with { |r| r.del(*r.keys(“klook_products_cache”)) }



17
18
19
20
21
22
23
24
# File 'app/services/vendors_service/klook/product_service.rb', line 17

def export
  start_json_export
  append_restaurants_to_json
  finish_json_export
  compress_exported_json
  upload_compressed_json
  cache_gzip_data
end

#fetch_cached_dataObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/services/vendors_service/klook/product_service.rb', line 34

def fetch_cached_data
  raw = $redis_lru_ro.with { |r| r.get(UNIQUE_CACHE_KEY) }
  return raw if raw.present?

  download
  raise "File download failed: #{GZIP_FILE_PATH} does not exist" unless File.exist?(GZIP_FILE_PATH)

  gzip = File.read(GZIP_FILE_PATH)
  cache_gzip_data(gzip)
  gzip
rescue StandardError => e
  handle_error(e, 'fetch_cached_data')
  nil
end