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

pulibrary / tigerdata-app / 5205f704-d89d-4c43-8cfa-9e6783edc33a

29 Feb 2024 07:04PM UTC coverage: 60.206% (-29.9%) from 90.092%
5205f704-d89d-4c43-8cfa-9e6783edc33a

push

circleci

jrgriffiniii
Ensuring that the storage usage and storage capacity is retrieved for
Mediaflux Projects and rendered on the "contents" Project View

11 of 34 new or added lines in 4 files covered. (32.35%)

451 existing lines in 34 files now uncovered.

935 of 1553 relevant lines covered (60.21%)

9.47 hits per line

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

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

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

11
      # The default request URL path for the Mediaflux API
12
      # @return [String]
13
      def self.request_path
1✔
UNCOV
14
        "/__mflux_svc__"
×
15
      end
16

17
      def self.protocol
1✔
UNCOV
18
        if mediaflux_port == 443
×
19
          "https"
×
20
        else
UNCOV
21
          "http"
×
22
        end
23
      end
24

25
      def self.uri
1✔
UNCOV
26
        URI("#{protocol}://#{mediaflux_host}:#{mediaflux_port}/#{request_path}")
×
27
      end
28

29
      # Constructs a new HTTP POST request for usage with the Mediaflux API
30
      # @return [Net::HTTP::Post]
31
      def self.build_post_request
1✔
UNCOV
32
        Net::HTTP::Post.new(request_path)
×
33
      end
34

35
      # The Rails configuration options specifying the Mediaflux server
36
      # @return [Hash]
37
      def self.mediaflux
1✔
38
        Rails.configuration.mediaflux
30✔
39
      end
40

41
      # The host URL for the Mediaflux server
42
      # @return [String]
43
      def self.mediaflux_host
1✔
UNCOV
44
        Rails.configuration.mediaflux["api_host"]
×
45
      end
46

47
      # The host port for the Mediaflux server
48
      # @return [String]
49
      def self.mediaflux_port
1✔
UNCOV
50
        Rails.configuration.mediaflux["api_port"].to_i
×
51
      end
52

53
      # The default XML namespace which should be used for building the XML
54
      #   Document transmitted in the body of the HTTP request
55
      # @return [String]
56
      def self.default_xml_namespace
1✔
UNCOV
57
        "tigerdata"
×
58
      end
59

60
      def self.default_xml_namespace_uri
1✔
UNCOV
61
        "http://tigerdata.princeton.edu"
×
62
      end
63

64
      # Constructs and memoizes a new instance of the Net::HTTP::Persistent object at the level of the Class
65
      # @returns http_client [Net::HTTP::Persistent] HTTP client for transmitting requests to the Mediaflux server API
66
      def self.find_or_create_http_client
1✔
67
        HttpConnection.instance.http_client
61✔
68
      end
69

70
      attr_reader :session_token
1✔
71

72
      # Constructor
73
      # @param file [File] any upload file required for the POST request
74
      # @param session_token [String] the API token for the authenticated session
75
      # @param http_client [Net::HTTP::Persistent] HTTP client for transmitting requests to the Mediaflux server API
76
      def initialize(file: nil, session_token: nil, http_client: nil)
1✔
77
        @http_client = http_client || self.class.find_or_create_http_client
61✔
78
        @file = file
61✔
79
        @session_token = session_token
61✔
80
        ObjectSpace.define_finalizer(self, self.class.finalizer(@http_client))
61✔
81
      end
82

83
      # Resolves the HTTP request against the Mediaflux API
84
      # @return [Net::HTTP]
85
      def resolve
1✔
UNCOV
86
        @http_response = @http_client.request self.class.uri, http_request
×
87
      end
88

89
      # Determines whether or not the request has been resolved
90
      # @return [Boolean]
91
      def resolved?
1✔
UNCOV
92
        @http_response.present?
×
93
      end
94

95
      # Resolves the HTTP request, and returns the XML Document parsed from the response body
96
      # @return [Nokogiri::XML::Document]
97
      def response_xml
1✔
UNCOV
98
        resolve unless resolved?
×
99

UNCOV
100
        Rails.logger.debug(response_body)
×
UNCOV
101
        @response_xml ||= Nokogiri::XML.parse(response_body)
×
UNCOV
102
        Rails.logger.debug(@response_xml)
×
UNCOV
103
        if @response_xml.xpath("//message").text == "session is not valid"
×
104
          raise Mediaflux::Http::SessionExpired, session_token
×
105
        end
106

UNCOV
107
        @response_xml
×
108
      end
109

110
      # The response body of the mediaflux call
111
      def response_body
1✔
UNCOV
112
        @response_body ||= http_response.body
×
113
      end
114

115
      def error?
1✔
UNCOV
116
        response_error.present?
×
117
      end
118

119
      def response_error
1✔
UNCOV
120
        xml = response_xml
×
UNCOV
121
        return nil if xml.xpath("/response/reply/error").count == 0
×
122
        error = {
123
          title: xml.xpath("/response/reply/error").text,
×
124
          message: xml.xpath("/response/reply/message").text
125
        }
126
        Rails.logger.error "MediaFlux error: #{error[:title]}, #{error[:message]}"
×
127
        error
×
128
      end
129

130
      delegate :to_s, to: :response_xml
1✔
131

132
      def xml_payload( name: self.class.service)
1✔
UNCOV
133
        body = build_http_request_body(name: )
×
UNCOV
134
        xml_payload = body.to_xml
×
135
      end
136

137
      private
1✔
138

139
        def http_request
1✔
UNCOV
140
          @http_request ||= build_http_request(name: self.class.service, form_file: @file)
×
141
        end
142

143
        def http_response
1✔
UNCOV
144
          @http_response ||= resolve
×
145
        end
146

147
        def build_http_request_body(name:)
1✔
UNCOV
148
          args = { name: name }
×
UNCOV
149
          args[:session] = session_token unless session_token.nil?
×
150

UNCOV
151
          Nokogiri::XML::Builder.new(namespace_inheritance: false) do |xml|
×
UNCOV
152
            xml.request do
×
UNCOV
153
              xml.service(**args) do
×
UNCOV
154
                yield xml if block_given?
×
155
              end
156
            end
157
          end
158
        end
159

160
        # rubocop:disable Metrics/MethodLength
161
        def build_http_request(name:, form_file: nil)
1✔
UNCOV
162
          request = self.class.build_post_request
×
163

UNCOV
164
          Rails.logger.debug(xml_payload)
×
UNCOV
165
          if form_file.nil?
×
UNCOV
166
            request["Content-Type"] = "text/xml; charset=utf-8"
×
UNCOV
167
            request.body = xml_payload(name:)
×
168
          else
UNCOV
169
            request["Content-Type"] = "multipart/form-data"
×
UNCOV
170
            request.set_form({ "request" => xml_payload,
×
171
                               "nb-data-attachments" => "1",
172
                               "file_0" => form_file },
173
                          "multipart/form-data",
174
                          "charset" => "UTF-8")
175
          end
176

UNCOV
177
          request
×
178
        end
179
      # rubocop:enable Metrics/MethodLength
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