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

pulibrary / allsearch_api / 29cc47ba-38a5-447a-9a89-fa55bc36f401

14 Oct 2025 01:59PM UTC coverage: 99.32% (-0.2%) from 99.514%
29cc47ba-38a5-447a-9a89-fa55bc36f401

Pull #395

circleci

sandbergja
add missing require_relative
Pull Request #395: Migrate a few more controllers away from Rails

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

90 existing lines in 18 files now uncovered.

1022 of 1029 relevant lines covered (99.32%)

96.72 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)
120✔
6
    new(request).response
120✔
7
  end
8

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

13
  def response
3✔
14
    if empty_query?
120✔
15
      empty_query_response
19✔
16
    else
17
      data_response
101✔
18
    end
19
  end
20

21
  ErrorResponse = Struct.new(:problem, :message, :status) do
3✔
22
    def respond
3✔
23
      [
24
        status,
25✔
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]]
101✔
41
  rescue AllsearchError => error
UNCOV
42
    allsearch_error_response error
3✔
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
3✔
50
    service.new(query_terms:).our_response
101✔
51
  end
52

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

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

69
  # :reek:UtilityFunction
70
  def empty_query_response
3✔
71
    ErrorResponse.new('QUERY_IS_EMPTY',
19✔
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✔
UNCOV
78
    Honeybadger.notify exception
3✔
UNCOV
79
    ErrorResponse.new(exception.problem,
3✔
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✔
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
3✔
101
    '{}#!</>'
101✔
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