Class: Edi::Houzz::Retriever

Inherits:
BaseService show all
Defined in:
app/services/edi/houzz/retriever.rb

Overview

I connect to a transport, retrieve files and save them in the Edi Communication log

Instance Attribute Summary

Attributes inherited from BaseService

#options

Instance Method Summary collapse

Methods inherited from BaseService

#initialize, #log_debug, #log_error, #log_info, #log_warning, #logger, #tagged_logger

Constructor Details

This class inherits a constructor from BaseService

Instance Method Details

#instantiate_transporter(transporter, transporter_profile = nil) ⇒ Object



56
57
58
59
60
61
62
63
# File 'app/services/edi/houzz/retriever.rb', line 56

def instantiate_transporter(transporter, transporter_profile = nil)
  case transporter
  when :http_api
    Transport::HttpApiConnection.new({ profile: transporter_profile })
  else
    raise "Unknown transporter: #{transporter}"
  end
end

#process(transporter: :http_api, transporter_profile: nil, remote_path:, file_pattern: nil, partner:, category:, data_type: 'json') ⇒ Object



6
7
8
9
10
11
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
# File 'app/services/edi/houzz/retriever.rb', line 6

def process(transporter: :http_api,
            transporter_profile: nil, # Transporter profile for sftp connection, see secrets.yml
            remote_path:, # Directory to pull from, usually specified in partner configuration
            file_pattern: nil, # Regexp file pattern to match
            partner:, # Name of partner for creating log entry
            category:, # Category for log entry
            data_type: 'json')
  transport = instantiate_transporter(transporter, transporter_profile)
  batch_data = transport.batch_download_data(remote_path, file_pattern)
  logger.warn "No new files found at #{remote_path} with pattern: #{file_pattern}" if batch_data.blank?
  edi_logs = []
  batch_data.each do |file_name, data|
    next if data.blank?

    begin
      parsed = JSON.parse(data)
    rescue JSON::ParserError => e
      # Houzz intermittently serves an HTML error page ("Oops! We're
      # experiencing some technical issues") instead of JSON. Skip the
      # file rather than aborting the whole order flow.
      logger.warn "Edi::Houzz::Retriever: skipping #{file_name} — response was not valid JSON (#{e.message.truncate(120)})"
      next
    end
    unless parsed.is_a?(Hash)
      # Syntactically-valid JSON that isn't an object (e.g. `[]` or a
      # bare scalar) would NoMethodError on `with_indifferent_access`.
      # Skip it the same way as a parse failure.
      logger.warn "Edi::Houzz::Retriever: skipping #{file_name} — top-level JSON was #{parsed.class}, expected object"
      next
    end
    json_hash = parsed.with_indifferent_access
    next unless json_hash[:Orders]&.any? # skip files with no records to process

    EdiCommunicationLog.transaction do
      edi_log = EdiCommunicationLog.new(partner: partner,
                                        category: category,
                                        data: json_hash.to_json,
                                        data_type: data_type,
                                        file_name: file_name,
                                        transmit_datetime: Time.current)

      edi_log.save!

      logger.info "#{file_name} saved to edi log #{edi_log.id}"
      edi_logs << edi_log
    end
  end
  edi_logs
end