Class: EMIS::Service

Inherits:
Common::Client::Base show all
Includes:
Common::Client::Concerns::LogAsWarningHelpers
Defined in:
lib/emis/service.rb

Overview

HTTP Client for EMIS requests. Requests and responses are SOAP format

Constant Summary collapse

STATSD_KEY_PREFIX =

Prefix string for StatsD monitoring

'api.emis'

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Common::Client::Concerns::LogAsWarningHelpers

#warn_for_service_unavailable

Methods inherited from Common::Client::Base

configuration, #raise_backend_exception

Methods included from SentryLogging

#log_exception_to_sentry, #log_message_to_sentry, #non_nil_hash?, #normalize_level, #rails_logger

Class Method Details

.create_endpoints(endpoints) ⇒ Object

Create methods for each endpoint in EMIS API.

Parameters:

  • endpoints (Array<String, Symbol, Array<String, Symbol>>)

    An array of endpoints, either a string or symbol if the method name and endpoint path (converted to camelcase) are the same or an Array containing the method name, endpoint path, and optional version of response and soapaction



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/emis/service.rb', line 25

def self.create_endpoints(endpoints)
  endpoints.each do |endpoint|
    operation, request_name, version = get_endpoint_attributes(endpoint)
    define_method(operation) do |options|
      edipi, icn = options.values_at(:edipi, :icn)

      parameters = {
        edipi:,
        icn:,
        request_name:,
        operation:,
        response_type: "EMIS::Responses::#{operation.camelize}Response#{version.upcase}".constantize
      }
      parameters[:version] = version unless version.empty?
      make_request(**parameters)
    end
  end
end

.get_endpoint_attributes(endpoint) ⇒ Array<String, Symbol>

Helper for extracting endpoint attributes from the endpoint configuration

Parameters:

  • endpoint (String, Symbol, Array<String, Symbol>)

    Either a string or symbol if the method name and endpoint path (converted to camelcase) are the same or an Array containing the method name, endpoint path, and optional version of response and soapaction

Returns:

  • (Array<String, Symbol>)

    An array containing the method name, endpoint path, and version of response and soapaction



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/emis/service.rb', line 53

def self.get_endpoint_attributes(endpoint)
  operation = nil
  request_name = nil
  if endpoint.is_a?(Array)
    operation = endpoint[0].to_s
    request_name = endpoint[1]
    version = endpoint[2] || ''
  else
    operation = endpoint.to_s
    request_name = "#{endpoint.to_s.camelize(:lower).sub(/^get/, '').camelize(:lower)}Request"
    version = ''
  end
  [operation, request_name, version]
end

Instance Method Details

#create_edipi_or_icn_message(edipi:, icn:, request_name:) ⇒ EMIS::Messages::EdipiOrIcnMessage (protected)

Creates a SOAP request body that includes user identifiers to send to the EMIS API

Parameters:

  • edipi (String)

    User’s Electronic Data Interchange Personal Identifier

  • icn (String)

    User’s Integration Control Number

  • request_name (String)

    Request name used in XML request body

Returns:



115
116
117
118
119
120
121
122
# File 'lib/emis/service.rb', line 115

def create_edipi_or_icn_message(edipi:, icn:, request_name:)
  EMIS::Messages::EdipiOrIcnMessage.new(
    edipi:,
    icn:,
    request_name:,
    custom_namespaces:
  ).to_xml
end

#make_request(response_type:, operation:, request_name:, edipi: nil, icn: nil, version: 'V1') ⇒ EMIS::Responses (protected)

Helper for sending requests to the EMIS API

rubocop:disable Metrics/ParameterLists

Parameters:

  • edipi (String) (defaults to: nil)

    User’s Electronic Data Interchange Personal Identifier

  • icn (String) (defaults to: nil)

    User’s Integration Control Number

  • response_type (EMIS::Responses)

    EMIS Response class

  • operation (String)

    API path endpoint

  • request_name (String)

    Request name used in XML request body

  • version (String) (defaults to: 'V1')

    Version for soapaction

Returns:

  • (EMIS::Responses)

    Whatever response_type was passed in will be returned



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/emis/service.rb', line 82

def make_request(response_type:, operation:, request_name:, edipi: nil, icn: nil, version: 'V1')
  message = create_edipi_or_icn_message(
    edipi:,
    icn:,
    request_name:
  )
  raw_response = warn_for_service_unavailable do
    perform(
      :post,
      '',
      message,
      soapaction: "http://viers.va.gov/cdi/eMIS/#{operation.camelize(:lower)}/#{version.downcase}"
    )
  end
  response_type.new(raw_response)
  # :nocov:
rescue Faraday::ConnectionFailed => e
  Rails.logger.error "eMIS connection failed: #{e.message}"
  EMIS::Responses::ErrorResponse.new(e)
rescue Common::Client::Errors::ClientError => e
  Rails.logger.error "eMIS error: #{e.message}"
  EMIS::Responses::ErrorResponse.new(e)
  # :nocov:
end