Class: EVSS::BaseService

Inherits:
Object
  • Object
show all
Includes:
SentryLogging
Defined in:
lib/evss/base_service.rb

Direct Known Subclasses

ClaimsService, CommonService, DocumentsService

Constant Summary collapse

SYSTEM_NAME =
'vets.gov'
DEFAULT_TIMEOUT =

in seconds

40

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SentryLogging

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

Constructor Details

#initialize(headers, use_mock = false) ⇒ BaseService

Returns a new instance of BaseService.



14
15
16
17
# File 'lib/evss/base_service.rb', line 14

def initialize(headers, use_mock = false)
  @headers = headers
  @use_mock = use_mock
end

Class Method Details

.create_breakers_service(name:, url:) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/evss/base_service.rb', line 19

def self.create_breakers_service(name:, url:)
  path = URI.parse(url).path
  host = URI.parse(url).host
  matcher = proc do |request_env|
    request_env.url.host == host && request_env.url.path =~ /^#{path}/
  end

  Breakers::Service.new(
    name:,
    request_matcher: matcher
  )
end

Instance Method Details

#base_urlObject (protected)



42
43
44
# File 'lib/evss/base_service.rb', line 42

def base_url
  self.class::BASE_URL
end

#cert?Boolean (private)

Returns:

  • (Boolean)


85
86
87
88
89
90
# File 'lib/evss/base_service.rb', line 85

def cert?
  # TODO(knkski): Is this logic correct?
  Settings.evss.cert_path.present? ||
    Settings.evss.key_path.present? ||
    Settings.evss.root_cert_path.present?
end

#client_certObject (private)

:nocov:



93
94
95
# File 'lib/evss/base_service.rb', line 93

def client_cert
  OpenSSL::X509::Certificate.new File.read(Settings.evss.cert_path)
end

#client_keyObject (private)



97
98
99
# File 'lib/evss/base_service.rb', line 97

def client_key
  OpenSSL::PKey::RSA.new File.read(Settings.evss.key_path)
end

#connObject (private)

Uses HTTPClient adapter because headers need to be sent unmanipulated Net/HTTP capitalizes headers



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/evss/base_service.rb', line 58

def conn
  @conn ||= Faraday.new(base_url, headers: @headers, ssl: ssl_options, request: timeout) do |faraday|
    faraday.use      :breakers
    faraday.use      Faraday::Response::RaiseError
    faraday.use      EVSS::ErrorMiddleware
    faraday.response :betamocks if @use_mock
    faraday.response :snakecase, symbolize: false
    faraday.response :json_parser
    faraday.use      :remove_cookies
    faraday.adapter  :httpclient
  end
end

#get(url) ⇒ Object (protected)



34
35
36
# File 'lib/evss/base_service.rb', line 34

def get(url)
  conn.get url
end

#post(url, body = nil, headers = { 'Content-Type' => 'application/json' }) ⇒ Object (protected)



38
39
40
# File 'lib/evss/base_service.rb', line 38

def post(url, body = nil, headers = { 'Content-Type' => 'application/json' }, &)
  conn.post(url, body, headers, &)
end

#root_caObject (private)



101
102
103
# File 'lib/evss/base_service.rb', line 101

def root_ca
  Settings.evss.root_cert_path
end

#ssl_optionsObject (private)



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/evss/base_service.rb', line 71

def ssl_options
  return { verify: false } if !cert? && (Rails.env.development? || Rails.env.test?)

  if cert?
    {
      version: :TLSv1_2,
      verify: true,
      client_cert:,
      client_key:,
      ca_file: root_ca
    }
  end
end

#timeoutObject (protected)



46
47
48
49
50
51
52
# File 'lib/evss/base_service.rb', line 46

def timeout
  {
    timeout: self.class::DEFAULT_TIMEOUT,
    open_timeout: self.class::DEFAULT_TIMEOUT,
    write_timeout: self.class::DEFAULT_TIMEOUT
  }
end