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

pulibrary / allsearch_api / ed1cefda-f43b-4b58-95dd-12a33b7f1f7a

02 Jul 2025 06:23PM UTC coverage: 96.252% (-3.2%) from 99.494%
ed1cefda-f43b-4b58-95dd-12a33b7f1f7a

Pull #381

circleci

sandbergja
Refactor RackResponseController so that subclasses don't need to implement #json

Co-authored-by: Christina Chortaria <christinach@users.noreply.github.com>
Pull Request #381: Rewrite DPUL and FindingAids controllers as rack applications

29 of 30 new or added lines in 3 files covered. (96.67%)

94 existing lines in 20 files now uncovered.

976 of 1014 relevant lines covered (96.25%)

63.21 hits per line

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

96.15
/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)
40✔
6
    new(request).response
40✔
7
  end
8

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

13
  def response
2✔
14
    if empty_query?
40✔
15
      empty_query_response
6✔
16
    else
17
      [200, { 'Content-Type' => 'application/json; charset=utf-8' }, [json]]
34✔
18
    end
19
  end
20

21
  private
2✔
22

23
  attr_reader :request
2✔
24

25
  def json
2✔
NEW
26
    raise 'Subclass should implement #json'
×
27
  end
28

29
  def empty_query?
2✔
30
    query_terms == ''
40✔
31
  end
32

33
  def query_terms
2✔
34
    @query_terms ||= case request.params['query']
74✔
35
                     # If the query contains any non-whitespace characters
36
                     in /\S/ => query
37
                       query.gsub(/[#{Regexp.escape(special_characters)}]/, ' ')
34✔
38
                            .gsub(/\s+/, ' ')
39
                            .strip
40
                     else
41
                       ''
6✔
42
                     end
43
  end
44

45
  # :reek:UtilityFunction
46
  def empty_query_response
2✔
47
    # We don't report these to Honeybadger, since the system is working as
48
    # expected in these cases by telling the user they need to enter a query.
49
    error_response(problem: 'QUERY_IS_EMPTY',
6✔
50
                   message: 'The query param must contain non-whitespace characters.',
51
                   status: 400)
52
  end
53

54
  # :reek:UtilityFunction
55
  def error_response(problem:, message:, status:)
2✔
56
    [
57
      status,
6✔
58
      { 'Content-Type' => 'application/json; charset=utf-8' },
59
      [{ error: {
60
        problem:,
61
        message:
62
      } }.to_json]
63
    ]
64
  end
65

66
  def special_characters
2✔
67
    '{}#!</>'
34✔
68
  end
69
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