adobe_pdfservices_ruby 0.1.1

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.
@@ -0,0 +1,109 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PdfServices
4
+ module Ocr
5
+ class External < Operation
6
+ EXTERNAL_OPTIONS = %i[input output params download_from_external].freeze
7
+ INPUT_KEYS = %i[uri storage].freeze
8
+ OUTPUT_KEYS = %i[uri storage].freeze
9
+ PARAMS_KEYS = %i[ocr_lang ocr_type].freeze
10
+ STORAGE_OPTIONS = %i[S3 SHAREPOINT DROPBOX BLOB].freeze
11
+
12
+ def initialize(api)
13
+ super
14
+ @download_from_external = false
15
+ @download_uri = nil
16
+ end
17
+
18
+ def execute(url, options = {})
19
+ @download_from_external = options[:download_from_external] || false
20
+ options[:input][:uri] = options[:input][:uri] || url
21
+ validate_options(options)
22
+
23
+ @download_uri = options[:output][:uri] if @download_from_external
24
+
25
+ response = @api.post(OPERATION_ENDPOINT,
26
+ body: request_body(asset.id, options),
27
+ headers: request_headers)
28
+ handle_response(response, asset.id)
29
+ end
30
+
31
+ private
32
+
33
+ def request_body(asset_id, options)
34
+ {
35
+ assetID: asset_id,
36
+ input: camelize_keys(options[:input]),
37
+ output: camelize_keys(options[:output]),
38
+ params: camelize_keys(options[:params])
39
+ }
40
+ end
41
+
42
+ def handle_polling_done(_json_response, original_asset)
43
+ file = @api.get(@download_uri).body if @download_from_external
44
+ super
45
+ file || true
46
+ end
47
+
48
+ def validate_options(options)
49
+ raise ArgumentError, 'Options must be a hash' unless options.is_a?(Hash)
50
+
51
+ ocr_lang = options[:params][:ocr_lang]
52
+ ocr_type = options[:params][:ocr_type]
53
+
54
+ validate_required_keys(options)
55
+ validate_input_options(options[:input])
56
+ validate_output_options(options[:output])
57
+ validate_ocr_lang_option(ocr_lang) if ocr_lang
58
+ validate_ocr_type_option(ocr_type) if ocr_type
59
+ end
60
+
61
+ def validate_required_keys(options)
62
+ required_keys = EXTERNAL_OPTIONS - %i[download_from_external]
63
+ required_keys.each do |key|
64
+ raise ArgumentError, "Missing required key: #{key}" unless options.key?(key)
65
+ end
66
+ end
67
+
68
+ def validate_input_options(input_options)
69
+ raise ArgumentError, 'Input options must be a hash' unless input_options.is_a?(Hash)
70
+
71
+ required_input_keys = INPUT_KEYS
72
+ required_input_keys.each do |key|
73
+ raise ArgumentError, "Missing required input key: #{key}" unless input_options.key?(key)
74
+ end
75
+ validate_storage_options(input_options[:storage])
76
+ end
77
+
78
+ def validate_output_options(output_options)
79
+ raise ArgumentError, 'Output options must be a hash' unless output_options.is_a?(Hash)
80
+
81
+ unless output_options.key?(:uri) && @download_from_external
82
+ raise ArgumentError,
83
+ 'Output options must contain uri when downloading from external storage'
84
+ end
85
+
86
+ required_output_keys = OUTPUT_KEYS - %i[uri]
87
+ required_output_keys.each do |key|
88
+ raise ArgumentError, "Missing required output key: #{key}" unless output_options.key?(key)
89
+ end
90
+ validate_storage_options(output_options[:storage])
91
+ end
92
+
93
+ def validate_storage_option(storage_option)
94
+ return if STORAGE_OPTIONS.include?(storage_option)
95
+
96
+ raise ArgumentError,
97
+ "Invalid storage option: #{storage_option}"
98
+ end
99
+
100
+ def validate_params_options(params_options)
101
+ raise ArgumentError, 'Params options must be a hash' unless params_options.is_a?(Hash)
102
+
103
+ params_options.each_key do |key|
104
+ raise ArgumentError, "Invalid params option: #{key}" unless PARAMS_KEYS.include?(key)
105
+ end
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PdfServices
4
+ module Ocr
5
+ class Internal < Operation
6
+ INTERNAL_OPTIONS = %i[ocr_lang ocr_type notifiers].freeze
7
+
8
+ def execute(html_file_path, options = {})
9
+ validate_options(options)
10
+ asset = upload_asset(html_file_path)
11
+
12
+ response = @api.post(OPERATION_ENDPOINT,
13
+ body: request_body(asset.id, options),
14
+ headers: request_headers)
15
+
16
+ handle_response(response, asset.id)
17
+ end
18
+
19
+ private
20
+
21
+ def handle_polling_done(json_response, _original_asset_id)
22
+ asset_id = json_response['asset']['assetID']
23
+ Asset.new(@api).download(asset_id).body
24
+ end
25
+
26
+ def request_body(asset_id, options)
27
+ body = {
28
+ assetID: asset_id
29
+ }
30
+ body[:ocrLang] = options[:ocr_lang] if options[:ocr_lang]
31
+ body[:ocrType] = options[:ocr_type] if options[:ocr_type]
32
+ body[:notifiers] = options[:notifiers] if options[:notifiers]
33
+ body
34
+ end
35
+
36
+ def validate_options(options)
37
+ raise ArgumentError, 'Invalid options' unless options.is_a?(Hash)
38
+
39
+ options.each_key do |key|
40
+ raise ArgumentError, "Invalid option: #{key}" unless INTERNAL_OPTIONS.include?(key)
41
+ end
42
+
43
+ validate_ocr_lang_option(options[:ocr_lang]) if options[:ocr_lang]
44
+ validate_ocr_type_option(options[:ocr_type]) if options[:ocr_type]
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PdfServices
4
+ module Ocr
5
+ class Operation < InternalExternalOperation::Operation
6
+ OCR_ENDPOINT = 'https://pdf-services-ue1.adobe.io/operation/ocr'
7
+ OCR_LANGS = %w[
8
+ da-DK lt-LT sl-SI el-GR ru-RU en-US zh-HK hu-HU et-EE
9
+ pt-BR uk-UA nb-NO pl-PL lv-LV fi-FI ja-JP es-ES bg-BG
10
+ en-GB cs-CZ mt-MT de-DE hr-HR sk-SK sr-SR ca-CA mk-MK
11
+ ko-KR de-CH nl-NL zh-CN sv-SE it-IT no-NO tr-TR fr-FR
12
+ ro-RO iw-IL
13
+ ].freeze
14
+
15
+ OCR_TYPES = %w[searchable_image searchable_image_exact].freeze
16
+
17
+ private
18
+
19
+ def validate_ocr_lang_option(ocr_lang)
20
+ raise ArgumentError, "Invalid ocr_lang option: #{ocr_lang}" unless OCR_LANGS.include?(ocr_lang)
21
+ end
22
+
23
+ def validate_ocr_type_option(ocr_type)
24
+ raise ArgumentError, "Invalid ocr_type option: #{ocr_type}" unless OCR_TYPES.include?(ocr_type)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PdfServices
4
+ VERSION = '0.1.1'
5
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: adobe_pdfservices_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Jimmy Bosse
8
+ - Ben Terova
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2024-01-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: faraday
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '2.9'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '2.9'
28
+ - !ruby/object:Gem::Dependency
29
+ name: json
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '2.6'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '2.6'
42
+ - !ruby/object:Gem::Dependency
43
+ name: mimemagic
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: 0.4.3
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: 0.4.3
56
+ description: An Adobe PDF Services Ruby SDK provides APIs for creating, combining,
57
+ exporting and manipulating PDFs.
58
+ email:
59
+ - jimmy.bosse@ankura.com
60
+ - ben@benterova.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - ".rubocop.yml"
66
+ - ".vscode/extensions.json"
67
+ - ".vscode/settings.json"
68
+ - CHANGELOG.md
69
+ - CODE_OF_CONDUCT.md
70
+ - Gemfile
71
+ - Gemfile.lock
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - adobe_pdfservices_ruby.gemspec
76
+ - lib/adobe_pdfservices_ruby.rb
77
+ - lib/pdfservices/api.rb
78
+ - lib/pdfservices/asset.rb
79
+ - lib/pdfservices/client.rb
80
+ - lib/pdfservices/errors.rb
81
+ - lib/pdfservices/operations/base.rb
82
+ - lib/pdfservices/operations/document_generation.rb
83
+ - lib/pdfservices/operations/document_generation/external.rb
84
+ - lib/pdfservices/operations/document_generation/internal.rb
85
+ - lib/pdfservices/operations/extract_pdf.rb
86
+ - lib/pdfservices/operations/html_to_pdf.rb
87
+ - lib/pdfservices/operations/html_to_pdf/external.rb
88
+ - lib/pdfservices/operations/html_to_pdf/internal.rb
89
+ - lib/pdfservices/operations/internal_external_operation.rb
90
+ - lib/pdfservices/operations/ocr.rb
91
+ - lib/pdfservices/operations/ocr/external.rb
92
+ - lib/pdfservices/operations/ocr/internal.rb
93
+ - lib/pdfservices/version.rb
94
+ homepage: https://github.com/benterova/adobe_pdfservices_ruby/blob/main/README.md
95
+ licenses:
96
+ - MIT
97
+ metadata:
98
+ homepage_uri: https://github.com/benterova/adobe_pdfservices_ruby/blob/main/README.md
99
+ source_code_uri: https://github.com/benterova/adobe_pdfservices_ruby
100
+ changelog_uri: https://github.com/benterova/adobe_pdfservices_ruby/blob/main/CHANGELOG.md
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: 3.0.0
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubygems_version: 3.4.10
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: Adobe PDF Services Ruby
120
+ test_files: []