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

pulibrary / allsearch_api / 322d09b9-bf13-4991-90db-61f466d8fd7d

02 Dec 2025 06:07PM UTC coverage: 99.905% (+12.0%) from 87.896%
322d09b9-bf13-4991-90db-61f466d8fd7d

push

circleci

web-flow
Merge pull request #416 from pulibrary/repo-factory

Extract ROM repository creation into a factory

17 of 17 new or added lines in 3 files covered. (100.0%)

78 existing lines in 18 files now uncovered.

1055 of 1056 relevant lines covered (99.91%)

102.95 hits per line

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

100.0
/app/controllers/rack_response_controller.rb
1
# frozen_string_literal: true
2

3
class RackResponseController
3✔
4
  def self.call(env)
3✔
5
    request = Rack::Request.new(env)
236✔
6
    new(request).response
236✔
7
  end
8

9
  def initialize(request)
3✔
10
    @request = request
236✔
11
  end
12

13
  def response
3✔
14
    if empty_query?
236✔
15
      empty_query_response
29✔
16
    else
17
      data_response
207✔
18
    end
19
  end
20

21
  ErrorResponse = Struct.new(:problem, :message, :status) do
3✔
22
    def respond
3✔
23
      [
24
        status,
39✔
25
        { 'Content-Type' => 'application/json; charset=utf-8' },
26
        [{ error: {
27
          problem:,
28
          message:
29
        } }.to_json]
30
      ]
31
    end
32
  end
33

34
  private
3✔
35

36
  attr_reader :request, :service
3✔
37

38
  # :reek:TooManyStatements
39
  def data_response
3✔
40
    [200, { 'Content-Type' => 'application/json; charset=utf-8' }, [json]]
207✔
41
  rescue AllsearchError => error
42
    allsearch_error_response error
7✔
43
  rescue Timeout::Error, Errno::ECONNRESET, Net::ProtocolError => error
44
    upstream_http_error_response error
2✔
45
  rescue StandardError => error
UNCOV
46
    standard_error_response error
1✔
47
  end
48

49
  def json
3✔
50
    service.new(query_terms:).our_response
207✔
51
  end
52

53
  def empty_query?
3✔
54
    query_terms == ''
236✔
55
  end
56

57
  def query_terms
3✔
58
    @query_terms ||= case request.params['query']
443✔
59
                     # If the query contains any non-whitespace characters
60
                     in /\S/ => query
61
                       query.gsub(/[#{Regexp.escape(special_characters)}]/, ' ')
207✔
62
                            .gsub(/\s+/, ' ')
63
                            .strip
64
                     else
65
                       ''
29✔
66
                     end
67
  end
68

69
  # :reek:UtilityFunction
70
  def empty_query_response
3✔
71
    ErrorResponse.new('QUERY_IS_EMPTY',
29✔
72
                      'The query param must contain non-whitespace characters.',
73
                      400).respond
74
  end
75

76
  # :reek:UtilityFunction
77
  def allsearch_error_response(exception)
3✔
78
    Honeybadger.notify exception
7✔
79
    ErrorResponse.new(exception.problem,
7✔
80
                      exception.message,
81
                      500).respond
82
  end
83

84
  # :reek:UtilityFunction
85
  def upstream_http_error_response(exception)
3✔
86
    Honeybadger.notify exception
2✔
87
    ErrorResponse.new('UPSTREAM_ERROR',
2✔
88
                      "Query to upstream failed with #{exception.class}, message: #{exception.message}",
89
                      500).respond
90
  end
91

92
  # :reek:UtilityFunction
93
  def standard_error_response(exception)
3✔
UNCOV
94
    Honeybadger.notify exception
1✔
UNCOV
95
    ErrorResponse.new('APPLICATION_ERROR',
1✔
96
                      "This application threw #{exception.class}",
97
                      500).respond
98
  end
99

100
  def special_characters
3✔
101
    '{}#!</>'
207✔
102
  end
103
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

© 2025 Coveralls, Inc