pandexio 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,308 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ require 'stringio'
4
+ require 'open-uri'
5
+ require_relative 'request.rb'
6
+ require_relative 'scope_patterns.rb'
7
+ require_relative 'signer.rb'
8
+ require_relative 'signing_mechanisms.rb'
9
+
10
+ module Pandexio
11
+
12
+ HOSTED_HOST = "hosted.pandexio.com"
13
+ PLATFORM_HOST = "platform.pandexio.com"
14
+
15
+ class DocumentSource
16
+
17
+ def initialize(params = {})
18
+ @id = params.fetch(:id, nil)
19
+ @name = params.fetch(:name, nil)
20
+ @content = params.fetch(:content, nil)
21
+ @location = params.fetch(:location, nil)
22
+ end
23
+
24
+ attr_accessor :id
25
+ attr_accessor :name
26
+ attr_accessor :content
27
+ attr_accessor :location
28
+
29
+ end
30
+
31
+ class Document
32
+
33
+ def initialize(params = {})
34
+ @document_id = params.fetch(:document_id, nil)
35
+ @name = params.fetch(:name, nil)
36
+ @page_count = params.fetch(:page_count, nil)
37
+ @content_type = params.fetch(:content_type, nil)
38
+ @content_length = params.fetch(:content_length, nil)
39
+ @cover = params.fetch(:cover, nil)
40
+ end
41
+
42
+ attr_accessor :document_id
43
+ attr_accessor :name
44
+ attr_accessor :page_count
45
+ attr_accessor :content_type
46
+ attr_accessor :content_length
47
+ attr_accessor :cover
48
+
49
+ end
50
+
51
+ class HttpClient
52
+
53
+ private
54
+
55
+ def build_headers(headers, content_type)
56
+
57
+ items = {'Content-Type' => content_type}
58
+
59
+ headers.each do |key, value|
60
+ items[key] = value.to_s
61
+ end
62
+
63
+ return items
64
+ end
65
+
66
+ def build_query_string(query_parameters)
67
+
68
+ query_string_builder = StringIO.new
69
+
70
+ query_parameters.each do |key, value|
71
+ query_string_builder << "&" if query_string_builder.length > 0
72
+ encodedKey = URI::encode(key.to_s)
73
+ encodedValue = URI::encode(value.to_s)
74
+ query_string_builder << "#{encodedKey}=#{encodedValue}"
75
+ end
76
+
77
+ return query_string_builder.string
78
+ end
79
+
80
+ public
81
+
82
+ def load_document(source_document, signing_options)
83
+
84
+ raise ArgumentError, "Document source is nil." if source_document.nil?
85
+ raise ArgumentError, "Signing options is nil." if signing_options.nil?
86
+
87
+ id = source_document.id.to_s
88
+ name = source_document.name.to_s
89
+ content = source_document.content.to_s
90
+ location = source_document.location.to_s
91
+
92
+ raise ArgumentError, "Document ID is not a string, is nil or empty, or does not match RegEx pattern: #{Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN}." if !source_document.id.is_a?(String) || id.nil? || id.empty? || !id.strip!.nil? || id.match(Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN).nil?
93
+ raise ArgumentError, "Document name is not a string or is nil or empty." if !source_document.name.is_a?(String) || name.nil? || name.empty? || !name.strip!.nil?
94
+ raise ArgumentError, "Document content is not a string or is nil or empty." if !source_document.content.is_a?(String) || content.nil? || content.empty? || !content.strip!.nil?
95
+ raise ArgumentError, "Document location must be AWS, Azure, Internet, or Inline." unless location == "AWS" || location == "Azure" || location == "Internet" || location == "Inline"
96
+
97
+ path = "/api/documents/#{id}"
98
+ payload = {
99
+ "name" => name,
100
+ "content" => content,
101
+ "location" => location
102
+ }.to_json
103
+
104
+ normalized_request = Pandexio::Request.new(
105
+ :method => "PUT",
106
+ :path => path,
107
+ :query_parameters => { },
108
+ :headers => { "Host" => HOSTED_HOST },
109
+ :payload => payload)
110
+
111
+ signer = Pandexio::Signer.new()
112
+
113
+ authorized_request = signer.sign(normalized_request, signing_options)
114
+
115
+ headers = build_headers(authorized_request.headers, 'application/json; charset=utf-8')
116
+ query_string = build_query_string(authorized_request.query_parameters)
117
+
118
+ http = Net::HTTP.new(HOSTED_HOST, 443)
119
+ http.use_ssl = true
120
+ http.open_timeout = 60
121
+ http.read_timeout = 600
122
+
123
+ req = Net::HTTP::Put.new("#{path}?#{query_string}", initheader = headers)
124
+ req.body = payload
125
+ res = http.start {|http| http.request(req)}
126
+
127
+ case res.code
128
+ when "202"
129
+ data = JSON.parse(res.body)
130
+ document = Pandexio::Document.new(
131
+ :document_id => data['documentId'],
132
+ :name => data['name'],
133
+ :page_count => data['pageCount'],
134
+ :content_type => data['contentType'],
135
+ :content_length => data['contentLength'],
136
+ :cover => data['cover'])
137
+ else
138
+ raise StandardError, "Failed to load document."
139
+ end
140
+
141
+ end
142
+
143
+ def get_document(document_id, cover_size, signing_options)
144
+
145
+ raise ArgumentError, "Signing options is nil." if signing_options.nil?
146
+
147
+ id = document_id.to_s
148
+
149
+ raise ArgumentError, "Document ID is not a string, is nil or empty, or does not match RegEx pattern: #{Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN}." if !document_id.is_a?(String) || id.nil? || id.empty? || !id.strip!.nil? || id.match(Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN).nil?
150
+ raise ArgumentError, "Document cover size must be xs, s, m, l, or xl." unless cover_size == 'xs' || cover_size == 's' || cover_size == 'm' || cover_size == 'l' || cover_size == 'xl'
151
+
152
+ path = "/v2/documents/#{id}"
153
+
154
+ normalized_request = Pandexio::Request.new(
155
+ :method => "GET",
156
+ :path => path,
157
+ :query_parameters => { "coverSize" => cover_size },
158
+ :headers => { "Host" => PLATFORM_HOST },
159
+ :payload => '')
160
+
161
+ signer = Pandexio::Signer.new()
162
+
163
+ authorized_request = signer.sign(normalized_request, signing_options)
164
+
165
+ headers = build_headers(authorized_request.headers, 'application/json; charset=utf-8')
166
+ query_string = build_query_string(authorized_request.query_parameters)
167
+
168
+ http = Net::HTTP.new(PLATFORM_HOST, 443)
169
+ http.use_ssl = true
170
+ http.open_timeout = 60
171
+ http.read_timeout = 120
172
+
173
+ req = Net::HTTP::Get.new("#{path}?#{query_string}", initheader = headers)
174
+ res = http.start {|http| http.request(req)}
175
+
176
+ case res.code
177
+ when "200"
178
+ data = JSON.parse(res.body)
179
+ document = Pandexio::Document.new(
180
+ :document_id => data['documentId'],
181
+ :name => data['name'],
182
+ :page_count => data['pageCount'],
183
+ :content_type => data['contentType'],
184
+ :content_length => data['contentLength'],
185
+ :cover => data['cover'])
186
+ when "404"
187
+ return nil
188
+ else
189
+ raise StandardError, "Failed to get document."
190
+ end
191
+
192
+ end
193
+
194
+ def get_document_cover(document_id, cover_size, signing_options)
195
+
196
+ raise ArgumentError, "Signing options is nil." if signing_options.nil?
197
+
198
+ id = document_id.to_s
199
+
200
+ raise ArgumentError, "Document ID is not a string, is nil or empty, or does not match RegEx pattern: #{Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN}." if !document_id.is_a?(String) || id.nil? || id.empty? || !id.strip!.nil? || id.match(Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN).nil?
201
+ raise ArgumentError, "Document cover size must be xs, s, m, l, or xl." unless cover_size == 'xs' || cover_size == 's' || cover_size == 'm' || cover_size == 'l' || cover_size == 'xl'
202
+
203
+ path = "/v2/documents/#{id}/cover"
204
+
205
+ normalized_request = Pandexio::Request.new(
206
+ :method => "GET",
207
+ :path => path,
208
+ :query_parameters => { "coverSize" => cover_size },
209
+ :headers => { "Host" => PLATFORM_HOST },
210
+ :payload => '')
211
+
212
+ signer = Pandexio::Signer.new()
213
+
214
+ authorized_request = signer.sign(normalized_request, signing_options)
215
+
216
+ headers = build_headers(authorized_request.headers, 'image/png')
217
+ query_string = build_query_string(authorized_request.query_parameters)
218
+
219
+ http = Net::HTTP.new(PLATFORM_HOST, 443)
220
+ http.use_ssl = true
221
+ http.open_timeout = 60
222
+ http.read_timeout = 120
223
+
224
+ req = Net::HTTP::Get.new("#{path}?#{query_string}", initheader = headers)
225
+ res = http.start {|http| http.request(req)}
226
+
227
+ case res.code
228
+ when "200"
229
+ return res.body
230
+ when "404"
231
+ return nil
232
+ else
233
+ raise StandardError, "Failed to get document cover."
234
+ end
235
+
236
+ end
237
+
238
+ def get_document_cover_url(document_id, cover_size, signing_options)
239
+
240
+ id = document_id.to_s
241
+
242
+ raise ArgumentError, "Document ID is not a string, is nil or empty, or does not match RegEx pattern: #{Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN}." if !document_id.is_a?(String) || id.nil? || id.empty? || !id.strip!.nil? || id.match(Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN).nil?
243
+ raise ArgumentError, "Document cover size must be xs, s, m, l, or xl." unless cover_size == 'xs' || cover_size == 's' || cover_size == 'm' || cover_size == 'l' || cover_size == 'xl'
244
+
245
+ raise ArgumentError, "Signing options is nil." if signing_options.nil?
246
+ raise ArgumentError, "Signing mechanism must be QueryString." unless signing_options.mechanism == Pandexio::SigningMechanisms::QUERY_STRING
247
+
248
+ path = "/v2/documents/#{id}/cover"
249
+
250
+ normalized_request = Pandexio::Request.new(
251
+ :method => "GET",
252
+ :path => path,
253
+ :query_parameters => { "coverSize" => cover_size },
254
+ :headers => { "Host" => PLATFORM_HOST },
255
+ :payload => '')
256
+
257
+ signer = Pandexio::Signer.new()
258
+
259
+ authorized_request = signer.sign(normalized_request, signing_options)
260
+
261
+ query_string = build_query_string(authorized_request.query_parameters)
262
+
263
+ return "https://#{PLATFORM_HOST}#{path}?#{query_string}"
264
+
265
+ end
266
+
267
+ def get_document_snip_count(document_id, signing_options)
268
+
269
+ raise ArgumentError, "Signing options is nil." if signing_options.nil?
270
+
271
+ id = document_id.to_s
272
+
273
+ raise ArgumentError, "Document ID is not a string, is nil or empty, or does not match RegEx pattern: #{Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN}." if !document_id.is_a?(String) || id.nil? || id.empty? || !id.strip!.nil? || id.match(Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN).nil?
274
+
275
+ path = "/v2/documents/#{id}/snips/count"
276
+
277
+ normalized_request = Pandexio::Request.new(
278
+ :method => "GET",
279
+ :path => path,
280
+ :query_parameters => {},
281
+ :headers => { "Host" => PLATFORM_HOST },
282
+ :payload => '')
283
+
284
+ signer = Pandexio::Signer.new()
285
+
286
+ authorized_request = signer.sign(normalized_request, signing_options)
287
+
288
+ headers = build_headers(authorized_request.headers, 'application/json; charset=utf-8')
289
+ query_string = build_query_string(authorized_request.query_parameters)
290
+
291
+ http = Net::HTTP.new(PLATFORM_HOST, 443)
292
+ http.use_ssl = true
293
+
294
+ req = Net::HTTP::Get.new("#{path}?#{query_string}", initheader = headers)
295
+ res = http.start {|http| http.request(req)}
296
+
297
+ case res.code
298
+ when "200"
299
+ return res.body.to_i
300
+ else
301
+ raise StandardError, "Failed to get document snip count."
302
+ end
303
+
304
+ end
305
+
306
+ end
307
+
308
+ end
data/lib/request.rb CHANGED
@@ -2,6 +2,9 @@ module Pandexio
2
2
 
3
3
  class Request
4
4
 
5
+ LINE_BREAK = "\r\n"
6
+ private_constant :LINE_BREAK
7
+
5
8
  def initialize(params = {})
6
9
  @method = params.fetch(:method, nil)
7
10
  @path = params.fetch(:path, nil)
@@ -1,6 +1,7 @@
1
1
  module Pandexio
2
2
 
3
3
  class ScopePatterns
4
+ DOCUMENT_ID_PATTERN = /^[a-zA-Z0-9\-_]+$/i
4
5
  DOCUMENT_PATH_PATTERN = /(\/.*?documents\/)(?<documentid>[a-zA-Z0-9\-_]+)/i
5
6
  end
6
7
 
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  require 'minitest/autorun'
4
- require_relative '../lib/pandexio.rb'
4
+ require 'pandexio'
5
5
 
6
6
  describe Pandexio do
7
7
  describe "#to_authorized_request" do
@@ -0,0 +1,1238 @@
1
+ # encoding: utf-8
2
+
3
+ require 'time'
4
+ require 'uuid'
5
+ require 'minitest/autorun'
6
+ #require 'pandexio'
7
+ require_relative 'test_config.rb'
8
+ require_relative '../lib/http_client.rb'
9
+ require_relative '../lib/scope_patterns.rb'
10
+ require_relative '../lib/signing_options.rb'
11
+ require_relative '../lib/signing_algorithms.rb'
12
+ require_relative '../lib/signing_mechanisms.rb'
13
+
14
+ describe Pandexio::HttpClient do
15
+
16
+ describe "#load_document" do
17
+
18
+ describe "when id is nil" do
19
+
20
+ before do
21
+ document_source = Pandexio::DocumentSource.new(:id => nil, :name => "test_name", :content => "test_content", :location => "Inline")
22
+ signing_options = Pandexio::SigningOptions.new()
23
+ http_client = Pandexio::HttpClient.new()
24
+ @load_document = ->{ http_client.load_document(document_source, signing_options) }
25
+ end
26
+
27
+ it "should raise ArgumentError" do
28
+ err = @load_document.must_raise ArgumentError
29
+ err.message.must_match "Document ID is not a string, is nil or empty, or does not match RegEx pattern: #{Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN}."
30
+ end
31
+
32
+ end
33
+
34
+ describe "when id is empty" do
35
+
36
+ before do
37
+ document_source = Pandexio::DocumentSource.new(:id => "", :name => "test_name", :content => "test_content", :location => "Inline")
38
+ signing_options = Pandexio::SigningOptions.new()
39
+ http_client = Pandexio::HttpClient.new()
40
+ @load_document = ->{ http_client.load_document(document_source, signing_options) }
41
+ end
42
+
43
+ it "should raise ArgumentError" do
44
+ err = @load_document.must_raise ArgumentError
45
+ err.message.must_match "Document ID is not a string, is nil or empty, or does not match RegEx pattern: #{Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN}."
46
+ end
47
+
48
+ end
49
+
50
+ describe "when id is blank" do
51
+
52
+ before do
53
+ document_source = Pandexio::DocumentSource.new(:id => " ", :name => "test_name", :content => "test_content", :location => "Inline")
54
+ signing_options = Pandexio::SigningOptions.new()
55
+ http_client = Pandexio::HttpClient.new()
56
+ @load_document = ->{ http_client.load_document(document_source, signing_options) }
57
+ end
58
+
59
+ it "should raise ArgumentError" do
60
+ err = @load_document.must_raise ArgumentError
61
+ err.message.must_match "Document ID is not a string, is nil or empty, or does not match RegEx pattern: #{Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN}."
62
+ end
63
+
64
+ end
65
+
66
+ describe "when id is not a string" do
67
+
68
+ before do
69
+ document_source = Pandexio::DocumentSource.new(:id => 123, :name => "test_name", :content => "test_content", :location => "Inline")
70
+ signing_options = Pandexio::SigningOptions.new()
71
+ http_client = Pandexio::HttpClient.new()
72
+ @load_document = ->{ http_client.load_document(document_source, signing_options) }
73
+ end
74
+
75
+ it "should raise ArgumentError" do
76
+ err = @load_document.must_raise ArgumentError
77
+ err.message.must_match "Document ID is not a string, is nil or empty, or does not match RegEx pattern: #{Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN}."
78
+ end
79
+
80
+ end
81
+
82
+ describe "when id does not match RegEx" do
83
+
84
+ before do
85
+ document_source = Pandexio::DocumentSource.new(:id => "123^%$*abc", :name => "test_name", :content => "test_content", :location => "Inline")
86
+ signing_options = Pandexio::SigningOptions.new()
87
+ http_client = Pandexio::HttpClient.new()
88
+ @load_document = ->{ http_client.load_document(document_source, signing_options) }
89
+ end
90
+
91
+ it "should raise ArgumentError" do
92
+ err = @load_document.must_raise ArgumentError
93
+ err.message.must_match "Document ID is not a string, is nil or empty, or does not match RegEx pattern: #{Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN}."
94
+ end
95
+
96
+ end
97
+
98
+ describe "when name is nil" do
99
+
100
+ before do
101
+ document_source = Pandexio::DocumentSource.new(:id => "test_id", :name => nil, :content => "test_content", :location => "Inline")
102
+ signing_options = Pandexio::SigningOptions.new()
103
+ http_client = Pandexio::HttpClient.new()
104
+ @load_document = ->{ http_client.load_document(document_source, signing_options) }
105
+ end
106
+
107
+ it "should raise ArgumentError" do
108
+ err = @load_document.must_raise ArgumentError
109
+ err.message.must_match "Document name is not a string or is nil or empty."
110
+ end
111
+
112
+ end
113
+
114
+ describe "when name is empty" do
115
+
116
+ before do
117
+ document_source = Pandexio::DocumentSource.new(:id => "test_id", :name => "", :content => "test_content", :location => "Inline")
118
+ signing_options = Pandexio::SigningOptions.new()
119
+ http_client = Pandexio::HttpClient.new()
120
+ @load_document = ->{ http_client.load_document(document_source, signing_options) }
121
+ end
122
+
123
+ it "should raise ArgumentError" do
124
+ err = @load_document.must_raise ArgumentError
125
+ err.message.must_match "Document name is not a string or is nil or empty."
126
+ end
127
+
128
+ end
129
+
130
+ describe "when name is blank" do
131
+
132
+ before do
133
+ document_source = Pandexio::DocumentSource.new(:id => "test_id", :name => " ", :content => "test_content", :location => "Inline")
134
+ signing_options = Pandexio::SigningOptions.new()
135
+ http_client = Pandexio::HttpClient.new()
136
+ @load_document = ->{ http_client.load_document(document_source, signing_options) }
137
+ end
138
+
139
+ it "should raise ArgumentError" do
140
+ err = @load_document.must_raise ArgumentError
141
+ err.message.must_match "Document name is not a string or is nil or empty."
142
+ end
143
+
144
+ end
145
+
146
+ describe "when name is not a string" do
147
+
148
+ before do
149
+ document_source = Pandexio::DocumentSource.new(:id => "test_id", :name => 123, :content => "test_content", :location => "Inline")
150
+ signing_options = Pandexio::SigningOptions.new()
151
+ http_client = Pandexio::HttpClient.new()
152
+ @load_document = ->{ http_client.load_document(document_source, signing_options) }
153
+ end
154
+
155
+ it "should raise ArgumentError" do
156
+ err = @load_document.must_raise ArgumentError
157
+ err.message.must_match "Document name is not a string or is nil or empty."
158
+ end
159
+
160
+ end
161
+
162
+ describe "when content is nil" do
163
+
164
+ before do
165
+ document_source = Pandexio::DocumentSource.new(:id => "test_id", :name => "test_name", :content => nil, :location => "Inline")
166
+ signing_options = Pandexio::SigningOptions.new()
167
+ http_client = Pandexio::HttpClient.new()
168
+ @load_document = ->{ http_client.load_document(document_source, signing_options) }
169
+ end
170
+
171
+ it "should raise ArgumentError" do
172
+ err = @load_document.must_raise ArgumentError
173
+ err.message.must_match "Document content is not a string or is nil or empty."
174
+ end
175
+
176
+ end
177
+
178
+ describe "when content is empty" do
179
+
180
+ before do
181
+ document_source = Pandexio::DocumentSource.new(:id => "test_id", :name => "test_name", :content => "", :location => "Inline")
182
+ signing_options = Pandexio::SigningOptions.new()
183
+ http_client = Pandexio::HttpClient.new()
184
+ @load_document = ->{ http_client.load_document(document_source, signing_options) }
185
+ end
186
+
187
+ it "should raise ArgumentError" do
188
+ err = @load_document.must_raise ArgumentError
189
+ err.message.must_match "Document content is not a string or is nil or empty."
190
+ end
191
+
192
+ end
193
+
194
+ describe "when content is blank" do
195
+
196
+ before do
197
+ document_source = Pandexio::DocumentSource.new(:id => "test_id", :name => "test_name", :content => " ", :location => "Inline")
198
+ signing_options = Pandexio::SigningOptions.new()
199
+ http_client = Pandexio::HttpClient.new()
200
+ @load_document = ->{ http_client.load_document(document_source, signing_options) }
201
+ end
202
+
203
+ it "should raise ArgumentError" do
204
+ err = @load_document.must_raise ArgumentError
205
+ err.message.must_match "Document content is not a string or is nil or empty."
206
+ end
207
+
208
+ end
209
+
210
+ describe "when content is not a string" do
211
+
212
+ before do
213
+ document_source = Pandexio::DocumentSource.new(:id => "test_id", :name => "test_name", :content => 123, :location => "Inline")
214
+ signing_options = Pandexio::SigningOptions.new()
215
+ http_client = Pandexio::HttpClient.new()
216
+ @load_document = ->{ http_client.load_document(document_source, signing_options) }
217
+ end
218
+
219
+ it "should raise ArgumentError" do
220
+ err = @load_document.must_raise ArgumentError
221
+ err.message.must_match "Document content is not a string or is nil or empty."
222
+ end
223
+
224
+ end
225
+
226
+ describe "when location is not AWS" do
227
+
228
+ before do
229
+ document_source = Pandexio::DocumentSource.new(:id => "test_id", :name => "test_name", :content => "test_content", :location => "AUUS")
230
+ signing_options = Pandexio::SigningOptions.new()
231
+ http_client = Pandexio::HttpClient.new()
232
+ @load_document = ->{ http_client.load_document(document_source, signing_options) }
233
+ end
234
+
235
+ it "should raise ArgumentError" do
236
+ err = @load_document.must_raise ArgumentError
237
+ err.message.must_match "Document location must be AWS, Azure, Internet, or Inline."
238
+ end
239
+
240
+ end
241
+
242
+ describe "when content is data URL" do
243
+
244
+ before do
245
+ skip("skip integration tests") if TestConfig::SKIP_INTEGRATION_TESTS
246
+ @id = UUID.new.generate
247
+ document_source = Pandexio::DocumentSource.new(
248
+ :id => @id,
249
+ :name => "test_name_#{@id}.txt",
250
+ :content => "data:text/plain;base64,dGVzdGluZyAxMjM=",
251
+ :location => "Inline")
252
+ signing_options = Pandexio::SigningOptions.new(
253
+ :algorithm => Pandexio::SigningAlgorithms::PDX_HMAC_SHA256,
254
+ :mechanism => Pandexio::SigningMechanisms::HEADER,
255
+ :domain_id => TestConfig::DOMAIN_ID,
256
+ :domain_key => TestConfig::DOMAIN_KEY,
257
+ :date => Time.now.utc,
258
+ :expires => 90,
259
+ :originator => "HttpClientTest",
260
+ :email_address => "HttpClientTest",
261
+ :display_name => "HttpClientTest")
262
+ http_client = Pandexio::HttpClient.new()
263
+ @load_document = ->{ http_client.load_document(document_source, signing_options) }
264
+ end
265
+
266
+ it "should return document details" do
267
+ doc = @load_document.call
268
+ doc.document_id.must_equal @id
269
+ doc.name.must_equal "test_name_#{@id}.txt"
270
+ doc.page_count.must_equal 1
271
+ doc.content_type.must_equal "text/plain"
272
+ doc.content_length.must_equal 11
273
+ doc.cover[0..29].must_equal 'data:image/png;base64,iVBORw0K'
274
+ doc.cover.length.must_equal 24846
275
+ end
276
+
277
+ end
278
+
279
+ describe "when content is HTTP URL" do
280
+
281
+ before do
282
+ skip("skip integration tests") if TestConfig::SKIP_INTEGRATION_TESTS
283
+ @id = UUID.new.generate
284
+ document_source = Pandexio::DocumentSource.new(
285
+ :id => @id,
286
+ :name => "test_name_#{@id}.pdf",
287
+ :content => "http://martinfowler.com/ieeeSoftware/whenType.pdf",
288
+ :location => "Internet")
289
+ signing_options = Pandexio::SigningOptions.new(
290
+ :algorithm => Pandexio::SigningAlgorithms::PDX_HMAC_SHA256,
291
+ :mechanism => Pandexio::SigningMechanisms::HEADER,
292
+ :domain_id => TestConfig::DOMAIN_ID,
293
+ :domain_key => TestConfig::DOMAIN_KEY,
294
+ :date => Time.now.utc,
295
+ :expires => 90,
296
+ :originator => "HttpClientTest",
297
+ :email_address => "HttpClientTest",
298
+ :display_name => "HttpClientTest")
299
+ http_client = Pandexio::HttpClient.new()
300
+ @load_document = ->{ http_client.load_document(document_source, signing_options) }
301
+ end
302
+
303
+ it "should return document details" do
304
+ doc = @load_document.call
305
+ doc.document_id.must_equal @id
306
+ doc.name.must_equal "test_name_#{@id}.pdf"
307
+ doc.page_count.must_equal 2
308
+ doc.content_type.must_equal "application/pdf"
309
+ doc.content_length.must_equal 289041
310
+ doc.cover[0..29].must_equal 'data:image/png;base64,iVBORw0K'
311
+ doc.cover.length.must_equal 436650
312
+ end
313
+
314
+ end
315
+
316
+ end
317
+
318
+ describe "#get_document" do
319
+
320
+ describe "when id is nil" do
321
+
322
+ before do
323
+ signing_options = Pandexio::SigningOptions.new()
324
+ http_client = Pandexio::HttpClient.new()
325
+ @get_document = ->{ http_client.get_document(nil, 'm', signing_options) }
326
+ end
327
+
328
+ it "should raise ArgumentError" do
329
+ err = @get_document.must_raise ArgumentError
330
+ err.message.must_match "Document ID is not a string, is nil or empty, or does not match RegEx pattern: #{Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN}."
331
+ end
332
+
333
+ end
334
+
335
+ describe "when id is empty" do
336
+
337
+ before do
338
+ signing_options = Pandexio::SigningOptions.new()
339
+ http_client = Pandexio::HttpClient.new()
340
+ @get_document = ->{ http_client.get_document("", 'm', signing_options) }
341
+ end
342
+
343
+ it "should raise ArgumentError" do
344
+ err = @get_document.must_raise ArgumentError
345
+ err.message.must_match "Document ID is not a string, is nil or empty, or does not match RegEx pattern: #{Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN}."
346
+ end
347
+
348
+ end
349
+
350
+ describe "when id is blank" do
351
+
352
+ before do
353
+ signing_options = Pandexio::SigningOptions.new()
354
+ http_client = Pandexio::HttpClient.new()
355
+ @get_document = ->{ http_client.get_document(" ", 'm', signing_options) }
356
+ end
357
+
358
+ it "should raise ArgumentError" do
359
+ err = @get_document.must_raise ArgumentError
360
+ err.message.must_match "Document ID is not a string, is nil or empty, or does not match RegEx pattern: #{Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN}."
361
+ end
362
+
363
+ end
364
+
365
+ describe "when id is not a string" do
366
+
367
+ before do
368
+ signing_options = Pandexio::SigningOptions.new()
369
+ http_client = Pandexio::HttpClient.new()
370
+ @get_document = ->{ http_client.get_document(123, 'm', signing_options) }
371
+ end
372
+
373
+ it "should raise ArgumentError" do
374
+ err = @get_document.must_raise ArgumentError
375
+ err.message.must_match "Document ID is not a string, is nil or empty, or does not match RegEx pattern: #{Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN}."
376
+ end
377
+
378
+ end
379
+
380
+ describe "when id does not match RegEx" do
381
+
382
+ before do
383
+ signing_options = Pandexio::SigningOptions.new()
384
+ http_client = Pandexio::HttpClient.new()
385
+ @get_document = ->{ http_client.get_document("123^%$*abc", 'm', signing_options) }
386
+ end
387
+
388
+ it "should raise ArgumentError" do
389
+ err = @get_document.must_raise ArgumentError
390
+ err.message.must_match "Document ID is not a string, is nil or empty, or does not match RegEx pattern: #{Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN}."
391
+ end
392
+
393
+ end
394
+
395
+ describe "when cover_size is invalid" do
396
+
397
+ before do
398
+ signing_options = Pandexio::SigningOptions.new()
399
+ http_client = Pandexio::HttpClient.new()
400
+ @get_document = ->{ http_client.get_document('test_id', 'abc', signing_options) }
401
+ end
402
+
403
+ it "should raise ArgumentError" do
404
+ err = @get_document.must_raise ArgumentError
405
+ err.message.must_match "Document cover size must be xs, s, m, l, or xl."
406
+ end
407
+
408
+ end
409
+
410
+ describe "when document does not exist" do
411
+
412
+ before do
413
+ skip("skip integration tests") if TestConfig::SKIP_INTEGRATION_TESTS
414
+ id = UUID.new.generate
415
+ signing_options = Pandexio::SigningOptions.new(
416
+ :algorithm => Pandexio::SigningAlgorithms::PDX_HMAC_SHA256,
417
+ :mechanism => Pandexio::SigningMechanisms::HEADER,
418
+ :domain_id => TestConfig::DOMAIN_ID,
419
+ :domain_key => TestConfig::DOMAIN_KEY,
420
+ :date => Time.now.utc,
421
+ :expires => 90,
422
+ :originator => "HttpClientTest",
423
+ :email_address => "HttpClientTest",
424
+ :display_name => "HttpClientTest")
425
+ http_client = Pandexio::HttpClient.new()
426
+ @get_document = ->{ http_client.get_document(id, 'm', signing_options) }
427
+ end
428
+
429
+ it "should return nil" do
430
+ doc = @get_document.call
431
+ doc.must_be_nil
432
+ end
433
+
434
+ end
435
+
436
+ describe "when document exists" do
437
+
438
+ before do
439
+ skip("skip integration tests") if TestConfig::SKIP_INTEGRATION_TESTS
440
+ @id = UUID.new.generate
441
+ document_source = Pandexio::DocumentSource.new(
442
+ :id => @id,
443
+ :name => "test_name_#{@id}.txt",
444
+ :content => "data:text/plain;base64,dGVzdGluZyAxMjM=",
445
+ :location => "Inline")
446
+ signing_options = Pandexio::SigningOptions.new(
447
+ :algorithm => Pandexio::SigningAlgorithms::PDX_HMAC_SHA256,
448
+ :mechanism => Pandexio::SigningMechanisms::HEADER,
449
+ :domain_id => TestConfig::DOMAIN_ID,
450
+ :domain_key => TestConfig::DOMAIN_KEY,
451
+ :date => Time.now.utc,
452
+ :expires => 90,
453
+ :originator => "HttpClientTest",
454
+ :email_address => "HttpClientTest",
455
+ :display_name => "HttpClientTest")
456
+ http_client = Pandexio::HttpClient.new()
457
+ http_client.load_document(document_source, signing_options)
458
+ sleep(3)
459
+ @get_document = ->{ http_client.get_document(@id, 'm', signing_options) }
460
+ end
461
+
462
+ it "should return document details" do
463
+ doc = @get_document.call
464
+ doc.document_id.must_equal @id
465
+ doc.name.must_equal "test_name_#{@id}.txt"
466
+ doc.page_count.must_equal 1
467
+ doc.content_type.must_equal "text/plain"
468
+ doc.content_length.must_equal 11
469
+ doc.cover[0..29].must_equal 'data:image/png;base64,iVBORw0K'
470
+ doc.cover.length.must_equal 2542
471
+ end
472
+
473
+ end
474
+
475
+ describe "when cover size is xs" do
476
+
477
+ before do
478
+ skip("skip integration tests") if TestConfig::SKIP_INTEGRATION_TESTS
479
+ @id = UUID.new.generate
480
+ document_source = Pandexio::DocumentSource.new(
481
+ :id => @id,
482
+ :name => "test_name_#{@id}.txt",
483
+ :content => "data:text/plain;base64,dGVzdGluZyAxMjM=",
484
+ :location => "Inline")
485
+ signing_options = Pandexio::SigningOptions.new(
486
+ :algorithm => Pandexio::SigningAlgorithms::PDX_HMAC_SHA256,
487
+ :mechanism => Pandexio::SigningMechanisms::HEADER,
488
+ :domain_id => TestConfig::DOMAIN_ID,
489
+ :domain_key => TestConfig::DOMAIN_KEY,
490
+ :date => Time.now.utc,
491
+ :expires => 90,
492
+ :originator => "HttpClientTest",
493
+ :email_address => "HttpClientTest",
494
+ :display_name => "HttpClientTest")
495
+ http_client = Pandexio::HttpClient.new()
496
+ http_client.load_document(document_source, signing_options)
497
+ sleep(3)
498
+ @get_document = ->{ http_client.get_document(@id, 'xs', signing_options) }
499
+ end
500
+
501
+ it "should return document details" do
502
+ doc = @get_document.call
503
+ doc.document_id.must_equal @id
504
+ doc.name.must_equal "test_name_#{@id}.txt"
505
+ doc.page_count.must_equal 1
506
+ doc.content_type.must_equal "text/plain"
507
+ doc.content_length.must_equal 11
508
+ doc.cover[0..29].must_equal 'data:image/png;base64,iVBORw0K'
509
+ doc.cover.length.must_equal 370
510
+ end
511
+
512
+ end
513
+
514
+ end
515
+
516
+ describe "#get_document_cover" do
517
+
518
+ describe "when id is nil" do
519
+
520
+ before do
521
+ signing_options = Pandexio::SigningOptions.new()
522
+ http_client = Pandexio::HttpClient.new()
523
+ @get_document_cover = ->{ http_client.get_document_cover(nil, 'm', signing_options) }
524
+ end
525
+
526
+ it "should raise ArgumentError" do
527
+ err = @get_document_cover.must_raise ArgumentError
528
+ err.message.must_match "Document ID is not a string, is nil or empty, or does not match RegEx pattern: #{Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN}."
529
+ end
530
+
531
+ end
532
+
533
+ describe "when id is empty" do
534
+
535
+ before do
536
+ signing_options = Pandexio::SigningOptions.new()
537
+ http_client = Pandexio::HttpClient.new()
538
+ @get_document_cover = ->{ http_client.get_document_cover("", 'm', signing_options) }
539
+ end
540
+
541
+ it "should raise ArgumentError" do
542
+ err = @get_document_cover.must_raise ArgumentError
543
+ err.message.must_match "Document ID is not a string, is nil or empty, or does not match RegEx pattern: #{Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN}."
544
+ end
545
+
546
+ end
547
+
548
+ describe "when id is blank" do
549
+
550
+ before do
551
+ signing_options = Pandexio::SigningOptions.new()
552
+ http_client = Pandexio::HttpClient.new()
553
+ @get_document_cover = ->{ http_client.get_document_cover(" ", 'm', signing_options) }
554
+ end
555
+
556
+ it "should raise ArgumentError" do
557
+ err = @get_document_cover.must_raise ArgumentError
558
+ err.message.must_match "Document ID is not a string, is nil or empty, or does not match RegEx pattern: #{Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN}."
559
+ end
560
+
561
+ end
562
+
563
+ describe "when id is not a string" do
564
+
565
+ before do
566
+ signing_options = Pandexio::SigningOptions.new()
567
+ http_client = Pandexio::HttpClient.new()
568
+ @get_document_cover = ->{ http_client.get_document_cover(123, 'm', signing_options) }
569
+ end
570
+
571
+ it "should raise ArgumentError" do
572
+ err = @get_document_cover.must_raise ArgumentError
573
+ err.message.must_match "Document ID is not a string, is nil or empty, or does not match RegEx pattern: #{Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN}."
574
+ end
575
+
576
+ end
577
+
578
+ describe "when id does not match RegEx" do
579
+
580
+ before do
581
+ signing_options = Pandexio::SigningOptions.new()
582
+ http_client = Pandexio::HttpClient.new()
583
+ @get_document_cover = ->{ http_client.get_document_cover("123^%$*abc", 'm', signing_options) }
584
+ end
585
+
586
+ it "should raise ArgumentError" do
587
+ err = @get_document_cover.must_raise ArgumentError
588
+ err.message.must_match "Document ID is not a string, is nil or empty, or does not match RegEx pattern: #{Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN}."
589
+ end
590
+
591
+ end
592
+
593
+ describe "when cover_size is invalid" do
594
+
595
+ before do
596
+ signing_options = Pandexio::SigningOptions.new()
597
+ http_client = Pandexio::HttpClient.new()
598
+ @get_document_cover = ->{ http_client.get_document_cover('test_id', 'abc', signing_options) }
599
+ end
600
+
601
+ it "should raise ArgumentError" do
602
+ err = @get_document_cover.must_raise ArgumentError
603
+ err.message.must_match "Document cover size must be xs, s, m, l, or xl."
604
+ end
605
+
606
+ end
607
+
608
+ describe "when document does not exist" do
609
+
610
+ before do
611
+ skip("skip integration tests") if TestConfig::SKIP_INTEGRATION_TESTS
612
+ id = UUID.new.generate
613
+ signing_options = Pandexio::SigningOptions.new(
614
+ :algorithm => Pandexio::SigningAlgorithms::PDX_HMAC_SHA256,
615
+ :mechanism => Pandexio::SigningMechanisms::HEADER,
616
+ :domain_id => TestConfig::DOMAIN_ID,
617
+ :domain_key => TestConfig::DOMAIN_KEY,
618
+ :date => Time.now.utc,
619
+ :expires => 90,
620
+ :originator => "HttpClientTest",
621
+ :email_address => "HttpClientTest",
622
+ :display_name => "HttpClientTest")
623
+ http_client = Pandexio::HttpClient.new()
624
+ @get_document_cover = ->{ http_client.get_document_cover(id, 'm', signing_options) }
625
+ end
626
+
627
+ it "should return nil" do
628
+ doc = @get_document_cover.call
629
+ doc.must_be_nil
630
+ end
631
+
632
+ end
633
+
634
+ describe "when cover size is xs" do
635
+
636
+ before do
637
+ skip("skip integration tests") if TestConfig::SKIP_INTEGRATION_TESTS
638
+ @id = UUID.new.generate
639
+ document_source = Pandexio::DocumentSource.new(
640
+ :id => @id,
641
+ :name => "test_name_#{@id}.txt",
642
+ :content => "data:text/plain;base64,dGVzdGluZyAxMjM=",
643
+ :location => "Inline")
644
+ signing_options = Pandexio::SigningOptions.new(
645
+ :algorithm => Pandexio::SigningAlgorithms::PDX_HMAC_SHA256,
646
+ :mechanism => Pandexio::SigningMechanisms::HEADER,
647
+ :domain_id => TestConfig::DOMAIN_ID,
648
+ :domain_key => TestConfig::DOMAIN_KEY,
649
+ :date => Time.now.utc,
650
+ :expires => 90,
651
+ :originator => "HttpClientTest",
652
+ :email_address => "HttpClientTest",
653
+ :display_name => "HttpClientTest")
654
+ http_client = Pandexio::HttpClient.new()
655
+ http_client.load_document(document_source, signing_options)
656
+ sleep(3)
657
+ @get_document_cover = ->{ http_client.get_document_cover(@id, 'xs', signing_options) }
658
+ end
659
+
660
+ it "should return document details" do
661
+ cover = @get_document_cover.call
662
+ cover.length.must_equal 259
663
+ end
664
+
665
+ end
666
+
667
+ describe "when cover size is s" do
668
+
669
+ before do
670
+ skip("skip integration tests") if TestConfig::SKIP_INTEGRATION_TESTS
671
+ @id = UUID.new.generate
672
+ document_source = Pandexio::DocumentSource.new(
673
+ :id => @id,
674
+ :name => "test_name_#{@id}.txt",
675
+ :content => "data:text/plain;base64,dGVzdGluZyAxMjM=",
676
+ :location => "Inline")
677
+ signing_options = Pandexio::SigningOptions.new(
678
+ :algorithm => Pandexio::SigningAlgorithms::PDX_HMAC_SHA256,
679
+ :mechanism => Pandexio::SigningMechanisms::HEADER,
680
+ :domain_id => TestConfig::DOMAIN_ID,
681
+ :domain_key => TestConfig::DOMAIN_KEY,
682
+ :date => Time.now.utc,
683
+ :expires => 90,
684
+ :originator => "HttpClientTest",
685
+ :email_address => "HttpClientTest",
686
+ :display_name => "HttpClientTest")
687
+ http_client = Pandexio::HttpClient.new()
688
+ http_client.load_document(document_source, signing_options)
689
+ sleep(3)
690
+ @get_document_cover = ->{ http_client.get_document_cover(@id, 's', signing_options) }
691
+ end
692
+
693
+ it "should return document details" do
694
+ cover = @get_document_cover.call
695
+ cover.length.must_equal 808
696
+ end
697
+
698
+ end
699
+
700
+ describe "when cover size is m" do
701
+
702
+ before do
703
+ skip("skip integration tests") if TestConfig::SKIP_INTEGRATION_TESTS
704
+ @id = UUID.new.generate
705
+ document_source = Pandexio::DocumentSource.new(
706
+ :id => @id,
707
+ :name => "test_name_#{@id}.txt",
708
+ :content => "data:text/plain;base64,dGVzdGluZyAxMjM=",
709
+ :location => "Inline")
710
+ signing_options = Pandexio::SigningOptions.new(
711
+ :algorithm => Pandexio::SigningAlgorithms::PDX_HMAC_SHA256,
712
+ :mechanism => Pandexio::SigningMechanisms::HEADER,
713
+ :domain_id => TestConfig::DOMAIN_ID,
714
+ :domain_key => TestConfig::DOMAIN_KEY,
715
+ :date => Time.now.utc,
716
+ :expires => 90,
717
+ :originator => "HttpClientTest",
718
+ :email_address => "HttpClientTest",
719
+ :display_name => "HttpClientTest")
720
+ http_client = Pandexio::HttpClient.new()
721
+ http_client.load_document(document_source, signing_options)
722
+ sleep(3)
723
+ @get_document_cover = ->{ http_client.get_document_cover(@id, 'm', signing_options) }
724
+ end
725
+
726
+ it "should return document details" do
727
+ cover = @get_document_cover.call
728
+ cover.length.must_equal 1888
729
+ end
730
+
731
+ end
732
+
733
+ describe "when cover size is l" do
734
+
735
+ before do
736
+ skip("skip integration tests") if TestConfig::SKIP_INTEGRATION_TESTS
737
+ @id = UUID.new.generate
738
+ document_source = Pandexio::DocumentSource.new(
739
+ :id => @id,
740
+ :name => "test_name_#{@id}.txt",
741
+ :content => "data:text/plain;base64,dGVzdGluZyAxMjM=",
742
+ :location => "Inline")
743
+ signing_options = Pandexio::SigningOptions.new(
744
+ :algorithm => Pandexio::SigningAlgorithms::PDX_HMAC_SHA256,
745
+ :mechanism => Pandexio::SigningMechanisms::HEADER,
746
+ :domain_id => TestConfig::DOMAIN_ID,
747
+ :domain_key => TestConfig::DOMAIN_KEY,
748
+ :date => Time.now.utc,
749
+ :expires => 90,
750
+ :originator => "HttpClientTest",
751
+ :email_address => "HttpClientTest",
752
+ :display_name => "HttpClientTest")
753
+ http_client = Pandexio::HttpClient.new()
754
+ http_client.load_document(document_source, signing_options)
755
+ sleep(3)
756
+ @get_document_cover = ->{ http_client.get_document_cover(@id, 'l', signing_options) }
757
+ end
758
+
759
+ it "should return document details" do
760
+ cover = @get_document_cover.call
761
+ cover.length.must_equal 3726
762
+ end
763
+
764
+ end
765
+
766
+ describe "when cover size is xl" do
767
+
768
+ before do
769
+ skip("skip integration tests") if TestConfig::SKIP_INTEGRATION_TESTS
770
+ @id = UUID.new.generate
771
+ document_source = Pandexio::DocumentSource.new(
772
+ :id => @id,
773
+ :name => "test_name_#{@id}.txt",
774
+ :content => "data:text/plain;base64,dGVzdGluZyAxMjM=",
775
+ :location => "Inline")
776
+ signing_options = Pandexio::SigningOptions.new(
777
+ :algorithm => Pandexio::SigningAlgorithms::PDX_HMAC_SHA256,
778
+ :mechanism => Pandexio::SigningMechanisms::HEADER,
779
+ :domain_id => TestConfig::DOMAIN_ID,
780
+ :domain_key => TestConfig::DOMAIN_KEY,
781
+ :date => Time.now.utc,
782
+ :expires => 90,
783
+ :originator => "HttpClientTest",
784
+ :email_address => "HttpClientTest",
785
+ :display_name => "HttpClientTest")
786
+ http_client = Pandexio::HttpClient.new()
787
+ http_client.load_document(document_source, signing_options)
788
+ sleep(3)
789
+ @get_document_cover = ->{ http_client.get_document_cover(@id, 'xl', signing_options) }
790
+ end
791
+
792
+ it "should return document details" do
793
+ cover = @get_document_cover.call
794
+ cover.length.must_equal 7089
795
+ end
796
+
797
+ end
798
+
799
+ end
800
+
801
+ describe "#get_document_cover_url" do
802
+
803
+ describe "when id is nil" do
804
+
805
+ before do
806
+ signing_options = Pandexio::SigningOptions.new()
807
+ http_client = Pandexio::HttpClient.new()
808
+ @get_document_cover_url = ->{ http_client.get_document_cover_url(nil, 'm', signing_options) }
809
+ end
810
+
811
+ it "should raise ArgumentError" do
812
+ err = @get_document_cover_url.must_raise ArgumentError
813
+ err.message.must_match "Document ID is not a string, is nil or empty, or does not match RegEx pattern: #{Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN}."
814
+ end
815
+
816
+ end
817
+
818
+ describe "when id is empty" do
819
+
820
+ before do
821
+ signing_options = Pandexio::SigningOptions.new()
822
+ http_client = Pandexio::HttpClient.new()
823
+ @get_document_cover_url = ->{ http_client.get_document_cover_url("", 'm', signing_options) }
824
+ end
825
+
826
+ it "should raise ArgumentError" do
827
+ err = @get_document_cover_url.must_raise ArgumentError
828
+ err.message.must_match "Document ID is not a string, is nil or empty, or does not match RegEx pattern: #{Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN}."
829
+ end
830
+
831
+ end
832
+
833
+ describe "when id is blank" do
834
+
835
+ before do
836
+ signing_options = Pandexio::SigningOptions.new()
837
+ http_client = Pandexio::HttpClient.new()
838
+ @get_document_cover_url = ->{ http_client.get_document_cover_url(" ", 'm', signing_options) }
839
+ end
840
+
841
+ it "should raise ArgumentError" do
842
+ err = @get_document_cover_url.must_raise ArgumentError
843
+ err.message.must_match "Document ID is not a string, is nil or empty, or does not match RegEx pattern: #{Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN}."
844
+ end
845
+
846
+ end
847
+
848
+ describe "when id is not a string" do
849
+
850
+ before do
851
+ signing_options = Pandexio::SigningOptions.new()
852
+ http_client = Pandexio::HttpClient.new()
853
+ @get_document_cover_url = ->{ http_client.get_document_cover_url(123, 'm', signing_options) }
854
+ end
855
+
856
+ it "should raise ArgumentError" do
857
+ err = @get_document_cover_url.must_raise ArgumentError
858
+ err.message.must_match "Document ID is not a string, is nil or empty, or does not match RegEx pattern: #{Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN}."
859
+ end
860
+
861
+ end
862
+
863
+ describe "when id does not match RegEx" do
864
+
865
+ before do
866
+ signing_options = Pandexio::SigningOptions.new()
867
+ http_client = Pandexio::HttpClient.new()
868
+ @get_document_cover_url = ->{ http_client.get_document_cover_url("123^%$*abc", 'm', signing_options) }
869
+ end
870
+
871
+ it "should raise ArgumentError" do
872
+ err = @get_document_cover_url.must_raise ArgumentError
873
+ err.message.must_match "Document ID is not a string, is nil or empty, or does not match RegEx pattern: #{Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN}."
874
+ end
875
+
876
+ end
877
+
878
+ describe "when cover_size is invalid" do
879
+
880
+ before do
881
+ signing_options = Pandexio::SigningOptions.new()
882
+ http_client = Pandexio::HttpClient.new()
883
+ @get_document_cover_url = ->{ http_client.get_document_cover_url('test_id', 'abc', signing_options) }
884
+ end
885
+
886
+ it "should raise ArgumentError" do
887
+ err = @get_document_cover_url.must_raise ArgumentError
888
+ err.message.must_match "Document cover size must be xs, s, m, l, or xl."
889
+ end
890
+
891
+ end
892
+
893
+ describe "when signing_mechanism is Header" do
894
+
895
+ before do
896
+ id = UUID.new.generate
897
+ signing_options = Pandexio::SigningOptions.new(
898
+ :algorithm => Pandexio::SigningAlgorithms::PDX_HMAC_SHA256,
899
+ :mechanism => Pandexio::SigningMechanisms::HEADER,
900
+ :domain_id => TestConfig::DOMAIN_ID,
901
+ :domain_key => TestConfig::DOMAIN_KEY,
902
+ :date => Time.now.utc,
903
+ :expires => 90,
904
+ :originator => "HttpClientTest",
905
+ :email_address => "HttpClientTest",
906
+ :display_name => "HttpClientTest")
907
+ http_client = Pandexio::HttpClient.new()
908
+ @get_document_cover_url = ->{ http_client.get_document_cover_url(id, 'm', signing_options) }
909
+ end
910
+
911
+ it "should return nil" do
912
+ err = @get_document_cover_url.must_raise ArgumentError
913
+ err.message.must_match "Signing mechanism must be QueryString."
914
+ end
915
+
916
+ end
917
+
918
+ describe "when cover size is xs" do
919
+
920
+ before do
921
+ @id = UUID.new.generate
922
+ document_source = Pandexio::DocumentSource.new(
923
+ :id => @id,
924
+ :name => "test_name_#{@id}.txt",
925
+ :content => "data:text/plain;base64,dGVzdGluZyAxMjM=",
926
+ :location => "Inline")
927
+ signing_options = Pandexio::SigningOptions.new(
928
+ :algorithm => Pandexio::SigningAlgorithms::PDX_HMAC_SHA256,
929
+ :mechanism => Pandexio::SigningMechanisms::QUERY_STRING,
930
+ :domain_id => TestConfig::DOMAIN_ID,
931
+ :domain_key => TestConfig::DOMAIN_KEY,
932
+ :date => Time.now.utc,
933
+ :expires => 90,
934
+ :originator => "HttpClientTest",
935
+ :email_address => "HttpClientTest",
936
+ :display_name => "HttpClientTest")
937
+ http_client = Pandexio::HttpClient.new()
938
+ @document_cover_url = http_client.get_document_cover_url(@id, 'xs', signing_options)
939
+ end
940
+
941
+ it "should return document details" do
942
+ (@document_cover_url.include? "/v2/documents/#{@id}/cover").must_equal true
943
+ (@document_cover_url.include? "coverSize=xs").must_equal true
944
+ (@document_cover_url.include? "X-Pdx-Originator=HttpClientTest").must_equal true
945
+ (@document_cover_url.include? "X-Pdx-EmailAddress=HttpClientTest").must_equal true
946
+ (@document_cover_url.include? "X-Pdx-DisplayName=HttpClientTest").must_equal true
947
+ (@document_cover_url.include? "X-Pdx-Expires=90").must_equal true
948
+ (@document_cover_url.include? "X-Pdx-Credential=#{TestConfig::DOMAIN_ID}").must_equal true
949
+ (@document_cover_url.include? "X-Pdx-Signature=").must_equal true
950
+ end
951
+
952
+ end
953
+
954
+ describe "when cover size is s" do
955
+
956
+ before do
957
+ @id = UUID.new.generate
958
+ document_source = Pandexio::DocumentSource.new(
959
+ :id => @id,
960
+ :name => "test_name_#{@id}.txt",
961
+ :content => "data:text/plain;base64,dGVzdGluZyAxMjM=",
962
+ :location => "Inline")
963
+ signing_options = Pandexio::SigningOptions.new(
964
+ :algorithm => Pandexio::SigningAlgorithms::PDX_HMAC_SHA256,
965
+ :mechanism => Pandexio::SigningMechanisms::QUERY_STRING,
966
+ :domain_id => TestConfig::DOMAIN_ID,
967
+ :domain_key => TestConfig::DOMAIN_KEY,
968
+ :date => Time.now.utc,
969
+ :expires => 90,
970
+ :originator => "HttpClientTest",
971
+ :email_address => "HttpClientTest",
972
+ :display_name => "HttpClientTest")
973
+ http_client = Pandexio::HttpClient.new()
974
+ @document_cover_url = http_client.get_document_cover_url(@id, 's', signing_options)
975
+ end
976
+
977
+ it "should return document details" do
978
+ (@document_cover_url.include? "/v2/documents/#{@id}/cover").must_equal true
979
+ (@document_cover_url.include? "coverSize=s").must_equal true
980
+ (@document_cover_url.include? "X-Pdx-Originator=HttpClientTest").must_equal true
981
+ (@document_cover_url.include? "X-Pdx-EmailAddress=HttpClientTest").must_equal true
982
+ (@document_cover_url.include? "X-Pdx-DisplayName=HttpClientTest").must_equal true
983
+ (@document_cover_url.include? "X-Pdx-Expires=90").must_equal true
984
+ (@document_cover_url.include? "X-Pdx-Credential=#{TestConfig::DOMAIN_ID}").must_equal true
985
+ (@document_cover_url.include? "X-Pdx-Signature=").must_equal true
986
+ end
987
+
988
+ end
989
+
990
+ describe "when cover size is m" do
991
+
992
+ before do
993
+ @id = UUID.new.generate
994
+ document_source = Pandexio::DocumentSource.new(
995
+ :id => @id,
996
+ :name => "test_name_#{@id}.txt",
997
+ :content => "data:text/plain;base64,dGVzdGluZyAxMjM=",
998
+ :location => "Inline")
999
+ signing_options = Pandexio::SigningOptions.new(
1000
+ :algorithm => Pandexio::SigningAlgorithms::PDX_HMAC_SHA256,
1001
+ :mechanism => Pandexio::SigningMechanisms::QUERY_STRING,
1002
+ :domain_id => TestConfig::DOMAIN_ID,
1003
+ :domain_key => TestConfig::DOMAIN_KEY,
1004
+ :date => Time.now.utc,
1005
+ :expires => 90,
1006
+ :originator => "HttpClientTest",
1007
+ :email_address => "HttpClientTest",
1008
+ :display_name => "HttpClientTest")
1009
+ http_client = Pandexio::HttpClient.new()
1010
+ @document_cover_url = http_client.get_document_cover_url(@id, 'm', signing_options)
1011
+ end
1012
+
1013
+ it "should return document details" do
1014
+ (@document_cover_url.include? "/v2/documents/#{@id}/cover").must_equal true
1015
+ (@document_cover_url.include? "coverSize=m").must_equal true
1016
+ (@document_cover_url.include? "X-Pdx-Originator=HttpClientTest").must_equal true
1017
+ (@document_cover_url.include? "X-Pdx-EmailAddress=HttpClientTest").must_equal true
1018
+ (@document_cover_url.include? "X-Pdx-DisplayName=HttpClientTest").must_equal true
1019
+ (@document_cover_url.include? "X-Pdx-Expires=90").must_equal true
1020
+ (@document_cover_url.include? "X-Pdx-Credential=#{TestConfig::DOMAIN_ID}").must_equal true
1021
+ (@document_cover_url.include? "X-Pdx-Signature=").must_equal true
1022
+ end
1023
+
1024
+ end
1025
+
1026
+ describe "when cover size is l" do
1027
+
1028
+ before do
1029
+ @id = UUID.new.generate
1030
+ document_source = Pandexio::DocumentSource.new(
1031
+ :id => @id,
1032
+ :name => "test_name_#{@id}.txt",
1033
+ :content => "data:text/plain;base64,dGVzdGluZyAxMjM=",
1034
+ :location => "Inline")
1035
+ signing_options = Pandexio::SigningOptions.new(
1036
+ :algorithm => Pandexio::SigningAlgorithms::PDX_HMAC_SHA256,
1037
+ :mechanism => Pandexio::SigningMechanisms::QUERY_STRING,
1038
+ :domain_id => TestConfig::DOMAIN_ID,
1039
+ :domain_key => TestConfig::DOMAIN_KEY,
1040
+ :date => Time.now.utc,
1041
+ :expires => 90,
1042
+ :originator => "HttpClientTest",
1043
+ :email_address => "HttpClientTest",
1044
+ :display_name => "HttpClientTest")
1045
+ http_client = Pandexio::HttpClient.new()
1046
+ @document_cover_url = http_client.get_document_cover_url(@id, 'l', signing_options)
1047
+ end
1048
+
1049
+ it "should return document details" do
1050
+ (@document_cover_url.include? "/v2/documents/#{@id}/cover").must_equal true
1051
+ (@document_cover_url.include? "coverSize=l").must_equal true
1052
+ (@document_cover_url.include? "X-Pdx-Originator=HttpClientTest").must_equal true
1053
+ (@document_cover_url.include? "X-Pdx-EmailAddress=HttpClientTest").must_equal true
1054
+ (@document_cover_url.include? "X-Pdx-DisplayName=HttpClientTest").must_equal true
1055
+ (@document_cover_url.include? "X-Pdx-Expires=90").must_equal true
1056
+ (@document_cover_url.include? "X-Pdx-Credential=#{TestConfig::DOMAIN_ID}").must_equal true
1057
+ (@document_cover_url.include? "X-Pdx-Signature=").must_equal true
1058
+ end
1059
+
1060
+ end
1061
+
1062
+ describe "when cover size is xl" do
1063
+
1064
+ before do
1065
+ @id = UUID.new.generate
1066
+ document_source = Pandexio::DocumentSource.new(
1067
+ :id => @id,
1068
+ :name => "test_name_#{@id}.txt",
1069
+ :content => "data:text/plain;base64,dGVzdGluZyAxMjM=",
1070
+ :location => "Inline")
1071
+ signing_options = Pandexio::SigningOptions.new(
1072
+ :algorithm => Pandexio::SigningAlgorithms::PDX_HMAC_SHA256,
1073
+ :mechanism => Pandexio::SigningMechanisms::QUERY_STRING,
1074
+ :domain_id => TestConfig::DOMAIN_ID,
1075
+ :domain_key => TestConfig::DOMAIN_KEY,
1076
+ :date => Time.now.utc,
1077
+ :expires => 90,
1078
+ :originator => "HttpClientTest",
1079
+ :email_address => "HttpClientTest",
1080
+ :display_name => "HttpClientTest")
1081
+ http_client = Pandexio::HttpClient.new()
1082
+ @document_cover_url = http_client.get_document_cover_url(@id, 'xl', signing_options).to_s
1083
+ end
1084
+
1085
+ it "should return document details" do
1086
+ (@document_cover_url.include? "/v2/documents/#{@id}/cover").must_equal true
1087
+ (@document_cover_url.include? "coverSize=xl").must_equal true
1088
+ (@document_cover_url.include? "X-Pdx-Originator=HttpClientTest").must_equal true
1089
+ (@document_cover_url.include? "X-Pdx-EmailAddress=HttpClientTest").must_equal true
1090
+ (@document_cover_url.include? "X-Pdx-DisplayName=HttpClientTest").must_equal true
1091
+ (@document_cover_url.include? "X-Pdx-Expires=90").must_equal true
1092
+ (@document_cover_url.include? "X-Pdx-Credential=#{TestConfig::DOMAIN_ID}").must_equal true
1093
+ (@document_cover_url.include? "X-Pdx-Signature=").must_equal true
1094
+ end
1095
+
1096
+ end
1097
+
1098
+ end
1099
+
1100
+ describe "#get_document_snip_count" do
1101
+
1102
+ describe "when id is nil" do
1103
+
1104
+ before do
1105
+ signing_options = Pandexio::SigningOptions.new()
1106
+ http_client = Pandexio::HttpClient.new()
1107
+ @get_document_snip_count = ->{ http_client.get_document_snip_count(nil, signing_options) }
1108
+ end
1109
+
1110
+ it "should raise ArgumentError" do
1111
+ err = @get_document_snip_count.must_raise ArgumentError
1112
+ err.message.must_match "Document ID is not a string, is nil or empty, or does not match RegEx pattern: #{Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN}."
1113
+ end
1114
+
1115
+ end
1116
+
1117
+ describe "when id is empty" do
1118
+
1119
+ before do
1120
+ signing_options = Pandexio::SigningOptions.new()
1121
+ http_client = Pandexio::HttpClient.new()
1122
+ @get_document_snip_count = ->{ http_client.get_document_snip_count("", signing_options) }
1123
+ end
1124
+
1125
+ it "should raise ArgumentError" do
1126
+ err = @get_document_snip_count.must_raise ArgumentError
1127
+ err.message.must_match "Document ID is not a string, is nil or empty, or does not match RegEx pattern: #{Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN}."
1128
+ end
1129
+
1130
+ end
1131
+
1132
+ describe "when id is blank" do
1133
+
1134
+ before do
1135
+ signing_options = Pandexio::SigningOptions.new()
1136
+ http_client = Pandexio::HttpClient.new()
1137
+ @get_document_snip_count = ->{ http_client.get_document_snip_count(" ", signing_options) }
1138
+ end
1139
+
1140
+ it "should raise ArgumentError" do
1141
+ err = @get_document_snip_count.must_raise ArgumentError
1142
+ err.message.must_match "Document ID is not a string, is nil or empty, or does not match RegEx pattern: #{Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN}."
1143
+ end
1144
+
1145
+ end
1146
+
1147
+ describe "when id is not a string" do
1148
+
1149
+ before do
1150
+ signing_options = Pandexio::SigningOptions.new()
1151
+ http_client = Pandexio::HttpClient.new()
1152
+ @get_document_snip_count = ->{ http_client.get_document_snip_count(123, signing_options) }
1153
+ end
1154
+
1155
+ it "should raise ArgumentError" do
1156
+ err = @get_document_snip_count.must_raise ArgumentError
1157
+ err.message.must_match "Document ID is not a string, is nil or empty, or does not match RegEx pattern: #{Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN}."
1158
+ end
1159
+
1160
+ end
1161
+
1162
+ describe "when id does not match RegEx" do
1163
+
1164
+ before do
1165
+ signing_options = Pandexio::SigningOptions.new()
1166
+ http_client = Pandexio::HttpClient.new()
1167
+ @get_document_snip_count = ->{ http_client.get_document_snip_count("123^%$*abc", signing_options) }
1168
+ end
1169
+
1170
+ it "should raise ArgumentError" do
1171
+ err = @get_document_snip_count.must_raise ArgumentError
1172
+ err.message.must_match "Document ID is not a string, is nil or empty, or does not match RegEx pattern: #{Pandexio::ScopePatterns::DOCUMENT_ID_PATTERN}."
1173
+ end
1174
+
1175
+ end
1176
+
1177
+ describe "when document does not exist" do
1178
+
1179
+ before do
1180
+ skip("skip integration tests") if TestConfig::SKIP_INTEGRATION_TESTS
1181
+ id = UUID.new.generate
1182
+ signing_options = Pandexio::SigningOptions.new(
1183
+ :algorithm => Pandexio::SigningAlgorithms::PDX_HMAC_SHA256,
1184
+ :mechanism => Pandexio::SigningMechanisms::HEADER,
1185
+ :domain_id => TestConfig::DOMAIN_ID,
1186
+ :domain_key => TestConfig::DOMAIN_KEY,
1187
+ :date => Time.now.utc,
1188
+ :expires => 90,
1189
+ :originator => "HttpClientTest",
1190
+ :email_address => "HttpClientTest",
1191
+ :display_name => "HttpClientTest")
1192
+ http_client = Pandexio::HttpClient.new()
1193
+ @get_document_snip_count = ->{ http_client.get_document_snip_count(id, signing_options) }
1194
+ end
1195
+
1196
+ it "should return ." do
1197
+ count = @get_document_snip_count.call
1198
+ count.must_equal 0
1199
+ end
1200
+
1201
+ end
1202
+
1203
+ describe "when document exists" do
1204
+
1205
+ before do
1206
+ skip("skip integration tests") if TestConfig::SKIP_INTEGRATION_TESTS
1207
+ @id = UUID.new.generate
1208
+ document_source = Pandexio::DocumentSource.new(
1209
+ :id => @id,
1210
+ :name => "test_name_#{@id}.txt",
1211
+ :content => "data:text/plain;base64,dGVzdGluZyAxMjM=",
1212
+ :location => "Inline")
1213
+ signing_options = Pandexio::SigningOptions.new(
1214
+ :algorithm => Pandexio::SigningAlgorithms::PDX_HMAC_SHA256,
1215
+ :mechanism => Pandexio::SigningMechanisms::HEADER,
1216
+ :domain_id => TestConfig::DOMAIN_ID,
1217
+ :domain_key => TestConfig::DOMAIN_KEY,
1218
+ :date => Time.now.utc,
1219
+ :expires => 90,
1220
+ :originator => "HttpClientTest",
1221
+ :email_address => "HttpClientTest",
1222
+ :display_name => "HttpClientTest")
1223
+ http_client = Pandexio::HttpClient.new()
1224
+ http_client.load_document(document_source, signing_options)
1225
+ sleep(3)
1226
+ @get_document_snip_count = ->{ http_client.get_document_snip_count(@id, signing_options) }
1227
+ end
1228
+
1229
+ it "should return document details" do
1230
+ count = @get_document_snip_count.call
1231
+ count.must_equal 0
1232
+ end
1233
+
1234
+ end
1235
+
1236
+ end
1237
+
1238
+ end