Module: WorldpacApi

Defined in:
lib/worldpac_api.rb,
lib/worldpac_api/version.rb

Overview

WorldpacApi

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
'1.0.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.invoice_details_keysObject

Returns the value of attribute invoice_details_keys.



24
25
26
# File 'lib/worldpac_api.rb', line 24

def invoice_details_keys
  @invoice_details_keys
end

.max_delayObject

Returns the value of attribute max_delay.



24
25
26
# File 'lib/worldpac_api.rb', line 24

def max_delay
  @max_delay
end

.max_triesObject

Returns the value of attribute max_tries.



24
25
26
# File 'lib/worldpac_api.rb', line 24

def max_tries
  @max_tries
end

.redis_configObject

Returns the value of attribute redis_config.



24
25
26
# File 'lib/worldpac_api.rb', line 24

def redis_config
  @redis_config
end

.required_invoice_keysObject

Returns the value of attribute required_invoice_keys.



24
25
26
# File 'lib/worldpac_api.rb', line 24

def required_invoice_keys
  @required_invoice_keys
end

.run_worldpac_api_locallyObject

Returns the value of attribute run_worldpac_api_locally.



24
25
26
# File 'lib/worldpac_api.rb', line 24

def run_worldpac_api_locally
  @run_worldpac_api_locally
end

.tomcat_domainObject

Returns the value of attribute tomcat_domain.



24
25
26
# File 'lib/worldpac_api.rb', line 24

def tomcat_domain
  @tomcat_domain
end

.wpc_download_url_combinedObject

Returns the value of attribute wpc_download_url_combined.



24
25
26
# File 'lib/worldpac_api.rb', line 24

def wpc_download_url_combined
  @wpc_download_url_combined
end

.wpc_download_url_detailsObject

Returns the value of attribute wpc_download_url_details.



24
25
26
# File 'lib/worldpac_api.rb', line 24

def wpc_download_url_details
  @wpc_download_url_details
end

.wpc_loginObject

Returns the value of attribute wpc_login.



24
25
26
# File 'lib/worldpac_api.rb', line 24

def 
  @wpc_login
end

.wpc_login_url_invoiceObject

Returns the value of attribute wpc_login_url_invoice.



24
25
26
# File 'lib/worldpac_api.rb', line 24

def 
  @wpc_login_url_invoice
end

.wpc_login_url_returnsObject

Returns the value of attribute wpc_login_url_returns.



24
25
26
# File 'lib/worldpac_api.rb', line 24

def 
  @wpc_login_url_returns
end

.wpc_main_orders_urlObject

Returns the value of attribute wpc_main_orders_url.



24
25
26
# File 'lib/worldpac_api.rb', line 24

def wpc_main_orders_url
  @wpc_main_orders_url
end

.wpc_passwordObject

Returns the value of attribute wpc_password.



24
25
26
# File 'lib/worldpac_api.rb', line 24

def wpc_password
  @wpc_password
end

.wpc_rma_reason_coreObject

Returns the value of attribute wpc_rma_reason_core.



24
25
26
# File 'lib/worldpac_api.rb', line 24

def wpc_rma_reason_core
  @wpc_rma_reason_core
end

.wpc_rma_reason_not_neededObject

Returns the value of attribute wpc_rma_reason_not_needed.



24
25
26
# File 'lib/worldpac_api.rb', line 24

def wpc_rma_reason_not_needed
  @wpc_rma_reason_not_needed
end

.wpc_specific_orders_urlObject

Returns the value of attribute wpc_specific_orders_url.



24
25
26
# File 'lib/worldpac_api.rb', line 24

def wpc_specific_orders_url
  @wpc_specific_orders_url
end

.wpc_url_authorized_returnsObject

Returns the value of attribute wpc_url_authorized_returns.



24
25
26
# File 'lib/worldpac_api.rb', line 24

def wpc_url_authorized_returns
  @wpc_url_authorized_returns
end

.wpc_url_rma_createObject

Returns the value of attribute wpc_url_rma_create.



24
25
26
# File 'lib/worldpac_api.rb', line 24

def wpc_url_rma_create
  @wpc_url_rma_create
end

.wpc_url_rma_searchObject

Returns the value of attribute wpc_url_rma_search.



24
25
26
# File 'lib/worldpac_api.rb', line 24

def wpc_url_rma_search
  @wpc_url_rma_search
end

.wpc_url_tplObject

Returns the value of attribute wpc_url_tpl.



24
25
26
# File 'lib/worldpac_api.rb', line 24

def wpc_url_tpl
  @wpc_url_tpl
end

Class Method Details

.bulk_fetch_rmas(accounts) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/worldpac_api.rb', line 124

def bulk_fetch_rmas(accounts)
  response = []
   = [WorldpacApi.]
   += accounts
  .each_with_index do |, idx|
    begin
      puts "#{idx + 1}: Downloading for Account: #{}"
      cookies = session_cookies(
        WorldpacApi.,
        user_id: ,
        delay: 2
      )
      resp = RestClient.get(WorldpacApi.wpc_url_authorized_returns, cookies: cookies)
      response += JSON.parse(resp.body) if resp.code == 200
    rescue StandardError => e
      puts "#{e.message}\n#{e.backtrace.join("\n")}"
      Bugsnag.notify(e.message)
    end
  end
  response
end

.bulk_update_arrival_info(order_tracking_id) ⇒ Object



146
147
148
149
150
151
152
153
154
# File 'lib/worldpac_api.rb', line 146

def bulk_update_arrival_info(order_tracking_id)
  execute_with_retry do |delay|
    cookies = session_cookies(WorldpacApi.wpc_main_orders_url, delay: delay)
    order_url = format(WorldpacApi.wpc_specific_orders_url, order_num: order_tracking_id)
    html = RestClient.get(order_url, cookies: cookies)
    doc = Nokogiri::HTML(html)
    doc.xpath('//span[@class="truck-leave-time"]')
  end
end

.config {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (WorldpacApi)

    the object that the method was called on



33
34
35
# File 'lib/worldpac_api.rb', line 33

def config
  yield self
end

.create_wpc_order(data) ⇒ Object



192
193
194
# File 'lib/worldpac_api.rb', line 192

def create_wpc_order(data)
  RestClient.post(tomcat_url('/YMWorldpacApi/orders/create'), JSON.generate(data))
end

.download_invoice(account_nums, wpc_supplier_id, supplier_id, date, days = 0) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/worldpac_api.rb', line 37

def download_invoice(, wpc_supplier_id, supplier_id, date, days = 0)
  invoices = []
   = [WorldpacApi.]
   =  if supplier_id == wpc_supplier_id
  .each_with_index do |, idx|
    execute_with_retry(raise_error: true) do |delay|
      puts "#{idx + 1}: Downloading for Account: #{}"
      cookies = session_cookies(
        WorldpacApi.,
        user_id: ,
        delay: delay
      )
      page_url = format_url_wpc_invoice(date, days)
      RestClient.get(page_url, cookies: cookies)
      resp = RestClient.get(WorldpacApi.wpc_download_url_combined, headers: { 'Referer' => page_url }, cookies: cookies)
      resp_details = RestClient.get(WorldpacApi.wpc_download_url_details, headers: { 'Referer' => page_url }, cookies: cookies)
      body = resp.body.split("\n")
      body_details = resp_details.body.split("\n")
      next unless body || body_details

      # this code is to cleanup the response into searchable array
      clean_body_details = []
      fields = body_details.shift.delete('"').split(',')
      fields = fields.map { |f| f.downcase.tr(' ', '_') }
      body_details.each do |b|
        parsed_b = b.gsub('",', ';').delete('"').split(';')
        clean_body_details << Hash[*fields.zip(parsed_b).flatten]
      end

      fields = body.shift.delete('"').split(',') + ['Unit Net Price', 'Unit Core Charge', 'Description', 'List Price']
      fields = fields.map { |f| f.downcase.tr(' ', '_') }
      unless WorldpacApi.required_invoice_keys.all? { |k| fields.include?(k) }
        next
      end

      body.each do |b|
        parsed_b = b.gsub('",', ';').delete('"').split(';') # #TODO: This is kind of retarded, need a better way to do this
        invoice_hash = Hash[*fields.zip(parsed_b).flatten]
        psearch = clean_body_details.find do |details_hash|
          details_hash['invoice'] == invoice_hash['invoice'] &&
            details_hash['part_number'] == invoice_hash['part_number']
        end
        if psearch
          WorldpacApi.invoice_details_keys.each do |key|
            invoice_hash[key] = psearch[key] if psearch[key]
          end
        end
        invoice_hash['account_number'] = 
        invoices << invoice_hash
      end
    end
  end
  invoices
end

.find_or_create_wpc_rma(order_code, account_number, invoice_num, reason_code, quantity, end_date, is_core_return) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/worldpac_api.rb', line 92

def find_or_create_wpc_rma(order_code, , invoice_num, reason_code, quantity, end_date, is_core_return)
  return unless 

  execute_with_retry do |delay|
    cookies = session_cookies(
      WorldpacApi.,
      user_id: ,
      delay: delay
    )
    start_date = end_date - 1.year
    partnum = nil
    if order_code
      split_oc = order_code.split(':')
      partnum = split_oc.length > 2 ? split_oc[2] : split_oc[0]
    end
    return unless invoice_num

    page_url = format_url_rma_search(reason_code, start_date, end_date, partnum)
    # query whether part is eligible for RMA
    resp = RestClient.get(page_url, cookies: cookies)
    return unless resp.code == 200

    response = JSON.parse(resp.body)
    # check for existing RMAs or create new RMA
    if !response.is_a?(Array) && response['status'] == 'ERROR'
      find_wpc_rma(partnum, invoice_num, cookies, is_core_return)
    else
      create_wpc_rma(partnum, invoice_num, cookies, response, reason_code, quantity)
    end
  end
end

.search_wpc_part_by_number(partnum, country) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/worldpac_api.rb', line 174

def search_wpc_part_by_number(partnum, country)
  res = nil
  begin
    res = RestClient.get(tomcat_url(
                           '/YMWorldpacApi/parts/search',
                           prodCode: partnum,
                           country: country
                         ))
    return [] if res.code != 200

    res = JSON.parse(res)
  rescue StandardError
    Bugsnag.notify($ERROR_INFO)
    return []
  end
  res['quotes']
end

.update_wpc_po_tracking(order_tracking_id, country = nil) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/worldpac_api.rb', line 156

def update_wpc_po_tracking(order_tracking_id, country = nil)
  url = tomcat_url('/YMWorldpacApi/orders/track', orderID: order_tracking_id,
                                                  country: country)
  begin
    result = RestClient.get(url)
    result = JSON.parse(result.body)
    if result&.include?('trackCodes') && !result['trackCodes'].empty?
      tracking_codes = result['trackCodes']
    end
    if result&.include?('shippingStatus') && !result['shippingStatus'].empty?
      shipping_status = result['shippingStatus']
    end
    { tracking_codes: tracking_codes, shipping_status: shipping_status }
  rescue StandardError
    Bugsnag.notify($ERROR_INFO)
  end
end