Class: Mediaflux::Request
- Inherits:
-
Object
- Object
- Mediaflux::Request
- Defined in:
- app/models/mediaflux/request.rb
Direct Known Subclasses
ActorSelfDescribeRequest, AssetCreateRequest, AssetDestroyRequest, AssetExistRequest, AssetMetadataRequest, IteratorDestroyRequest, IteratorRequest, LogonRequest, LogoutRequest, NamespaceDescribeRequest, NamespaceDestroyRequest, NamespaceListRequest, ProjectCreateServiceRequest, ProjectReport, ProjectUpdateRequest, ProjectUserAddRequest, QueryRequest, SchemaFetchRequest, ScriptFileListInitRequest, ScriptFileListIterateRequest, ScriptMakeExecutableRequest, ScriptUploadRequest, ServiceExecuteRequest, StoreListRequest, TestAssetCreateRequest, VersionRequest
Instance Attribute Summary collapse
-
#session_token ⇒ Object
readonly
Returns the value of attribute session_token.
Class Method Summary collapse
-
.build_post_request ⇒ Net::HTTP::Post
Constructs a new HTTP POST request for usage with the Mediaflux API.
-
.default_xml_namespace ⇒ String
The default XML namespace which should be used for building the XML Document transmitted in the body of the HTTP request.
- .default_xml_namespace_uri ⇒ Object
-
.find_or_create_http_client ⇒ Object
Constructs and memoizes a new instance of the Net::HTTP::Persistent object at the level of the Class.
-
.format_date_for_mediaflux(iso8601_date) ⇒ Object
This method is used for transforming iso8601 dates to dates that MediaFlux likes Take a string like “2024-02-26T10:33:11-05:00” and convert this string to “22-FEB-2024 13:57:19”.
-
.request_path ⇒ String
The default request URL path for the Mediaflux API.
-
.service ⇒ Object
As this is an abstract class, this should be overridden to specify the Mediaflux API service.
- .uri ⇒ Object
Instance Method Summary collapse
- #error? ⇒ Boolean
-
#initialize(file: nil, session_token: nil, http_client: nil) ⇒ Request
constructor
Constructor.
-
#resolve ⇒ Net::HTTP
Resolves the HTTP request against the Mediaflux API.
-
#resolved? ⇒ Boolean
Determines whether or not the request has been resolved.
-
#response_body ⇒ Object
The response body of the mediaflux call.
- #response_error ⇒ Object
-
#response_xml ⇒ Nokogiri::XML::Document
Resolves the HTTP request, and returns the XML Document parsed from the response body.
- #xml_payload(name: self.class.service) ⇒ Object
-
#xtoshell_xml(name: self.class.service) ⇒ String
The output of this routine can be passed to xtoshell in aterm.
Constructor Details
#initialize(file: nil, session_token: nil, http_client: nil) ⇒ Request
Constructor
50 51 52 53 54 |
# File 'app/models/mediaflux/request.rb', line 50 def initialize(file: nil, session_token: nil, http_client: nil) @http_client = http_client || self.class.find_or_create_http_client @file = file @session_token = session_token end |
Instance Attribute Details
#session_token ⇒ Object (readonly)
Returns the value of attribute session_token.
44 45 46 |
# File 'app/models/mediaflux/request.rb', line 44 def session_token @session_token end |
Class Method Details
.build_post_request ⇒ Net::HTTP::Post
Constructs a new HTTP POST request for usage with the Mediaflux API
23 24 25 |
# File 'app/models/mediaflux/request.rb', line 23 def self.build_post_request Net::HTTP::Post.new(request_path) end |
.default_xml_namespace ⇒ String
The default XML namespace which should be used for building the XML
Document transmitted in the body of the HTTP request
30 31 32 |
# File 'app/models/mediaflux/request.rb', line 30 def self.default_xml_namespace "tigerdata" end |
.default_xml_namespace_uri ⇒ Object
34 35 36 |
# File 'app/models/mediaflux/request.rb', line 34 def self.default_xml_namespace_uri "http://tigerdata.princeton.edu" end |
.find_or_create_http_client ⇒ Object
Constructs and memoizes a new instance of the Net::HTTP::Persistent object at the level of the Class
40 41 42 |
# File 'app/models/mediaflux/request.rb', line 40 def self.find_or_create_http_client HttpConnection.instance.http_client end |
.format_date_for_mediaflux(iso8601_date) ⇒ Object
This method is used for transforming iso8601 dates to dates that MediaFlux likes Take a string like “2024-02-26T10:33:11-05:00” and convert this string to “22-FEB-2024 13:57:19”
121 122 123 124 |
# File 'app/models/mediaflux/request.rb', line 121 def self.format_date_for_mediaflux(iso8601_date) return if iso8601_date.nil? Time.format_date_for_mediaflux(iso8601_date) end |
.request_path ⇒ String
The default request URL path for the Mediaflux API
12 13 14 |
# File 'app/models/mediaflux/request.rb', line 12 def self.request_path "/__mflux_svc__" end |
.service ⇒ Object
As this is an abstract class, this should be overridden to specify the Mediaflux API service
6 7 8 |
# File 'app/models/mediaflux/request.rb', line 6 def self.service raise(NotImplementedError, "#{self} is an abstract class, please override #{self}.service") end |
.uri ⇒ Object
16 17 18 19 |
# File 'app/models/mediaflux/request.rb', line 16 def self.uri # Setting the URI to the Ansible build or Docker build of mediaflux depending on the environment URI("#{Connection.transport}://#{Connection.host}:#{Connection.port}#{request_path}") end |
Instance Method Details
#error? ⇒ Boolean
87 88 89 |
# File 'app/models/mediaflux/request.rb', line 87 def error? response_error.present? end |
#resolve ⇒ Net::HTTP
Resolves the HTTP request against the Mediaflux API
58 59 60 |
# File 'app/models/mediaflux/request.rb', line 58 def resolve @http_response = @http_client.request self.class.uri, http_request end |
#resolved? ⇒ Boolean
Determines whether or not the request has been resolved
64 65 66 |
# File 'app/models/mediaflux/request.rb', line 64 def resolved? @http_response.present? end |
#response_body ⇒ Object
The response body of the mediaflux call
83 84 85 |
# File 'app/models/mediaflux/request.rb', line 83 def response_body @response_body ||= http_response.body end |
#response_error ⇒ Object
91 92 93 94 95 96 97 98 99 100 |
# File 'app/models/mediaflux/request.rb', line 91 def response_error xml = response_xml return nil if xml.xpath("/response/reply/error").count == 0 error = { title: xml.xpath("/response/reply/error").text, message: xml.xpath("/response/reply/message").text } Rails.logger.error "MediaFlux error: #{error[:title]}, #{error[:message]}" error end |
#response_xml ⇒ Nokogiri::XML::Document
Resolves the HTTP request, and returns the XML Document parsed from the response body
70 71 72 73 74 75 76 77 78 79 80 |
# File 'app/models/mediaflux/request.rb', line 70 def response_xml resolve unless resolved? Rails.logger.debug(response_body) @response_xml ||= Nokogiri::XML.parse(response_body) Rails.logger.debug(@response_xml) if @response_xml.xpath("//message").text == "session is not valid" raise Mediaflux::SessionExpired, "Session expired for token #{session_token}" end @response_xml end |
#xml_payload(name: self.class.service) ⇒ Object
104 105 106 107 |
# File 'app/models/mediaflux/request.rb', line 104 def xml_payload( name: self.class.service) body = build_http_request_body(name: ) xml_payload = body.to_xml end |
#xtoshell_xml(name: self.class.service) ⇒ String
The output of this routine can be passed to xtoshell in aterm. The output of which can be sent to service.execute
112 113 114 115 116 117 |
# File 'app/models/mediaflux/request.rb', line 112 def xtoshell_xml( name: self.class.service) xml_builder = build_http_request_body(name: ) xml_builder.doc.xpath("//request/service/@session").remove xml = xml_builder.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::AS_XML | Nokogiri::XML::Node::SaveOptions::NO_DECLARATION) xml.strip.gsub("\"","'").gsub("<args>","").gsub("</args>","") end |