Class: Skala::AlephAdapter::CreateUserHoldRequest

Inherits:
Skala::Adapter::CreateUserHoldRequest show all
Includes:
parentparent::ResolveUser
Defined in:
lib/skala/aleph_adapter/create_user_hold_request.rb

Instance Attribute Summary

Attributes inherited from Skala::Adapter::Operation

#adapter

Instance Method Summary collapse

Methods inherited from Skala::Adapter::Operation

#initialize

Constructor Details

This class inherits a constructor from Skala::Adapter::Operation

Instance Method Details

#call(username, document_number, options = {}) ⇒ Object



9
10
11
12
13
14
15
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
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/skala/aleph_adapter/create_user_hold_request.rb', line 9

def call(username, document_number, options = {})
  get_record_items_result = adapter.get_record_items(
    document_number, {
      username: username,
    }
  )

  if first_holdable_item = get_record_items_result.items.find(&:hold_request_can_be_created)
    document_base = options[:document_base] || adapter.default_document_base
    record_id = "#{document_base}#{document_number}"
    resolved_user_id = resolve_user(username)

    raw_aleph_response = adapter.restful_api.patron(resolved_user_id).record(record_id).holds.get(view: :full)
    doc = Nokogiri::XML(raw_aleph_response)

    if element = doc.at_xpath("//group/item-status[text() = '#{first_holdable_item.item_status}']/parent::*")
      group_id = element.attr("href").split("/").last
      pickup_location = element.xpath("./pickup-locations/pickup-location").first.try(:attr, "code")
      start_interest_date = Date.today.strftime("%Y%m%d")
      last_interest_date = (Date.today + 1.year).strftime("%Y%m%d")

      raw_aleph_response = adapter.restful_api.patron(resolved_user_id).record(record_id).holds(group_id).put(
        [
          "post_xml=",
          "<hold-request-parameters>",
            "<pickup-location>#{pickup_location}</pickup-location>",
            "<start-interest-date>#{start_interest_date}</start-interest-date>",
            "<last-interest-date>#{last_interest_date}</last-interest-date>",
          "</hold-request-parameters>"
        ].join # !!! do not add linebreaks or something else or it will fail
      )

      reply_code = Nokogiri::XML(raw_aleph_response).at_xpath("//reply-code").try(:content).to_i

      if [1, 2, 19, 21].include?(reply_code)
        raise adapter.class::BadRequestError
      elsif [25].include?(reply_code)
        if raw_aleph_response[/Patron has already requested this item/]
          raise self.class::AlreadyRequestedError
        else
          raise adapter.class::RequestFailedError
        end
      else
        adapter.class::CreateUserHoldRequest::Result.new(
          source: raw_aleph_response
        )
      end
    else
      raise adapter.class::BadRequestError
    end
  else
    raise adapter.class::BadRequestError
  end
end