Class: Zanders::Item

Inherits:
SoapClient show all
Defined in:
lib/zanders/item.rb

Constant Summary

Constants inherited from SoapClient

SoapClient::NAMESPACES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

connect

Constructor Details

#initialize(options = {}) ⇒ Item

Public: Initialize a new Item interface with username and password

options - Hash of username and password

Returns a new Item service interface



13
14
15
16
17
18
19
# File 'lib/zanders/item.rb', line 13

def initialize(options = {})
  requires!(options, :username, :password)

  @username = options[:username]
  @password = options[:password]
  @testing  = options.fetch(:testing, false)
end

Instance Attribute Details

#passwordObject (readonly)

Returns the value of attribute password.



5
6
7
# File 'lib/zanders/item.rb', line 5

def password
  @password
end

#usernameObject (readonly)

Returns the value of attribute username.



4
5
6
# File 'lib/zanders/item.rb', line 4

def username
  @username
end

Instance Method Details

#get_info(item_number) ⇒ Object

Public: Get item info by item number

item_number - Integer of the item number

Returns Hash containing item info, or an error code if the call failed



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
55
56
57
58
59
60
61
# File 'lib/zanders/item.rb', line 27

def get_info(item_number)
  request_data = build_request_data({ itemnumber: item_number })

  response = soap_client(ITEM_API_URL).call(:get_item_info, message: request_data)
  response = response.body[:get_item_info_response][:return][:item]

  # Successful call return_code is 0
  if response.first[:value] == "0"
    info = Hash.new

    # Let's toss the data we need into a ruby-ish hash
    response.each do |info_part|
      case info_part[:key]
      when  "itemNumber"
        info[:item_number] = info_part[:value]
      when "itemDescription"
        info[:description] = info_part[:value]
      when "itemPrice"
        info[:price] = info_part[:value]
      when "itemWeight"
        info[:weight] = info_part[:value]
      when "numberAvailable"
        info[:quantity] = info_part[:value]
      else
        next
      end
    end

    info[:success] = true

    info
  else
    { success: false, error_code: response.first[:value] }
  end
end

#get_quantity(item_number) ⇒ Object

Public: Get item quantity by item number

item_number - Integer of the item number

Returns Hash containing item quantity, or an error code if the call failed



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/zanders/item.rb', line 69

def get_quantity(item_number)
  request_data = build_request_data({ itemnumber: item_number })

  response = soap_client(ITEM_API_URL).call(:get_item_inventory, message: request_data)
  response = response.body[:get_item_inventory_response][:return][:item]

  # Successful call return_code is 0
  if response.first[:value] == "0"
    quantity = Hash.new

    # We only need the quantity out of the data received
    quantity[:quantity] = response.find { |i| i[:key] == "numberAvailable" }[:value].to_i
    quantity[:success]  = true

    quantity
  else
    { success: false, error_code: response.first[:value] }
  end
end