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

pulibrary / tigerdata-app / 529f7cba-0651-41c2-9045-ff17f0a5a349

21 Oct 2025 09:43PM UTC coverage: 86.877%. Remained the same
529f7cba-0651-41c2-9045-ff17f0a5a349

Pull #2070

circleci

bess
Remove tests for special production behavior
Pull Request #2070: Remove tests for special behavior in production

2615 of 3010 relevant lines covered (86.88%)

340.18 hits per line

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

97.53
/app/models/mediaflux/request.rb
1
# frozen_string_literal: true
2
module Mediaflux
4✔
3
    class Request
4✔
4

5
      # As this is an abstract class, this should be overridden to specify the Mediaflux API service
6
      def self.service
4✔
7
        raise(NotImplementedError, "#{self} is an abstract class, please override #{self}.service")
1✔
8
      end
9

10
      # The default request URL path for the Mediaflux API
11
      # @return [String]
12
      def self.request_path
4✔
13
        "/__mflux_svc__"
6,116✔
14
      end
15

16
      def self.uri
4✔
17
        # Setting the URI to the Ansible build or Docker build of mediaflux depending on the environment
18
        URI("#{Connection.transport}://#{Connection.host}:#{Connection.port}#{request_path}")
3,070✔
19
      end
20

21
      # Constructs a new HTTP POST request for usage with the Mediaflux API
22
      # @return [Net::HTTP::Post]
23
      def self.build_post_request
4✔
24
        Net::HTTP::Post.new(request_path)
3,046✔
25
      end
26

27
      # The default XML namespace which should be used for building the XML
28
      #   Document transmitted in the body of the HTTP request
29
      # @return [String]
30
      def self.default_xml_namespace
4✔
31
        "tigerdata"
48✔
32
      end
33

34
      def self.default_xml_namespace_uri
4✔
35
        "http://tigerdata.princeton.edu"
48✔
36
      end
37

38
      # Constructs and memoizes a new instance of the Net::HTTP::Persistent object at the level of the Class
39
      # @returns http_client [Net::HTTP::Persistent] HTTP client for transmitting requests to the Mediaflux server API
40
      def self.find_or_create_http_client
4✔
41
        HttpConnection.instance.http_client
3,053✔
42
      end
43

44
      attr_reader :session_token
4✔
45

46
      # Constructor
47
      # @param file [File] any upload file required for the POST request
48
      # @param session_token [String] the API token for the authenticated session
49
      # @param http_client [Net::HTTP::Persistent] HTTP client for transmitting requests to the Mediaflux server API
50
      def initialize(file: nil, session_token: nil, http_client: nil)
4✔
51
        @http_client = http_client || self.class.find_or_create_http_client
3,053✔
52
        @file = file
3,053✔
53
        @session_token = session_token
3,053✔
54
      end
55

56
      # Resolves the HTTP request against the Mediaflux API
57
      # @return [Net::HTTP]
58
      def resolve
4✔
59
        @http_response = @http_client.request self.class.uri, http_request
3,052✔
60
      end
61

62
      # Determines whether or not the request has been resolved
63
      # @return [Boolean]
64
      def resolved?
4✔
65
        @http_response.present?
3,949✔
66
      end
67

68
      # Resolves the HTTP request, and returns the XML Document parsed from the response body
69
      # @return [Nokogiri::XML::Document]
70
      def response_xml
4✔
71
        resolve unless resolved?
3,947✔
72
        Rails.logger.debug(response_body)
3,947✔
73
        @response_xml ||= Nokogiri::XML.parse(response_body)
3,947✔
74
        Rails.logger.debug(@response_xml)
3,947✔
75
        if @response_xml.xpath("//message").text == "session is not valid"
3,947✔
76
          raise Mediaflux::SessionExpired, "Session expired for token #{session_token}"
4✔
77
        end
78

79
        @response_xml
3,943✔
80
      end
81

82
      # The response body of the mediaflux call
83
      def response_body
4✔
84
        @response_body ||= http_response.body
6,854✔
85
      end
86

87
      def error?
4✔
88
        response_error.present?
942✔
89
      end
90

91
      def response_error
4✔
92
        xml = response_xml
952✔
93
        return nil if xml.xpath("/response/reply/error").count == 0
952✔
94
        error = {
95
          title: xml.xpath("/response/reply/error").text,
17✔
96
          message: xml.xpath("/response/reply/message").text
97
        }
98
        Rails.logger.error "MediaFlux error: #{error[:title]}, #{error[:message]}"
17✔
99
        error
17✔
100
      end
101

102
      delegate :to_s, to: :response_xml
4✔
103

104
      def xml_payload( name: self.class.service)
4✔
105
        body = build_http_request_body(name: )
6,094✔
106
        xml_payload = body.to_xml
6,094✔
107
      end
108

109
      # The output of this routine can be passed to xtoshell in aterm.  The output of which can be sent to service.execute
110
      # @param [String] name name of the service this request will send
111
      # @return [String] xml that can be passed to xtoshell without manipulation
112
      def xtoshell_xml( name: self.class.service)
4✔
113
        xml_builder = build_http_request_body(name: )
1✔
114
        xml_builder.doc.xpath("//request/service/@session").remove
1✔
115
        xml = xml_builder.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::AS_XML | Nokogiri::XML::Node::SaveOptions::NO_DECLARATION)
1✔
116
        xml.strip.gsub("\"","'").gsub("<args>","").gsub("</args>","")
1✔
117
      end
118

119
      # This method is used for transforming iso8601 dates to dates that MediaFlux likes
120
      # Take a string like "2024-02-26T10:33:11-05:00" and convert this string to "22-FEB-2024 13:57:19"
121
      def self.format_date_for_mediaflux(iso8601_date)
4✔
122
        return if iso8601_date.nil?
×
123
        Time.format_date_for_mediaflux(iso8601_date)
×
124
      end
125

126
      private
4✔
127

128
        def http_request
4✔
129
          @http_request ||= build_http_request(name: self.class.service, form_file: @file)
3,052✔
130
        end
131

132
        def http_response
4✔
133
          @http_response ||= resolve
2,902✔
134
        end
135

136
        def build_http_request_body(name:)
4✔
137
          args = { name: name }
6,095✔
138
                                                # must use @session_token here instead of session_token
139
                                                #  for login to not go into an infinaite loop
140
          args[:session] = session_token unless @session_token.nil?
6,095✔
141

142
          Nokogiri::XML::Builder.new(namespace_inheritance: false) do |xml|
6,095✔
143
            xml.request do
6,095✔
144
              xml.service(**args) do
6,095✔
145
                yield xml if block_given?
6,095✔
146
              end
147
            end
148
          end
149
        end
150

151
        # rubocop:disable Metrics/MethodLength
152
        def build_http_request(name:, form_file: nil)
4✔
153
          request = self.class.build_post_request
3,046✔
154

155
          log_xml_request(xml_payload)
3,046✔
156
          if form_file.nil?
3,046✔
157
            request["Content-Type"] = "text/xml; charset=utf-8"
3,045✔
158
            request.body = xml_payload(name:)
3,045✔
159
          else
160
            request["Content-Type"] = "multipart/form-data"
1✔
161
            request.set_form({ "request" => xml_payload,
1✔
162
                               "nb-data-attachments" => "1",
163
                               "file_0" => form_file },
164
                          "multipart/form-data",
165
                          "charset" => "UTF-8")
166
          end
167

168
          request
3,046✔
169
        end
170
      # rubocop:enable Metrics/MethodLength
171

172
      def log_xml_request(xml_payload)
4✔
173
        password_element = xml_payload.match(/\<password\>.*\<\/password\>/)
3,046✔
174
        if password_element.nil?
3,046✔
175
          Rails.logger.debug(xml_payload)
1,568✔
176
        else
177
          # Don't log the password
178
          Rails.logger.debug(xml_payload.gsub(password_element.to_s, "<password>***</password>"))
1,478✔
179
        end
180
      end
181
    end
182
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