• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hathitrust / holdings-backend / 14364076017

09 Apr 2025 06:11PM UTC coverage: 74.313%. First build
14364076017

Pull #372

github

web-flow
Merge d9abc7478 into cf4a99761
Pull Request #372: DEV-1590: API for ETAS access queries

38 of 41 new or added lines in 1 file covered. (92.68%)

6382 of 8588 relevant lines covered (74.31%)

41.16 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.68
/lib/api/holdings_api.rb
1
require "cluster"
1✔
2
require "clusterable/ht_item"
1✔
3
require "services"
1✔
4
require "sinatra"
1✔
5

6
class HoldingsAPI < Sinatra::Base
1✔
7
  set :show_exceptions, ENV["show_sinatra_exceptions"] || false
1✔
8
  no_matching_data_error = {"application_error" => "no matching data"}.to_json
1✔
9

10
  get "/v1/ping" do
1✔
11
    "pong"
1✔
12
  end
13

14
  # Answers the question: can organization access ht_id?
15
  get "/v1/item_access" do
1✔
16
    # ArgumentError if missing organization / ht_id.
17
    validate_params(params, ["organization", "ht_id"])
16✔
18

19
    organization = params["organization"]
13✔
20
    ht_id = params["ht_id"]
13✔
21

22
    ht_item = Clusterable::HtItem.find(item_id: ht_id)
13✔
23
    cluster = ht_item.cluster
12✔
24

25
    return_doc = {
26
      "copy_count" => cluster.copy_counts[organization],
12✔
27
      "format" => cluster.format,
28
      "n_enum" => ht_item.n_enum,
29
      "ocns" => ht_item.ocns.sort
30
    }
31
    return_doc.to_json
12✔
32
  end
33

34
  # Answers the question: which organization have holdings that match ht_id?
35
  get "/v1/item_held_by" do
1✔
36
    # ArgumentError if missing ht_id.
37
    validate_params(params, ["ht_id"])
4✔
38
    ht_id = params["ht_id"]
4✔
39

40
    ht_item = Clusterable::HtItem.find(item_id: ht_id)
4✔
41
    cluster = ht_item.cluster
3✔
42

43
    return_doc = {
44
      "organizations" => cluster.organizations_in_cluster
3✔
45
    }
46
    return_doc.to_json
3✔
47
  end
48

49
  def validate_params(params, to_validate)
1✔
50
    argument_errors = []
20✔
51
    to_validate.each do |param_name|
20✔
52
      if !params.key?(param_name) || params[param_name].empty?
36✔
53
        argument_errors << "missing param: #{param_name}"
4✔
54
      end
55
    end
56
    if argument_errors.any?
20✔
57
      raise ArgumentError, argument_errors.join("; ")
3✔
58
    end
59
  end
60

61
  error ArgumentError do
1✔
62
    status 404
3✔
63
    {"application_error" => env["sinatra.error"].message}.to_json
3✔
64
  end
65

66
  error Sequel::NoMatchingRow do
1✔
67
    status 404
2✔
68
    no_matching_data_error
2✔
69
  end
70

71
  error do
1✔
NEW
72
    Services.logger.error env["sinatra.error"].message
×
NEW
73
    status 404
×
NEW
74
    "Error"
×
75
  end
76
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc