Class: BorrowDirect::RequestItem

Inherits:
Request
  • Object
show all
Defined in:
lib/borrow_direct/request_item.rb

Overview

The BorrowDirect RequestItem service, for placing a request borrowdirect.pbworks.com/w/file/86126056/RequestItem.docx

You can also use #find_item_request to get the raw BD response as a ruby hash

Constant Summary collapse

@@api_path =
"/dws/item/add"
@@valid_search_types =
%w{ISBN ISSN LCCN OCLC Control }

Instance Attribute Summary collapse

Attributes inherited from Request

#auth_id, #expected_error_codes, #http_client, #http_method, #last_request_json, #last_request_response, #last_request_time, #last_request_uri, #timeout

Instance Method Summary collapse

Methods inherited from Request

#fetch_auth_id!, #need_auth_id, #request, #request_headers, #with_auth_id

Constructor Details

#initialize(patron_barcode, patron_library_symbol = Defaults.library_symbol) ⇒ RequestItem

Returns a new instance of RequestItem.



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/borrow_direct/request_item.rb', line 18

def initialize(patron_barcode,
               patron_library_symbol = Defaults.library_symbol)
  super(@@api_path)

  @patron_barcode        = patron_barcode
  @patron_library_symbol = patron_library_symbol

  # BD sometimes unpredictably returns one of these errors when it means
  # "no results", other times it doesn't. We don't want to raise on it. 
  self.expected_error_codes << "PUBRI004"
  self.expected_error_codes << "PUBRI003"
end

Instance Attribute Details

#authorization_idObject (readonly)

Returns the value of attribute authorization_id.



11
12
13
# File 'lib/borrow_direct/request_item.rb', line 11

def authorization_id
  @authorization_id
end

#patron_barcodeObject (readonly)

Returns the value of attribute patron_barcode.



10
11
12
# File 'lib/borrow_direct/request_item.rb', line 10

def patron_barcode
  @patron_barcode
end

#patron_library_symbolObject (readonly)

Returns the value of attribute patron_library_symbol.



10
11
12
# File 'lib/borrow_direct/request_item.rb', line 10

def patron_library_symbol
  @patron_library_symbol
end

Instance Method Details

#make_request(pickup_location, options) ⇒ Object

Pass in a BD exact search and pickup location eg

make_request(pickup_location, :isbn => isbn)

Pass in nil for pickup_location if… not sure exactly what BD will do, but it does allow it.

Returns the BD RequestNumber, or nil if a request could not be made

See also make_request! to raise if request can not be made



81
82
83
84
85
# File 'lib/borrow_direct/request_item.rb', line 81

def make_request(pickup_location, options)
  resp = request_item_request(pickup_location, options)

  return extract_request_number(resp)
end

#make_request!(pickup_location, options) ⇒ Object

Like make_request, but will raise a BorrowDirect::Error if item can’t be requested.



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/borrow_direct/request_item.rb', line 89

def make_request!(pickup_location, options)
  resp = request_item_request(pickup_location, options)

  number = extract_request_number(resp)

  if number.nil?
    raise BorrowDirect::Error.new("Can not request for: #{options.inspect}: #{resp.inspect}")
  end

  return number
end

#request_item_request(pickup_location, options) ⇒ Object

need to send a key and value for a valid exact_search type type can be string or symbol, lowercase or uppercase.

Also a pickup_location – can pass in nil, and we’ll send no PickupLocation to BD, which it seems to accept, not sure what it does with it.

pickup_location can be a BorrowDirect::PickupLocation object, or a string. If a string, BD recommends it be a CODE returned from FindItem, rather than DESCRIPTION as in the past, but we think description still works?

Returns the actual complete BD response hash. You may want #make_request instead

finder.request_item_request(pickup_location, :isbn => "12345545456")
finder.request_item_request(pickup_location, :lccn => "12345545456")
finder.request_item_request(pickup_location, :oclc => "12345545456")


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/borrow_direct/request_item.rb', line 49

def request_item_request(pickup_location, options)
  search_type, search_value = nil, nil
  options.each_pair do |key, value|
    if @@valid_search_types.include? key.to_s.upcase
      if search_type || search_value
        raise ArgumentError.new("Only one search criteria at a time is allowed: '#{options}'")
      end

      search_type, search_value = key, value
    end
  end
  unless search_type && search_value
    raise ArgumentError.new("Missing valid search type and value: '#{options}'")
  end

  if pickup_location.kind_of?(BorrowDirect::PickupLocation)
    pickup_location = pickup_location.code
  end

  request exact_search_request_hash(pickup_location, search_type, search_value), need_auth_id(self.patron_barcode, self.patron_library_symbol)
end