Class: CARMA::Models::Attachments

Inherits:
Base
  • Object
show all
Defined in:
lib/carma/models/attachments.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

after_to_request_payload, request_payload_key

Constructor Details

#initialize(carma_case_id, veteran_first_name, veteran_last_name) ⇒ Attachments

Returns a new instance of Attachments.



17
18
19
20
21
22
# File 'lib/carma/models/attachments.rb', line 17

def initialize(carma_case_id, veteran_first_name, veteran_last_name)
  @carma_case_id        = carma_case_id
  @veteran_first_name   = veteran_first_name
  @veteran_last_name    = veteran_last_name
  @all                  = []
end

Instance Attribute Details

#allObject

Returns the value of attribute all.



12
13
14
# File 'lib/carma/models/attachments.rb', line 12

def all
  @all
end

#carma_case_idObject

Returns the value of attribute carma_case_id.



12
13
14
# File 'lib/carma/models/attachments.rb', line 12

def carma_case_id
  @carma_case_id
end

#has_errorsObject (readonly)

Returns the value of attribute has_errors.



9
10
11
# File 'lib/carma/models/attachments.rb', line 9

def has_errors
  @has_errors
end

#responseObject (readonly)

Returns the value of attribute response.



9
10
11
# File 'lib/carma/models/attachments.rb', line 9

def response
  @response
end

#veteran_first_nameObject

Returns the value of attribute veteran_first_name.



12
13
14
# File 'lib/carma/models/attachments.rb', line 12

def veteran_first_name
  @veteran_first_name
end

#veteran_last_nameObject

Returns the value of attribute veteran_last_name.



12
13
14
# File 'lib/carma/models/attachments.rb', line 12

def veteran_last_name
  @veteran_last_name
end

Instance Method Details

#add(document_type, local_path) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/carma/models/attachments.rb', line 24

def add(document_type, local_path)
  all << CARMA::Models::Attachment.new(
    carma_case_id:,
    document_type:,
    file_path: local_path,
    veteran_name: {
      first: veteran_first_name,
      last: veteran_last_name
    }
  )

  self
end

#submit!(client) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/carma/models/attachments.rb', line 46

def submit!(client)
  return response if response

  @response = client.upload_attachments(to_request_payload)

  @has_errors = @response['hasErrors']

  @all.each do |attachment|
    matching_result = @response['results'].find do |upload_result|
      upload_result['referenceId'] == attachment.reference_id
    end

    attachment.id = matching_result['id'] if matching_result
  end

  self
end

#to_hashObject



64
65
66
67
68
69
# File 'lib/carma/models/attachments.rb', line 64

def to_hash
  {
    has_errors:,
    data: all.map(&:to_hash)
  }
end

#to_request_payloadObject



38
39
40
41
42
43
44
# File 'lib/carma/models/attachments.rb', line 38

def to_request_payload
  raise 'must have at least one attachment' if all.empty?

  {
    'records' => all.map(&:to_request_payload)
  }
end