Class: Mediaflux::Request

Inherits:
Object
  • Object
show all
Defined in:
app/models/mediaflux/request.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file: nil, session_token: nil, http_client: nil, session_user: nil) ⇒ Request

Constructor

Parameters:

  • file (File) (defaults to: nil)

    any upload file required for the POST request

  • session_token (String) (defaults to: nil)

    the API token for the authenticated session

  • http_client (Net::HTTP::Persistent) (defaults to: nil)

    HTTP client for transmitting requests to the Mediaflux server API



50
51
52
53
54
55
# File 'app/models/mediaflux/request.rb', line 50

def initialize(file: nil, session_token: nil, http_client: nil, session_user: nil)
  @http_client = http_client || self.class.find_or_create_http_client
  @file = file
  @session_token = session_token
  @session_user = session_user
end

Instance Attribute Details

#session_tokenObject (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_requestNet::HTTP::Post

Constructs a new HTTP POST request for usage with the Mediaflux API

Returns:

  • (Net::HTTP::Post)


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_namespaceString

The default XML namespace which should be used for building the XML

Document transmitted in the body of the HTTP request

Returns:

  • (String)


30
31
32
# File 'app/models/mediaflux/request.rb', line 30

def self.default_xml_namespace
  "tigerdata"
end

.default_xml_namespace_uriObject



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_clientObject

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”



122
123
124
125
# File 'app/models/mediaflux/request.rb', line 122

def self.format_date_for_mediaflux(iso8601_date)
  return if iso8601_date.nil?
  Time.format_date_for_mediaflux(iso8601_date)
end

.request_pathString

The default request URL path for the Mediaflux API

Returns:

  • (String)


12
13
14
# File 'app/models/mediaflux/request.rb', line 12

def self.request_path
  "/__mflux_svc__"
end

.serviceObject

As this is an abstract class, this should be overridden to specify the Mediaflux API service

Raises:

  • (NotImplementedError)


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

.uriObject



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

Returns:

  • (Boolean)


88
89
90
# File 'app/models/mediaflux/request.rb', line 88

def error?
  response_error.present?
end

#resolveNet::HTTP

Resolves the HTTP request against the Mediaflux API

Returns:

  • (Net::HTTP)


59
60
61
# File 'app/models/mediaflux/request.rb', line 59

def resolve
  @http_response = @http_client.request self.class.uri, http_request
end

#resolved?Boolean

Determines whether or not the request has been resolved

Returns:

  • (Boolean)


65
66
67
# File 'app/models/mediaflux/request.rb', line 65

def resolved?
  @http_response.present?
end

#response_bodyObject

The response body of the mediaflux call



84
85
86
# File 'app/models/mediaflux/request.rb', line 84

def response_body
  @response_body ||= http_response.body
end

#response_errorObject



92
93
94
95
96
97
98
99
100
101
# File 'app/models/mediaflux/request.rb', line 92

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_xmlNokogiri::XML::Document

Resolves the HTTP request, and returns the XML Document parsed from the response body

Returns:

  • (Nokogiri::XML::Document)


71
72
73
74
75
76
77
78
79
80
81
# File 'app/models/mediaflux/request.rb', line 71

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_token
  end

  @response_xml
end

#xml_payload(name: self.class.service) ⇒ Object



105
106
107
108
# File 'app/models/mediaflux/request.rb', line 105

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

Parameters:

  • name (String) (defaults to: self.class.service)

    name of the service this request will send

Returns:

  • (String)

    xml that can be passed to xtoshell without manipulation



113
114
115
116
117
118
# File 'app/models/mediaflux/request.rb', line 113

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