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

pulibrary / allsearch_api / 0400cc50-b3f0-4794-8616-b4382e0acdd0

14 Oct 2025 03:57AM UTC coverage: 94.558% (-5.0%) from 99.514%
0400cc50-b3f0-4794-8616-b4382e0acdd0

Pull #395

circleci

sandbergja
Migrate Library Staff Controller away from ActionController
Pull Request #395: Migrate a few more controllers away from Rails

8 of 8 new or added lines in 4 files covered. (100.0%)

93 existing lines in 21 files now uncovered.

973 of 1029 relevant lines covered (94.56%)

71.0 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
2✔
4
  def self.call(env)
2✔
5
    request = Rack::Request.new(env)
84✔
6
    new(request).response
84✔
7
  end
8

9
  def initialize(request)
2✔
10
    @request = request
84✔
11
  end
12

13
  def response
2✔
14
    if empty_query?
84✔
15
      empty_query_response
10✔
16
    else
17
      data_response
74✔
18
    end
19
  end
20

21
  ErrorResponse = Struct.new(:problem, :message, :status) do
2✔
22
    def respond
2✔
23
      [
24
        status,
14✔
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
2✔
35

36
  attr_reader :request, :service
2✔
37

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

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

53
  def empty_query?
2✔
54
    query_terms == ''
84✔
55
  end
56

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

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

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

84
  # :reek:UtilityFunction
85
  def upstream_http_error_response(exception)
2✔
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)
2✔
94
    Honeybadger.notify exception
1✔
95
    ErrorResponse.new('APPLICATION_ERROR',
1✔
96
                      "This application threw #{exception.class}",
97
                      500).respond
98
  end
99

100
  def special_characters
2✔
101
    '{}#!</>'
74✔
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