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

pulibrary / allsearch_api / cd2efc44-9e61-4b6d-8164-34646b309921

02 Jul 2025 09:38PM UTC coverage: 99.223% (-0.3%) from 99.507%
cd2efc44-9e61-4b6d-8164-34646b309921

Pull #382

circleci

sandbergja
Migrate Catalog from Rails controller to Rack app
Pull Request #382: Migrate Catalog from Rails controller to Rack app

18 of 21 new or added lines in 2 files covered. (85.71%)

1021 of 1029 relevant lines covered (99.22%)

92.21 hits per line

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

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

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

13
  def response
3✔
14
    if empty_query?
66✔
15
      empty_query_response
9✔
16
    else
17
      data_response
57✔
18
    end
19
  end
20

21
  ErrorResponse = Struct.new(:problem, :message, :status) do
3✔
22
    def respond
3✔
23
      [
24
        status,
11✔
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]]
57✔
41
  rescue AllsearchError => error
42
    allsearch_error_response error
1✔
43
  rescue Timeout::Error, Errno::ECONNRESET, Net::ProtocolError => error
44
    upstream_http_error_response error
1✔
45
  rescue StandardError
NEW
46
    standard_error_response error
×
47
  end
48

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

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

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

69
  # :reek:UtilityFunction
70
  def empty_query_response
3✔
71
    ErrorResponse.new('QUERY_IS_EMPTY',
9✔
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
1✔
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)
3✔
86
    Honeybadger.notify exception
1✔
87
    ErrorResponse.new('UPSTREAM_ERROR',
1✔
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✔
NEW
94
    Honeybadger.notify exception
×
NEW
95
    ErrorResponse.new('APPLICATION_ERROR',
×
96
                      "This application threw #{exception.class}",
97
                      500).respond
98
  end
99

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