nuncium 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +15 -0
  3. data/.overcommit.yml +49 -0
  4. data/.rspec +2 -0
  5. data/.rubocop.yml +26 -0
  6. data/.ruby-gemset +1 -0
  7. data/.ruby-version +1 -0
  8. data/CODE_OF_CONDUCT.md +74 -0
  9. data/DOCS.md +133 -0
  10. data/Gemfile +6 -0
  11. data/Gemfile.lock +136 -0
  12. data/Guardfile +7 -0
  13. data/LICENSE.txt +21 -0
  14. data/README.md +281 -0
  15. data/Rakefile +6 -0
  16. data/_config.yml +1 -0
  17. data/bin/console +7 -0
  18. data/lib/nuncium.rb +87 -0
  19. data/lib/nuncium/configuration.rb +82 -0
  20. data/lib/nuncium/data_box.rb +41 -0
  21. data/lib/nuncium/data_message.rb +136 -0
  22. data/lib/nuncium/request.rb +120 -0
  23. data/lib/nuncium/response.rb +18 -0
  24. data/lib/nuncium/responses/body.rb +19 -0
  25. data/lib/nuncium/responses/data_box.rb +14 -0
  26. data/lib/nuncium/responses/db/body.rb +9 -0
  27. data/lib/nuncium/responses/db/status.rb +13 -0
  28. data/lib/nuncium/responses/dm/body.rb +9 -0
  29. data/lib/nuncium/responses/dm/status.rb +13 -0
  30. data/lib/nuncium/responses/message.rb +21 -0
  31. data/lib/nuncium/responses/messages/attachment.rb +24 -0
  32. data/lib/nuncium/responses/messages/collection.rb +29 -0
  33. data/lib/nuncium/responses/messages/delivery_info.rb +23 -0
  34. data/lib/nuncium/responses/messages/envelope.rb +23 -0
  35. data/lib/nuncium/responses/messages/event.rb +14 -0
  36. data/lib/nuncium/responses/status.rb +19 -0
  37. data/lib/nuncium/utils/errors.rb +18 -0
  38. data/lib/nuncium/version.rb +3 -0
  39. data/lib/nuncium/web_services/db_access/.gitkeep +0 -0
  40. data/lib/nuncium/web_services/db_access/change_isds_password.rb +21 -0
  41. data/lib/nuncium/web_services/db_access/get_owner_info_from_login.rb +19 -0
  42. data/lib/nuncium/web_services/db_search/check_data_box.rb +17 -0
  43. data/lib/nuncium/web_services/db_search/d_t_info.rb +17 -0
  44. data/lib/nuncium/web_services/db_search/data_box_credit_info.rb +17 -0
  45. data/lib/nuncium/web_services/db_search/find_data_box.rb +25 -0
  46. data/lib/nuncium/web_services/db_search/find_personal_data_box.rb +22 -0
  47. data/lib/nuncium/web_services/db_search/get_data_box_activity_status.rb +17 -0
  48. data/lib/nuncium/web_services/db_search/get_data_box_list.rb +20 -0
  49. data/lib/nuncium/web_services/db_search/isds_search_2.rb +18 -0
  50. data/lib/nuncium/web_services/db_search/p_d_z_info.rb +17 -0
  51. data/lib/nuncium/web_services/db_search/p_d_z_send_info.rb +17 -0
  52. data/lib/nuncium/web_services/db_search/request.rb +19 -0
  53. data/lib/nuncium/web_services/db_search/response.rb +13 -0
  54. data/lib/nuncium/web_services/dm_info/confirm_delivery.rb +17 -0
  55. data/lib/nuncium/web_services/dm_info/erase_message.rb +17 -0
  56. data/lib/nuncium/web_services/dm_info/get_delivery_info.rb +25 -0
  57. data/lib/nuncium/web_services/dm_info/get_list_of_received_messages.rb +26 -0
  58. data/lib/nuncium/web_services/dm_info/get_list_of_sent_messages.rb +26 -0
  59. data/lib/nuncium/web_services/dm_info/get_message_author.rb +17 -0
  60. data/lib/nuncium/web_services/dm_info/get_message_state_changes.rb +17 -0
  61. data/lib/nuncium/web_services/dm_info/get_signed_delivery_info.rb +17 -0
  62. data/lib/nuncium/web_services/dm_info/mark_message_as_downloaded.rb +17 -0
  63. data/lib/nuncium/web_services/dm_info/message_envelope_download.rb +25 -0
  64. data/lib/nuncium/web_services/dm_info/request.rb +19 -0
  65. data/lib/nuncium/web_services/dm_info/response.rb +13 -0
  66. data/lib/nuncium/web_services/dm_info/verify_message.rb +17 -0
  67. data/lib/nuncium/web_services/dm_operations/authenticate_message.rb +17 -0
  68. data/lib/nuncium/web_services/dm_operations/create_message.rb +30 -0
  69. data/lib/nuncium/web_services/dm_operations/create_multiple_messages.rb +30 -0
  70. data/lib/nuncium/web_services/dm_operations/message_download.rb +25 -0
  71. data/lib/nuncium/web_services/dm_operations/ping.rb +15 -0
  72. data/lib/nuncium/web_services/dm_operations/request.rb +19 -0
  73. data/lib/nuncium/web_services/dm_operations/resign_isds_document.rb +17 -0
  74. data/lib/nuncium/web_services/dm_operations/response.rb +13 -0
  75. data/lib/nuncium/web_services/dm_operations/signed_message_download.rb +17 -0
  76. data/lib/nuncium/web_services/dm_operations/signed_sent_message_download.rb +17 -0
  77. data/nuncium.gemspec +47 -0
  78. data/wercker.yml +12 -0
  79. metadata +303 -0
@@ -0,0 +1,41 @@
1
+ module Nuncium
2
+ class DataBox
3
+ def self.check(options = {})
4
+ ::Nuncium::WebServices::DbSearch::CheckDataBox
5
+ .call(options.merge(dbID: ::Nuncium.configuration.data_box))
6
+ end
7
+
8
+ def self.find_by(options = {})
9
+ ::Nuncium::WebServices::DbSearch::FindDataBox
10
+ .call(options)
11
+ end
12
+
13
+ def self.owner_info
14
+ ::Nuncium::WebServices::DbAccess::GetOwnerInfoFromLogin.call
15
+ end
16
+
17
+ def self.credit_info(options = {})
18
+ ::Nuncium::WebServices::DbSearch::DataBoxCreditInfo
19
+ .call(options.merge(dbID: ::Nuncium.configuration.data_box))
20
+ end
21
+
22
+ def self.received(options = {})
23
+ Nuncium::WebServices::DmInfo::GetListOfReceivedMessages
24
+ .call(options)
25
+ end
26
+
27
+ def self.sent(options = {})
28
+ Nuncium::WebServices::DmInfo::GetListOfSentMessages
29
+ .call(options)
30
+ end
31
+
32
+ def self.state_changes(options = {})
33
+ Nuncium::WebServices::DmInfo::GetMessageStateChanges
34
+ .call(options)
35
+ end
36
+
37
+ def self.ping
38
+ Nuncium::WebServices::DmOperations::Ping.call
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,136 @@
1
+ # rubocop:disable Metrics/ClassLength
2
+ module Nuncium
3
+ class DataMessage
4
+ ATTRIBUTES = %w[dmType dmOrdinal dmID dbIDSender dmSender
5
+ dmSenderAddress dmSenderType dmRecipient dmRecipientAddress
6
+ dmSenderOrgUnit dmSenderOrgUnitNum dbIDRecipient dmRecipientOrgUnit
7
+ dmRecipientOrgUnitNum dmToHands dmAnnotation dmRecipientRefNumber
8
+ dmSenderRefNumber dmRecipientIdent dmSenderIdent dmLegalTitleLaw
9
+ dmLegalTitleYear dmLegalTitleSect dmLegalTitlePar dmLegalTitlePoint
10
+ dmPersonalDelivery dmAllowSubstDelivery dmMessageStatus
11
+ dmAttachmentSize dmDeliveryTime dmAcceptanceTime dmHash
12
+ dmQTimestamp dmAttachments dmAmbiguousRecipient].freeze
13
+
14
+ attr_accessor(*ATTRIBUTES)
15
+
16
+ def self.find(dmID)
17
+ Nuncium::WebServices::DmOperations::MessageDownload
18
+ .call(dmID: dmID)
19
+ end
20
+
21
+ def download(destination = '/tmp')
22
+ path = "#{destination}/#{dmID}.zfo"
23
+ File.open(path, 'wb') do |f|
24
+ f.write(Base64.decode64(signed.body.dmSignature))
25
+ end
26
+ path
27
+ end
28
+
29
+ def initialize(params = {})
30
+ @params = params.dup
31
+ @status_info = @params.delete('dmDm')
32
+ @dmAttachments = load_attachments
33
+ @dmEvents = load_events
34
+ unified_params = @status_info ? @status_info.merge(@params) : params
35
+ unified_params.each do |key, value|
36
+ raise "Not valid attribute of DataMessage #{key}" unless ATTRIBUTES.include?(key)
37
+ instance_variable_set("@#{key}", parsed_value(value))
38
+ end
39
+ end
40
+
41
+ def verify
42
+ Nuncium::WebServices::DmInfo::VerifyMessage
43
+ .call(dmID: dmID)
44
+ end
45
+
46
+ def authenticate
47
+ Nuncium::WebServices::DmOperations::AuthenticateMessage
48
+ .call(dmMessage: signed.body.dmSignature)
49
+ end
50
+
51
+ def signed
52
+ Nuncium::WebServices::DmOperations::SignedMessageDownload
53
+ .call(dmID: dmID)
54
+ end
55
+
56
+ def author
57
+ Nuncium::WebServices::DmInfo::GetMessageAuthor
58
+ .call(dmID: dmID)
59
+ end
60
+
61
+ def delivery_info
62
+ Nuncium::WebServices::DmInfo::GetDeliveryInfo
63
+ .call(dmID: dmID)
64
+ end
65
+
66
+ def signed_delivery_info
67
+ Nuncium::WebServices::DmInfo::GetSignedDeliveryInfo
68
+ .call(dmID: dmID)
69
+ end
70
+
71
+ def envelope
72
+ Nuncium::WebServices::DmInfo::MessageEnvelopeDownload
73
+ .call(dmID: dmID)
74
+ end
75
+
76
+ def confirm_delivery
77
+ Nuncium::WebServices::DmInfo::ConfirmDelivery
78
+ .call(dmID: dmID)
79
+ end
80
+
81
+ def mark_as_downloaded
82
+ Nuncium::WebServices::DmInfo::MarkMessageAsDownloaded
83
+ .call(dmID: dmID)
84
+ end
85
+
86
+ def destroy(options = {})
87
+ Nuncium::WebServices::DmInfo::EraseMessage
88
+ .call(options.merge(dmID: dmID))
89
+ end
90
+
91
+ def sent?
92
+ dbIDSender == ::Nuncium.configuration.data_box
93
+ end
94
+
95
+ def received?
96
+ dbIDSender != ::Nuncium.configuration.data_box
97
+ end
98
+
99
+ private
100
+
101
+ def load_events
102
+ return {} if @params['dmEvents'].blank?
103
+ events_hash = @params.delete('dmEvents')['dmEvent']
104
+ if events_hash.is_a?(Array)
105
+ events_hash.map do |event|
106
+ ::Nuncium::Responses::Messages::Event.new(event)
107
+ end
108
+ else
109
+ ::Nuncium::Responses::Messages::Event.new(events_hash)
110
+ end
111
+ end
112
+
113
+ def load_attachments
114
+ return {} if @status_info&.dig('dmFiles').blank?
115
+ attachments_hash = @status_info.delete('dmFiles')['dmFile']
116
+ if attachments_hash.is_a?(Array)
117
+ attachments_hash.map do |attachment|
118
+ ::Nuncium::Responses::Messages::Attachment.new(attachment)
119
+ end
120
+ else
121
+ [::Nuncium::Responses::Messages::Attachment.new(attachments_hash)]
122
+ end
123
+ end
124
+
125
+ def parsed_value(value)
126
+ return value unless value.is_a?(Hash)
127
+ raise "Missing parse rule for #{value}" if value.keys.size > 1
128
+ if value.keys.first == 'xsi:nil' && value.values.first == 'true'
129
+ nil
130
+ else
131
+ false
132
+ end
133
+ end
134
+ end
135
+ end
136
+ # rubocop:enable Metrics/ClassLength
@@ -0,0 +1,120 @@
1
+ module Nuncium
2
+ class Request
3
+ def initialize(params = {})
4
+ params.each do |k, v|
5
+ instance_variable_set("@#{k}", v)
6
+ end if params.any?
7
+ end
8
+
9
+ def self.call(params = {})
10
+ new(params).call
11
+ end
12
+
13
+ # rubocop:disable Metrics/AbcSize
14
+ # rubocop:disable Metrics/MethodLength
15
+ def call
16
+ uri = URI(full_url)
17
+
18
+ request = Net::HTTP::Post.new(uri)
19
+ default_headers.each { |k, v| request[k] = v }
20
+ request.body = to_xml
21
+
22
+ https = Net::HTTP.new(uri.hostname, uri.port)
23
+ https.use_ssl = true
24
+ https.ssl_version = :TLSv1_2_client
25
+ if Nuncium.configuration.cert_file
26
+ https.verify_mode = OpenSSL::SSL::VERIFY_PEER
27
+ https.cert = Nuncium.configuration.cert_file
28
+ https.key = Nuncium.configuration.private_key
29
+ https.verify_depth = 5
30
+ request.basic_auth(Nuncium.configuration.data_box, '')
31
+ else
32
+ request.basic_auth(
33
+ Nuncium.configuration.username,
34
+ Nuncium.configuration.password
35
+ )
36
+ end
37
+ response = https.request(request)
38
+ call_reponse_wrapper(response)
39
+ end
40
+ # rubocop:enable Metrics/MethodLength
41
+ # rubocop:enable Metrics/AbcSize
42
+
43
+ def call_reponse_wrapper(response)
44
+ response_wrapper.new(response)
45
+ end
46
+
47
+ def default_headers
48
+ {
49
+ 'Content-Type' => 'text/xml;charset=UTF-8',
50
+ 'Accept-Encoding' => 'gzip,deflate'
51
+ }
52
+ end
53
+
54
+ def to_xml
55
+ Nokogiri::XML::Builder.new(encoding: 'utf-8') do |xml|
56
+ xml[:soapenv].Envelope(envelope_namespaces) do
57
+ xml[:soapenv].Header
58
+ xml[:soapenv].Body do
59
+ body(xml)
60
+ end
61
+ end
62
+ end.to_xml
63
+ end
64
+
65
+ def envelope_namespaces
66
+ {
67
+ 'xmlns:soapenv' => 'http://schemas.xmlsoap.org/soap/envelope/',
68
+ 'xmlns:v20' => xml_url
69
+ }
70
+ end
71
+
72
+ def full_url
73
+ "#{Nuncium.configuration.api_domain}#{api_url}"
74
+ end
75
+
76
+ ##
77
+ # each subclass needs to have availbale attrs put in constant `ATTRS`
78
+ #
79
+ def values(xml)
80
+ self.class::ATTRS.each do |attribute|
81
+ value = instance_variable_get("@#{attribute}") || ''
82
+ xml[:v20].public_send(attribute, value)
83
+ end
84
+ end
85
+
86
+ ##
87
+ # Each subclass must implement
88
+ # this is whole body of message which differs and also needs to be
89
+ # accompanied with att_accessors
90
+ #
91
+ def body
92
+ raise NotImplementedError, "#{self.class} must implement #body!"
93
+ end
94
+
95
+ ##
96
+ # relative part of url because they differ...
97
+ # domain such as https://ws1.czebox.cz is take from config as such:
98
+ # `Nuncium.configuration.api_domain`
99
+ # it includes `https://` so only ending is supposed to be here
100
+ #
101
+ def api_url
102
+ raise NotImplementedError, "#{self.class} must implement #api_url!"
103
+ end
104
+
105
+ ##
106
+ # URL will differ based on endpoint that it accesses
107
+ #
108
+ def xml_url
109
+ "https://#{Nuncium.configuration.xml_url}"
110
+ end
111
+
112
+ ##
113
+ # Here you can change which response object will wrap the reponse
114
+ # reponse object has to inherit from `::Nuncium::Response`
115
+ #
116
+ def response_wrapper
117
+ ::Nuncium::Response
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,18 @@
1
+ module Nuncium
2
+ class Response
3
+ attr_accessor :status, :body
4
+
5
+ def initialize(response)
6
+ @response = Hash.from_xml(response.body)
7
+ end
8
+
9
+ def parsed_body
10
+ raise HtmlResponseReceived, @response.inspect unless @response['Envelope'].present?
11
+ @response['Envelope']['Body'][result_key]
12
+ end
13
+
14
+ def result_key
15
+ @response['Envelope']['Body'].first[0]
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,19 @@
1
+ module Nuncium
2
+ module Responses
3
+ class Body
4
+ def initialize(response)
5
+ (response.keys - self.class::REMOVED_KEYS).each do |key|
6
+ self.class.send(:attr_accessor, key.to_sym)
7
+ instance_variable_set("@#{key}", parsed_value(response[key]))
8
+ end
9
+ end
10
+
11
+ private
12
+
13
+ def parsed_value(value)
14
+ return value unless value.is_a?(Hash)
15
+ value.keys.first == 'xsi:nil' && value.values.first == 'true' ? nil : false
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,14 @@
1
+ module Nuncium
2
+ module Responses
3
+ class DataBox < Body
4
+ REMOVED_KEYS = %w[xmlns:p xmlns:xsi dbStatus].freeze
5
+
6
+ def initialize(response)
7
+ (response.keys - REMOVED_KEYS).each do |key|
8
+ self.class.send(:attr_accessor, key.to_sym)
9
+ instance_variable_set("@#{key}", response[key])
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,9 @@
1
+ module Nuncium
2
+ module Responses
3
+ module Db
4
+ class Body < ::Nuncium::Responses::Body
5
+ REMOVED_KEYS = %w[xmlns:p xmlns:xsi dbStatus].freeze
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,13 @@
1
+ module Nuncium
2
+ module Responses
3
+ module Db
4
+ class Status < ::Nuncium::Responses::Status
5
+ private
6
+
7
+ def namespace
8
+ 'db'
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ module Nuncium
2
+ module Responses
3
+ module Dm
4
+ class Body < ::Nuncium::Responses::Body
5
+ REMOVED_KEYS = %w[xmlns:q xmlns:p xmlns:xsi dmStatus].freeze
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,13 @@
1
+ module Nuncium
2
+ module Responses
3
+ module Dm
4
+ class Status < ::Nuncium::Responses::Status
5
+ private
6
+
7
+ def namespace
8
+ 'dm'
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,21 @@
1
+ module Nuncium
2
+ module Responses
3
+ class Message < ::Nuncium::Response
4
+ def initialize(response)
5
+ super
6
+ end
7
+
8
+ def message
9
+ ::Nuncium::DataMessage.new(message_hash)
10
+ end
11
+
12
+ private
13
+
14
+ def message_hash
15
+ hash = parsed_body['dmReturnedMessage']
16
+ hash['dmDm'].delete('xmlns:p')
17
+ hash
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,24 @@
1
+ module Nuncium
2
+ module Responses
3
+ module Messages
4
+ class Attachment
5
+ attr_accessor :file_type, :file_name, :encoded_content, :decoded_content
6
+
7
+ def initialize(hash)
8
+ @file_type = hash['dmMimeType']
9
+ @file_name = hash['dmFileDescr']
10
+ @encoded_content = hash['dmEncodedContent']
11
+ @decoded_content = Base64.decode64(@encoded_content)
12
+ end
13
+
14
+ def download(destination = '/tmp')
15
+ file_destination = "#{destination}/#{@file_name}"
16
+ File.open(file_destination, 'wb') do |f|
17
+ f.write(@decoded_content)
18
+ end
19
+ file_destination
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end