Class: SolidusBactracs::Api::RequestRunner
- Inherits:
-
Object
- Object
- SolidusBactracs::Api::RequestRunner
- Defined in:
- lib/solidus_bactracs/api/request_runner.rb
Instance Method Summary collapse
- #authenticate! ⇒ Object
- #authenticated_call(serializer: nil, shipment: nil, method: :post, path: '/webservices/rma/rmaservice.asmx', count: 0) ⇒ Object
- #call(method: nil, path: nil, params: {}) ⇒ Object
- #clear_cache ⇒ Object
- #create_rma_success?(response) ⇒ Boolean
-
#initialize ⇒ RequestRunner
constructor
A new instance of RequestRunner.
- #parse_authentication_response(response, field) ⇒ Object
- #parse_rma_creation_response(response, field = "Result") ⇒ Object
- #rma_exists?(response) ⇒ Boolean
- #rma_fail?(response) ⇒ Boolean
- #shipment_sync_failed(shipment) ⇒ Object
- #shipment_synced(shipment) ⇒ Object
Constructor Details
#initialize ⇒ RequestRunner
Returns a new instance of RequestRunner.
9 10 11 12 13 14 |
# File 'lib/solidus_bactracs/api/request_runner.rb', line 9 def initialize @username = SolidusBactracs.configuration.authentication_username @password = SolidusBactracs.configuration.authentication_password @api_base = SolidusBactracs.configuration.api_base @retries = SolidusBactracs.configuration.api_retries end |
Instance Method Details
#authenticate! ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/solidus_bactracs/api/request_runner.rb', line 81 def authenticate! unless @username.present? || @password.present? || @api_base.present? raise "Credentials not defined for Authentication" end @response = Rails.cache.fetch("backtracks_cache_key", expires_in: 1.hour, skip_nil: true) do self.call(method: :get, path: "/webservices/user/Authentication.asmx/Login?sUserName=#{@username}&sPassword=#{@password}") end raise RequestError.from_response(@response) unless @response # just try again for @retries? if "false" == parse_authentication_response(@response, "Result") Rails.logger.error({ event: 'bactracs auth failed', error: parse_authentication_response(@response, "Message")}) raise RequestError.from_response(@response) end sguid = parse_authentication_response(@response, "Message") return sguid end |
#authenticated_call(serializer: nil, shipment: nil, method: :post, path: '/webservices/rma/rmaservice.asmx', count: 0) ⇒ Object
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 |
# File 'lib/solidus_bactracs/api/request_runner.rb', line 16 def authenticated_call(serializer: nil, shipment: nil, method: :post, path: '/webservices/rma/rmaservice.asmx', count: 0) if count <= @retries sguid = authenticate! rescue nil if !sguid.presence clear_cache count += 1 self.authenticated_call(method: method, path: path, serializer: serializer, shipment: shipment, count: count) else params = serializer.call(shipment, sguid) rma_response = call(method: method, path: path, params: params) if create_rma_success?(rma_response) Rails.logger.info({ event: 'success CreateRMA', rma: shipment.number, response: parse_rma_creation_response(rma_response, "Message")}) shipment_synced(shipment) return true elsif rma_exists?(rma_response) or rma_fail?(rma_response) return false else clear_cache count += 1 Rails.logger.warn({ event: 'bactracs failed CreateRMA', error: parse_rma_creation_response(rma_response, "Message")}) self.authenticated_call(method: method, path: path, serializer: serializer, shipment: shipment, count: count) end end else shipment_sync_failed(shipment) return false end end |
#call(method: nil, path: nil, params: {}) ⇒ Object
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 |
# File 'lib/solidus_bactracs/api/request_runner.rb', line 48 def call(method: nil, path: nil, params: {}) doc = {} if params.present? doc = Nokogiri::XML(params.to_s) end response = HTTParty.send( method, URI.join(@api_base, path), body: doc.to_xml, http_proxyaddr: SolidusBactracs.configuration.proxy_address, http_proxyport: SolidusBactracs.configuration.proxy_port, http_proxyuser: SolidusBactracs.configuration.proxy_username, http_proxypass: SolidusBactracs.configuration.proxy_password, headers: { 'Content-Type' => 'text/xml', }, ) case response.code.to_s when /2\d{2}/ response when '429' raise RateLimitedError.from_response(response) else raise RequestError.from_response(response) end end |
#clear_cache ⇒ Object
76 77 78 79 |
# File 'lib/solidus_bactracs/api/request_runner.rb', line 76 def clear_cache Rails.cache.delete('bactracks_cache_key') @response = nil end |
#create_rma_success?(response) ⇒ Boolean
108 109 110 |
# File 'lib/solidus_bactracs/api/request_runner.rb', line 108 def create_rma_success?(response) parse_rma_creation_response(response) == 'true' && parse_rma_creation_response(response, "Message") == "ok" end |
#parse_authentication_response(response, field) ⇒ Object
100 101 102 |
# File 'lib/solidus_bactracs/api/request_runner.rb', line 100 def parse_authentication_response(response, field) response.dig("AuthenticationResponse", field) end |
#parse_rma_creation_response(response, field = "Result") ⇒ Object
104 105 106 |
# File 'lib/solidus_bactracs/api/request_runner.rb', line 104 def parse_rma_creation_response(response, field = "Result") response.dig("Envelope", "Body", "CreateNewResponse", "CreateNewResult", field).to_s.downcase end |
#rma_exists?(response) ⇒ Boolean
112 113 114 115 116 117 |
# File 'lib/solidus_bactracs/api/request_runner.rb', line 112 def rma_exists?(response) if parse_rma_creation_response(response, "Message").match(/failed CreateRMA/) Rails.logger.error({ event: 'bactracs failed CreateRMA', error: parse_rma_creation_response(response, "Message")}) return true end end |
#rma_fail?(response) ⇒ Boolean
119 120 121 122 123 124 |
# File 'lib/solidus_bactracs/api/request_runner.rb', line 119 def rma_fail?(response) if parse_rma_creation_response(response, "Message").match(/rma .* already exists/) Rails.logger.error({ event: 'bactracs failed CreateRMA', error: parse_rma_creation_response(response, "Message")}) return true end end |
#shipment_sync_failed(shipment) ⇒ Object
133 134 135 |
# File 'lib/solidus_bactracs/api/request_runner.rb', line 133 def shipment_sync_failed(shipment) ::Spree::Bus.publish(:'solidus_bactracs.api.sync_failed', shipment:) end |
#shipment_synced(shipment) ⇒ Object
127 128 129 130 131 |
# File 'lib/solidus_bactracs/api/request_runner.rb', line 127 def shipment_synced(shipment) shipment.update_attribute(:bactracs_synced_at, Time.zone.now) ::Spree::Bus.publish(:'solidus_bactracs.api.sync_completed', shipment:) end |