Class: Alma::BibRequest

Inherits:
Object
  • Object
show all
Extended by:
ApiDefaults
Defined in:
lib/alma/request.rb

Direct Known Subclasses

ItemRequest

Defined Under Namespace

Classes: ItemAlreadyExists

Constant Summary collapse

REQUEST_TYPES =
%w[HOLD DIGITIZATION BOOKING]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ApiDefaults

apikey, bibs_base_path, headers, region, timeout, users_base_path

Constructor Details

#initialize(args) ⇒ BibRequest

Returns a new instance of BibRequest.



22
23
24
25
26
27
28
29
# File 'lib/alma/request.rb', line 22

def initialize(args)
  @mms_id = args.delete(:mms_id) { raise ArgumentError.new(":mms_id option must be specified to create request") }
  @user_id = args.delete(:user_id) { raise ArgumentError.new(":user_id option must be specified to create request") }
  @request_type = args.fetch(:request_type, "NOT_SPECIFIED")
  validate!(args)
  normalize!(args)
  @body = args
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



21
22
23
# File 'lib/alma/request.rb', line 21

def body
  @body
end

#mms_idObject (readonly)

Returns the value of attribute mms_id.



21
22
23
# File 'lib/alma/request.rb', line 21

def mms_id
  @mms_id
end

#request_typeObject (readonly)

Returns the value of attribute request_type.



21
22
23
# File 'lib/alma/request.rb', line 21

def request_type
  @request_type
end

#user_idObject (readonly)

Returns the value of attribute user_id.



21
22
23
# File 'lib/alma/request.rb', line 21

def user_id
  @user_id
end

Class Method Details

.submit(args) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/alma/request.rb', line 10

def self.submit(args)
  request = new(args)
  response = HTTParty.post(
    "#{bibs_base_path}/#{request.mms_id}/requests",
    query: {user_id: request.user_id},
    headers: headers,
    body: request.body.to_json
    )
    Alma::Response.new(response)
end

Instance Method Details

#additional_normalization!(args) ⇒ Object

Intended to be overridden by subclasses, allowing extra normalization logic to be provided



43
44
# File 'lib/alma/request.rb', line 43

def additional_normalization!(args)
end

#additional_validation!(args) ⇒ Object

Intended to be overridden by subclasses, allowing extra validation logic to be provided



60
61
# File 'lib/alma/request.rb', line 60

def additional_validation!(args)
end

#booking_normalization(args) ⇒ Object



89
90
91
92
93
# File 'lib/alma/request.rb', line 89

def booking_normalization(args)
  if args[:material_type].is_a? String
    args[:material_type] = { value: args[:material_type] }
  end
end

#booking_validation(args) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/alma/request.rb', line 95

def booking_validation(args)
  args.fetch(:booking_start_date) do
    raise ArgumentError.new(
    ":booking_start_date option must be specified when request_type is BOOKING"
    )
  end
  args.fetch(:booking_end_date) do
    raise ArgumentError.new(
    ":booking_end_date option must be specified when request_type is BOOKING"
    )
  end
  args.fetch(:pickup_location_type) do
    raise ArgumentError.new(
    ":pickup_location_type option must be specified when request_type is BOOKING"
    )
  end
  args.fetch(:pickup_location_library) do
    raise ArgumentError.new(
    ":pickup_location_library option must be specified when request_type is BOOKING"
    )
  end
end

#digitization_normalization(args) ⇒ Object



63
64
65
66
67
# File 'lib/alma/request.rb', line 63

def digitization_normalization(args)
  if args[:target_destination].is_a? String
    args[:target_destination] = { value: args[:target_destination] }
  end
end

#digitization_validation(args) ⇒ Object



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

def digitization_validation(args)
  args.fetch(:target_destination) do
    raise ArgumentError.new(
    ":target_destination option must be specified when request_type is DIGITIZATION"
    )
  end
  pd = args.fetch(:partial_digitization) do
    raise ArgumentError.new(
    ":partial_digitization option must be specified when request_type is DIGITIZATION"
    )
  end
  if pd == true
    args.fetch(:comment) do
      raise ArgumentError.new(
      ":comment option must be specified when :request_type is DIGITIZATION and :partial_digitization is true"
      )
    end
  end
end

#hold_normalization(args) ⇒ Object



118
119
120
121
122
# File 'lib/alma/request.rb', line 118

def hold_normalization(args)
  # if args[:material_type].is_a? String
  #   args[:material_type] = { value: args[:material_type] }
  # end
end

#hold_validation(args) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/alma/request.rb', line 124

def hold_validation(args)
  args.fetch(:pickup_location_type) do
    raise ArgumentError.new(
    ":pickup_location_type option must be specified when request_type is HOLD"
    )
  end
  args.fetch(:pickup_location_library) do
    raise ArgumentError.new(
    ":pickup_location_library option must be specified when request_type is HOLD"
    )
  end
end

#normalize!(args) ⇒ Object



32
33
34
35
# File 'lib/alma/request.rb', line 32

def normalize!(args)
  request_type_normalization!(args)
  additional_normalization!(args)
end

#request_type_normalization!(args) ⇒ Object



37
38
39
40
# File 'lib/alma/request.rb', line 37

def request_type_normalization!(args)
  method = "#{@request_type.downcase}_normalization".to_sym
  send(method, args) if respond_to? method
end

#request_type_validation!(args) ⇒ Object



54
55
56
57
# File 'lib/alma/request.rb', line 54

def request_type_validation!(args)
  method = "#{@request_type.downcase}_validation".to_sym
  send(method, args) if respond_to? method
end

#validate!(args) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/alma/request.rb', line 46

def validate!(args)
  unless REQUEST_TYPES.include?(request_type)
    raise ArgumentError.new(":request_type option must be specified and one of #{REQUEST_TYPES.join(", ")} to submit a request")
  end
  request_type_validation!(args)
  additional_validation!(args)
end