Class: Transport::MiraklTransporter
- Inherits:
-
Object
- Object
- Transport::MiraklTransporter
- Defined in:
- app/services/transport/mirakl_transporter.rb
Overview
HTTP transporter for Mirakl marketplace integrations
Constant Summary collapse
- VALID_HTTP_METHODS =
%w[get put post patch delete head]
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#profile ⇒ Object
readonly
Returns the value of attribute profile.
Instance Method Summary collapse
-
#batch_download_data(directory, pattern = nil) ⇒ Object
In one shot returns a hash, hash key is filename, hash value is content of file.
-
#initialize(options = {}) ⇒ MiraklTransporter
constructor
A new instance of MiraklTransporter.
- #rm(remote_file_path) ⇒ Object
- #send_data(data, remote_filepath, method = 'put') ⇒ Object
- #send_file(remote_filepath, options) ⇒ Object
- #send_mirakl_data(data, remote_filepath, filename, options = {}) ⇒ Object
- #successful?(http_res) ⇒ Boolean
Constructor Details
#initialize(options = {}) ⇒ MiraklTransporter
Returns a new instance of MiraklTransporter.
9 10 11 12 13 14 15 16 17 18 19 |
# File 'app/services/transport/mirakl_transporter.rb', line 9 def initialize( = {}) @options = # if there's a profile, grab the headers to send from it if (profile = @options[:profile]) @headers = Heatwave::Configuration.fetch(profile&.to_sym) @headers = @headers.reject { |k| k == :hostname } # remove hostname which isn't used as a header end @headers ||= {} @headers = @headers.merge([:headers]) if [:headers].present? @logger = [:logger] || Rails.logger end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
7 8 9 |
# File 'app/services/transport/mirakl_transporter.rb', line 7 def logger @logger end |
#profile ⇒ Object (readonly)
Returns the value of attribute profile.
7 8 9 |
# File 'app/services/transport/mirakl_transporter.rb', line 7 def profile @profile end |
Instance Method Details
#batch_download_data(directory, pattern = nil) ⇒ Object
In one shot returns a hash, hash key is filename, hash value is content of file
77 78 79 80 81 82 83 84 85 86 |
# File 'app/services/transport/mirakl_transporter.rb', line 77 def batch_download_data(directory, pattern = nil) data = {} logger.info "Attempting connection to http endpoint: #{directory}" http_obj = HTTP @headers.each do |k, v| http_obj = http_obj.headers(k.to_s.upcase.dasherize => v) end data[pattern || 'response'] = http_obj.get(directory).to_s data end |
#rm(remote_file_path) ⇒ Object
88 89 90 |
# File 'app/services/transport/mirakl_transporter.rb', line 88 def rm(remote_file_path) # Do nothing end |
#send_data(data, remote_filepath, method = 'put') ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'app/services/transport/mirakl_transporter.rb', line 21 def send_data(data, remote_filepath, method = 'put') method = method.downcase method = 'put' unless VALID_HTTP_METHODS.include?(method) http_obj = HTTP @headers.each do |k, v| http_obj = http_obj.headers(k.to_s.upcase.dasherize => v) # here the header key goes from say "x_houzz_api_ssl_token" to "X-HOUZZ-API-SSL-TOKEN" end http_res = nil Retryable.retryable(tries: 3, sleep: lambda { |n| 4**n }, on: Retryable::TIMEOUT_CLASSES) do |attempt_number, exception| http_res = if data.present? http_obj.timeout(20).send(method, remote_filepath, json: JSON.parse(data)) else http_obj.timeout(20).send(method, remote_filepath) end end logger.debug("HTTP request complete", url: remote_filepath, method: method, status: http_res&.code) { success: successful?(http_res), http_result: http_res } end |
#send_file(remote_filepath, options) ⇒ Object
54 55 56 57 58 59 60 61 62 63 |
# File 'app/services/transport/mirakl_transporter.rb', line 54 def send_file(remote_filepath, ) http_obj = HTTP @headers.each do |k, v| http_obj = http_obj.headers(k.to_s.upcase.dasherize => v) # here the header key goes from say "x_houzz_api_ssl_token" to "X-HOUZZ-API-SSL-TOKEN" end http_res = http_obj.post(remote_filepath, form: ) logger.debug("HTTP file upload complete", url: remote_filepath, method: 'post', status: http_res&.code) { success: successful?(http_res), http_result: http_res } end |
#send_mirakl_data(data, remote_filepath, filename, options = {}) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'app/services/transport/mirakl_transporter.rb', line 40 def send_mirakl_data(data, remote_filepath, filename, = {}) http_obj = HTTP @headers.each do |k, v| http_obj = http_obj.headers(k.to_s.upcase.dasherize => v) # here the header key goes from say "x_houzz_api_ssl_token" to "X-HOUZZ-API-SSL-TOKEN" end # Build XML file to send io = StringIO.new(data) file = HTTP::FormData::File.new(io, filename:) [:file] = file http_res = http_obj.post(remote_filepath, form: ) logger.debug("HTTP Mirakl request complete", url: remote_filepath, method: 'post', status: http_res&.code) { success: successful?(http_res), http_result: http_res } end |
#successful?(http_res) ⇒ Boolean
65 66 67 68 69 70 71 72 73 74 |
# File 'app/services/transport/mirakl_transporter.rb', line 65 def successful?(http_res) res = false ack_res = true if http_res.mime_type == 'text/xml' || http_res.mime_type == 'text/csv' res = true if http_res.code >= 200 and http_res.code < 300 elsif (http_res.code >= 200 and http_res.code < 300) && ack_res res = true end res end |