Class: SolidusBacktracs::Api::RequestRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/solidus_backtracs/api/request_runner.rb

Instance Method Summary collapse

Constructor Details

#initializeRequestRunner

Returns a new instance of RequestRunner.



9
10
11
12
13
14
# File 'lib/solidus_backtracs/api/request_runner.rb', line 9

def initialize
  @username = SolidusBacktracs.configuration.authentication_username
  @password = SolidusBacktracs.configuration.authentication_password
  @api_base = SolidusBacktracs.configuration.api_base
  @retries  = SolidusBacktracs.configuration.api_retries
end

Instance Method Details

#authenticated_call(method: nil, path: nil, serializer: nil, shipment: nil, 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
46
47
48
49
# File 'lib/solidus_backtracs/api/request_runner.rb', line 16

def authenticated_call(method: nil, path: nil, serializer: nil, shipment: nil, count: 0)
  if count <= @retries
    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?
    authenticted = parse_authentication_response(@response, "Result")
    raise RequestError.from_response(@response) if authenticted == "false"
    sguid = parse_authentication_response(@response, "Message")

    if authenticted == 'false'
      clear_cache
      raise "User Not Authenticated"
    else
      sguid = parse_authentication_response(@response, "Message")
      params = serializer.call(shipment, sguid)

      rma_response = call(method: :post, path: path, params: params)
      unless parse_rma_creation_response(rma_response) == 'true'
        clear_cache
        count += 1
        self.authenticated_call(method: :post, path: '/webservices/rma/rmaservice.asmx', serializer: serializer, shipment: shipment, count: count)
      end
      shipment_synced(shipment)
    end
  else
    shipment_sync_failed(shipment)
  end
end

#call(method: nil, path: nil, params: {}) ⇒ Object



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
# File 'lib/solidus_backtracs/api/request_runner.rb', line 51

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: SolidusBacktracs.configuration.proxy_address,
    http_proxyport: SolidusBacktracs.configuration.proxy_port,
    http_proxyuser: SolidusBacktracs.configuration.proxy_username,
    http_proxypass: SolidusBacktracs.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_cacheObject



79
80
81
82
# File 'lib/solidus_backtracs/api/request_runner.rb', line 79

def clear_cache
  Rails.cache.delete('backtracks_cache_key')
  @response = nil
end

#parse_authentication_response(response, type) ⇒ Object



84
85
86
# File 'lib/solidus_backtracs/api/request_runner.rb', line 84

def parse_authentication_response(response, type)
  response.dig("AuthenticationResponse", type)
end

#parse_rma_creation_response(response) ⇒ Object



88
89
90
# File 'lib/solidus_backtracs/api/request_runner.rb', line 88

def parse_rma_creation_response(response)
  response.dig("Envelope", "Body", "CreateNewResponse", "CreateNewResult", "Result")
end

#shipment_sync_failed(shipment) ⇒ Object



101
102
103
104
105
106
# File 'lib/solidus_backtracs/api/request_runner.rb', line 101

def shipment_sync_failed(shipment)
  ::Spree::Event.fire(
    'solidus_backtracs.api.sync_failed',
    shipment: shipment
  )
end

#shipment_synced(shipment) ⇒ Object



92
93
94
95
96
97
98
99
# File 'lib/solidus_backtracs/api/request_runner.rb', line 92

def shipment_synced(shipment)
  shipment.update_column(:backtracs_synced_at, Time.zone.now)

  ::Spree::Event.fire(
    'solidus_backtracs.api.sync_completed',
    shipment: shipment
  )
end