alma 0.3.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/alma/bib.rb CHANGED
@@ -1,16 +1,18 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Alma
2
4
  class Bib
3
5
  extend Alma::ApiDefaults
4
6
  extend Forwardable
5
-
7
+
6
8
  def self.find(ids, args)
7
9
  get_bibs(ids, args)
8
10
  end
9
11
 
10
- def self.get_bibs(ids, args={})
12
+ def self.get_bibs(ids, args = {})
11
13
  response = HTTParty.get(
12
- self.bibs_base_path,
13
- query: {mms_id: ids_from_array(ids) }.merge(args),
14
+ self.bibs_base_path,
15
+ query: { mms_id: ids_from_array(ids) }.merge(args),
14
16
  headers: headers,
15
17
  timeout: timeout
16
18
  )
@@ -19,12 +21,12 @@ module Alma
19
21
  Alma::BibSet.new(get_body_from(response))
20
22
  else
21
23
  raise StandardError, get_body_from(response)
22
- end
24
+ end
23
25
  end
24
26
 
25
27
 
26
- def self.get_availability(ids, args={})
27
- args.merge!({expand: 'p_avail,e_avail,d_avail'})
28
+ def self.get_availability(ids, args = {})
29
+ args.merge!({ expand: "p_avail,e_avail,d_avail" })
28
30
  bibs = get_bibs(ids, args)
29
31
 
30
32
  Alma::AvailabilityResponse.new(bibs)
@@ -39,31 +41,31 @@ module Alma
39
41
 
40
42
  def initialize(response_body)
41
43
  @response = response_body
42
- @id = @response['mms_id'].to_s
44
+ @id = @response["mms_id"].to_s
43
45
  end
44
46
 
45
47
  # The raw MARCXML record, converted to a Hash
46
48
  def record
47
- @record ||= XmlSimple.xml_in(response['anies'].first)
49
+ @record ||= XmlSimple.xml_in(response["anies"].first)
48
50
  end
49
51
 
50
52
 
51
53
  private
52
54
 
53
- def bibs_base_path
54
- self.class.bibs_base_path
55
- end
55
+ def bibs_base_path
56
+ self.class.bibs_base_path
57
+ end
56
58
 
57
- def headers
58
- self.class.headers
59
- end
59
+ def headers
60
+ self.class.headers
61
+ end
60
62
 
61
- def self.get_body_from(response)
62
- JSON.parse(response.body)
63
- end
63
+ def self.get_body_from(response)
64
+ JSON.parse(response.body)
65
+ end
64
66
 
65
- def self.ids_from_array(ids)
66
- ids.map(&:to_s).map(&:strip).join(',')
67
- end
67
+ def self.ids_from_array(ids)
68
+ ids.map(&:to_s).map(&:strip).join(",")
69
+ end
68
70
  end
69
71
  end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Alma
4
+ class BibHolding
5
+ extend Alma::ApiDefaults
6
+ extend Forwardable
7
+
8
+ def self.find(mms_id:, holding_id:)
9
+ url = "#{bibs_base_path}/#{mms_id}/holdings/#{holding_id}"
10
+ response = HTTParty.get(url, headers: headers, timeout: timeout)
11
+ new(response)
12
+ end
13
+
14
+ attr_reader :holding
15
+ def_delegators :holding, :[], :[]=, :has_key?, :keys, :to_json, :each
16
+
17
+ def initialize(holding)
18
+ @holding = holding
19
+ end
20
+
21
+ def holding_id
22
+ holding["holding_id"]
23
+ end
24
+ end
25
+ end
data/lib/alma/bib_item.rb CHANGED
@@ -1,4 +1,6 @@
1
- require 'alma/bib_item_set'
1
+ # frozen_string_literal: true
2
+
3
+ require "alma/bib_item_set"
2
4
  module Alma
3
5
  class BibItem
4
6
  extend Alma::ApiDefaults
@@ -7,17 +9,28 @@ module Alma
7
9
  attr_reader :item
8
10
  def_delegators :item, :[], :has_key?, :keys, :to_json
9
11
 
10
- PERMITTED_ARGS = [
12
+ PERMITTED_ARGS = [
11
13
  :limit, :offset, :expand, :user_id, :current_library,
12
14
  :current_location, :q, :order_by, :direction
13
15
  ]
14
16
 
15
- def self.find(mms_id, options={})
17
+ def self.find(mms_id, options = {})
16
18
  holding_id = options.delete(:holding_id) || "ALL"
17
- options.select! {|k,_| PERMITTED_ARGS.include? k }
19
+ options.select! { |k, _| PERMITTED_ARGS.include? k }
18
20
  url = "#{bibs_base_path}/#{mms_id}/holdings/#{holding_id}/items"
19
21
  response = HTTParty.get(url, headers: headers, query: options, timeout: timeout)
20
- BibItemSet.new(response, options.merge({mms_id: mms_id, holding_id: holding_id}))
22
+ BibItemSet.new(response, options.merge({ mms_id: mms_id, holding_id: holding_id }))
23
+ end
24
+
25
+ def self.find_by_barcode(barcode)
26
+ response = HTTParty.get(items_base_path, headers: headers, query: { item_barcode: barcode }, timeout: timeout, follow_redirects: true)
27
+ new(response)
28
+ end
29
+
30
+ def self.scan(mms_id:, holding_id:, item_pid:, options: {})
31
+ url = "#{bibs_base_path}/#{mms_id}/holdings/#{holding_id}/items/#{item_pid}"
32
+ response = HTTParty.post(url, headers: headers, query: options)
33
+ new(response)
21
34
  end
22
35
 
23
36
  def initialize(item)
@@ -85,7 +98,7 @@ module Alma
85
98
  end
86
99
 
87
100
  def temp_call_number
88
- holding_data.fetch("temp_call_number","")
101
+ holding_data.fetch("temp_call_number", "")
89
102
  end
90
103
 
91
104
  def has_temp_call_number?
@@ -96,7 +109,7 @@ module Alma
96
109
  if has_temp_call_number?
97
110
  holding_data.fetch("temp_call_number")
98
111
  else
99
- holding_data.fetch("call_number","")
112
+ holding_data.fetch("call_number", "")
100
113
  end
101
114
  end
102
115
 
@@ -105,7 +118,7 @@ module Alma
105
118
  end
106
119
 
107
120
  def alt_call_number
108
- item_data.fetch("alternative_call_number","")
121
+ item_data.fetch("alternative_call_number", "")
109
122
  end
110
123
 
111
124
  def has_process_type?
@@ -121,7 +134,7 @@ module Alma
121
134
  end
122
135
 
123
136
  def base_status
124
- item_data.dig("base_status","value")|| ""
137
+ item_data.dig("base_status", "value") || ""
125
138
  end
126
139
 
127
140
  def in_place?
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Alma
4
-
5
4
  class BibItemSet < ResultSet
5
+ ITEMS_PER_PAGE = 100
6
6
 
7
7
  class ResponseError < ::Alma::StandardError
8
8
  end
@@ -13,7 +13,7 @@ module Alma
13
13
  def_delegators :items, :[], :[]=, :empty?, :size, :each
14
14
  def_delegators :raw_response, :response, :request
15
15
 
16
- def initialize(response, options={})
16
+ def initialize(response, options = {})
17
17
  @raw_response = response
18
18
  parsed = response.parsed_response
19
19
  @total_record_count = parsed["total_record_count"]
@@ -25,7 +25,7 @@ module Alma
25
25
  end
26
26
 
27
27
  def loggable
28
- { total_record_count: @total_record_count,
28
+ { total_record_count: @total_record_count.to_s,
29
29
  mms_id: @mms_id,
30
30
  uri: @raw_response&.request&.uri.to_s
31
31
  }.select { |k, v| !(v.nil? || v.empty?) }
@@ -49,22 +49,29 @@ module Alma
49
49
  end
50
50
 
51
51
  def all
52
+ @last_page_index ||= false
52
53
  Enumerator.new do |yielder|
53
54
  offset = 0
54
- loop do
55
- r = (offset == 0) ? self : single_record_class.find(@mms_id, options=@options.merge({limit: 100, offset: offset}))
55
+ while (!@last_page_index || @last_page_index >= offset / items_per_page) do
56
+ r = (offset == 0) ? self : single_record_class.find(@mms_id, options = @options.merge({ limit: items_per_page, offset: offset }))
56
57
  unless r.empty?
57
58
  r.map { |item| yielder << item }
58
- offset += 100
59
+ @last_page_index = (offset / items_per_page)
59
60
  else
60
- raise StopIteration
61
+ @last_page_index = @last_page_index ? @last_page_index - 1 : -1
62
+ end
63
+
64
+ if r.size == items_per_page
65
+ @last_page_index += 1
61
66
  end
67
+
68
+ offset += items_per_page
62
69
  end
63
70
  end
64
71
  end
65
72
 
66
73
  def each(&block)
67
- @items.each(&block)
74
+ @items.each(&block)
68
75
  end
69
76
 
70
77
  def success?
@@ -78,5 +85,9 @@ module Alma
78
85
  def single_record_class
79
86
  Alma::BibItem
80
87
  end
88
+
89
+ def items_per_page
90
+ ITEMS_PER_PAGE
91
+ end
81
92
  end
82
93
  end
data/lib/alma/config.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Alma
2
4
  class << self
3
5
  attr_accessor :configuration
@@ -14,7 +16,7 @@ module Alma
14
16
 
15
17
  def initialize
16
18
  @apikey = "TEST_API_KEY"
17
- @region = 'https://api-na.hosted.exlibrisgroup.com'
19
+ @region = "https://api-na.hosted.exlibrisgroup.com"
18
20
  @enable_loggable = false
19
21
  @timeout = 5
20
22
  @http_retries = 3
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Alma
4
+ class Course
5
+ extend Alma::ApiDefaults
6
+ extend Forwardable
7
+
8
+ def self.all_courses(args: {})
9
+ response = HTTParty.get("#{courses_base_path}/courses",
10
+ query: args,
11
+ headers: headers,
12
+ timeout: timeout)
13
+ if response.code == 200
14
+ Alma::CourseSet.new(get_body_from(response))
15
+ else
16
+ raise StandardError, get_body_from(response)
17
+ end
18
+ end
19
+
20
+ attr_accessor :response
21
+
22
+ # The Course object can respond directly to Hash like access of attributes
23
+ def_delegators :response, :[], :[]=, :has_key?, :keys, :to_json
24
+
25
+ def initialize(response_body)
26
+ @response = response_body
27
+ end
28
+
29
+ private
30
+
31
+ def self.get_body_from(response)
32
+ JSON.parse(response.body)
33
+ end
34
+
35
+ def self.courses_base_path
36
+ "https://api-na.hosted.exlibrisgroup.com/almaws/v1"
37
+ end
38
+
39
+ def courses_base_path
40
+ self.class.courses_base_path
41
+ end
42
+
43
+ def headers
44
+ self.class.headers
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Alma
4
+ class CourseSet < ResultSet
5
+ def key
6
+ "course"
7
+ end
8
+
9
+ def single_record_class
10
+ Alma::Course
11
+ end
12
+
13
+ def total_record_count
14
+ size
15
+ end
16
+ end
17
+ end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require "httparty"
3
4
  require "active_support"
4
5
  require "active_support/core_ext"
@@ -7,7 +8,6 @@ require "alma/config"
7
8
  module Alma
8
9
  # Alma::Electronic APIs wrapper.
9
10
  class Electronic
10
-
11
11
  class ElectronicError < ArgumentError
12
12
  end
13
13
 
@@ -81,12 +81,12 @@ module Alma
81
81
  end
82
82
 
83
83
  self.class.new(options.merge(
84
- chain: chain,
85
- ids: ids,
86
- type: type,
87
- tag: tag,
88
- notes: notes,
89
- logger: @@logger,
84
+ chain: chain,
85
+ ids: ids,
86
+ type: type,
87
+ tag: tag,
88
+ notes: notes,
89
+ logger: @@logger,
90
90
  ))
91
91
  end
92
92
 
@@ -179,11 +179,11 @@ module Alma
179
179
  end
180
180
 
181
181
  self.class.new(options.merge(
182
- chain: chain,
183
- notes: notes,
184
- type: type,
185
- tag: tag,
186
- logger: @@logger,
182
+ chain: chain,
183
+ notes: notes,
184
+ type: type,
185
+ tag: tag,
186
+ logger: @@logger,
187
187
  ))
188
188
  end
189
189
 
data/lib/alma/error.rb CHANGED
@@ -1,15 +1,16 @@
1
- module Alma::Error
1
+ # frozen_string_literal: true
2
2
 
3
+ module Alma::Error
3
4
  def has_error?
4
5
  !error.empty?
5
6
  end
6
7
 
7
8
  def error_message
8
- (has_error?) ? error['errorList']['error']['errorMessage'] : ''
9
+ (has_error?) ? error["errorList"]["error"]["errorMessage"] : ""
9
10
  end
10
11
 
11
12
  def error
12
- @response.fetch('web_service_result', {})
13
+ @response.fetch("web_service_result", {})
13
14
  end
14
15
  end
15
16
 
data/lib/alma/fine.rb CHANGED
@@ -1,9 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Alma
2
4
  class Fine < AlmaRecord
3
5
  extend Alma::ApiDefaults
4
6
 
5
- def self.where_user(user_id, args={})
6
-
7
+ def self.where_user(user_id, args = {})
7
8
  response = HTTParty.get("#{users_base_path}/#{user_id}/fees", query: args, headers: headers, timeout: timeout)
8
9
  if response.code == 200
9
10
  Alma::FineSet.new(response)
data/lib/alma/fine_set.rb CHANGED
@@ -30,7 +30,7 @@ module Alma
30
30
  end
31
31
 
32
32
  def each(&block)
33
- @results.each(&block)
33
+ @results.each(&block)
34
34
  end
35
35
 
36
36
  def success?
@@ -1,12 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Alma
2
4
  class ItemRequestOptions < RequestOptions
3
-
4
5
  class ResponseError < Alma::StandardError
5
6
  end
6
7
 
7
- def self.get(mms_id, holding_id=nil, item_pid=nil, options={})
8
+ def self.get(mms_id, holding_id = nil, item_pid = nil, options = {})
8
9
  url = "#{bibs_base_path}/#{mms_id}/holdings/#{holding_id}/items/#{item_pid}/request-options"
9
- options.select! {|k,_| REQUEST_OPTIONS_PERMITTED_ARGS.include? k }
10
+ options.select! { |k, _| REQUEST_OPTIONS_PERMITTED_ARGS.include? k }
10
11
  response = HTTParty.get(url, headers: headers, query: options, timeout: timeout)
11
12
  new(response)
12
13
  end