-
Notifications
You must be signed in to change notification settings - Fork 57
/
retriever.rb
71 lines (58 loc) · 1.76 KB
/
retriever.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# frozen_string_literal: true
module AskVAApi
module Inquiries
class InquiriesRetrieverError < StandardError; end
class Retriever < BaseRetriever
attr_reader :icn, :user_mock_data, :entity_class
def initialize(user_mock_data:, entity_class:, icn: nil)
super(user_mock_data:, entity_class:)
@icn = icn
end
def fetch_by_id(id:)
inq = fetch_data(id)
reply = fetch_correspondences(inquiry_id: id)
entity_class.new(inq.first, reply)
rescue => e
::ErrorHandler.handle_service_error(e)
end
private
def fetch_data(id = nil)
if user_mock_data
data = read_mock_data('get_inquiries_mock_data.json')
filter_data(data, id)
else
endpoint = 'inquiries'
id ||= icn
payload = { id: }
response = Crm::Service.new(icn:).call(endpoint:, payload:)
handle_response_data(response)
end
end
def fetch_correspondences(inquiry_id:)
correspondences = Correspondences::Retriever.new(
inquiry_id:,
user_mock_data:,
entity_class: AskVAApi::Correspondences::Entity
).call
case correspondences
when Hash
[]
else
correspondences
end
end
def read_mock_data(file_name)
data = File.read("modules/ask_va_api/config/locales/#{file_name}")
JSON.parse(data, symbolize_names: true)[:Data]
end
def filter_data(data, id = nil)
data.select do |inq|
id ? inq[:InquiryNumber] == id : inq[:Icn] == icn
end
end
def handle_response_data(response)
response[:Data].presence || raise(InquiriesRetrieverError, response)
end
end
end
end