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

pulibrary / allsearch_api / dc764cb3-fc9b-47a1-b010-56b68ecb28a7

01 Aug 2025 03:30PM UTC coverage: 99.514%. Remained the same
dc764cb3-fc9b-47a1-b010-56b68ecb28a7

push

circleci

web-flow
Merge pull request #384 from pulibrary/dependabot/bundler/nokogiri-1.18.9

Bump nokogiri from 1.18.8 to 1.18.9

1024 of 1029 relevant lines covered (99.51%)

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

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

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

21
  ErrorResponse = Struct.new(:problem, :message, :status) do
3✔
22
    def respond
3✔
23
      [
24
        status,
12✔
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]]
58✔
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 => error
46
    standard_error_response error
1✔
47
  end
48

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

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

57
  def query_terms
3✔
58
    @query_terms ||= case request.params['query']
125✔
59
                     # If the query contains any non-whitespace characters
60
                     in /\S/ => query
61
                       query.gsub(/[#{Regexp.escape(special_characters)}]/, ' ')
58✔
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✔
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
    '{}#!</>'
58✔
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