Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Test

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Set up Ruby 3.4
uses: ruby/setup-ruby@v1
with:
ruby-version: 3.4
bundler-cache: true

- name: Run tests
run: bundle exec rake spec
env:
NUTRIENT_API_KEY: ${{ secrets.NUTRIENT_API_KEY }}

- name: Run RuboCop
run: bundle exec rake rubocop
32 changes: 32 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
AllCops:
TargetRubyVersion: 3.0
SuggestExtensions: false

# Disable complexity and length metrics for this API client
Metrics/ClassLength:
Enabled: false

Metrics/MethodLength:
Enabled: false

Metrics/AbcSize:
Enabled: false

Metrics/CyclomaticComplexity:
Enabled: false

Metrics/PerceivedComplexity:
Enabled: false

Metrics/BlockLength:
Enabled: false

# Allow longer lines for descriptive text
Layout/LineLength:
Max: 120
Exclude:
- '*.gemspec'

# Documentation is optional for this internal gem
Style/Documentation:
Enabled: false
12 changes: 6 additions & 6 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# frozen_string_literal: true

source "https://rubygems.org"
source 'https://rubygems.org'

# Specify your gem's dependencies in nutrient-dws.gemspec
gemspec

gem "rake", "~> 13.0"
gem "rspec", "~> 3.0"
gem "rubocop", "~> 1.21"
gem "dotenv", "~> 2.8"
gem "webmock", "~> 3.18"
gem 'dotenv', '~> 2.8'
gem 'rake', '~> 13.0'
gem 'rspec', '~> 3.0'
gem 'rubocop', '~> 1.21'
gem 'webmock', '~> 3.18'
8 changes: 4 additions & 4 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# frozen_string_literal: true

require "bundler/gem_tasks"
require "rspec/core/rake_task"
require 'bundler/gem_tasks'
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec)

require "rubocop/rake_task"
require 'rubocop/rake_task'

RuboCop::RakeTask.new

task default: %i[spec rubocop]
task default: %i[spec rubocop]
8 changes: 4 additions & 4 deletions lib/nutrient_dws.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# frozen_string_literal: true

require_relative "nutrient_dws/version"
require_relative "nutrient_dws/error"
require_relative "nutrient_dws/processor"
require_relative 'nutrient_dws/version'
require_relative 'nutrient_dws/error'
require_relative 'nutrient_dws/processor'

module NutrientDWS
class Error < StandardError; end
end
end
2 changes: 1 addition & 1 deletion lib/nutrient_dws/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ def initialize(message, status_code: nil, response_body: nil)
@response_body = response_body
end
end
end
end
4 changes: 2 additions & 2 deletions lib/nutrient_dws/processor.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# frozen_string_literal: true

require_relative "processor/client"
require_relative 'processor/client'

module NutrientDWS
module Processor
# Main entry point for the Nutrient DWS Processor API client
end
end
end
26 changes: 14 additions & 12 deletions lib/nutrient_dws/processor/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Client

def initialize(api_key:)
raise ArgumentError, 'API key is required' if api_key.nil? || api_key.empty?

@api_key = api_key
@boundary = SecureRandom.hex(16)
end
Expand Down Expand Up @@ -47,7 +48,8 @@ def convert(file:, to:)
type: 'pptx'
}
else
raise ArgumentError, "Unsupported output format: #{to}. Supported formats: pdf, png, jpg, jpeg, webp, tiff, docx, xlsx, pptx"
raise ArgumentError,
"Unsupported output format: #{to}. Supported formats: pdf, png, jpg, jpeg, webp, tiff, docx, xlsx, pptx"
end

make_request(instructions, files: { 'document' => file })
Expand Down Expand Up @@ -76,7 +78,7 @@ def watermark(file:, text: nil, image: nil, **options)
files = { 'document' => file }

# Build watermark action with required width/height
watermark_action = {
watermark_action = {
type: 'watermark',
width: options[:width] || 200,
height: options[:height] || 50
Expand Down Expand Up @@ -131,7 +133,7 @@ def merge(files:)
def split(file:, ranges:)
# For splitting, we specify page ranges in the part itself
part = build_part(file, 'document')

# Add pageIndexes to specify which pages to include
page_indexes = parse_page_ranges(ranges)
if !url?(file)
Expand All @@ -140,7 +142,7 @@ def split(file:, ranges:)
# For URLs, modify the part structure
part[:file][:pageIndexes] = page_indexes
end

instructions = {
parts: [part]
}
Expand All @@ -151,7 +153,7 @@ def split(file:, ranges:)
def redact(file:, text: [])
parts = [build_part(file, 'document')]
actions = []

# Create a redaction action for each text term
text.each do |term|
actions << {
Expand All @@ -162,10 +164,10 @@ def redact(file:, text: [])
}
}
end

# Apply all redactions at the end
actions << { type: 'applyRedactions' }

instructions = {
parts: parts,
actions: actions
Expand Down Expand Up @@ -205,7 +207,7 @@ def url?(file)

def make_request(instructions, files: {})
uri = URI("#{API_BASE_URL}#{API_ENDPOINT}")

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true

Expand Down Expand Up @@ -244,7 +246,7 @@ def build_multipart_body(instructions, files)
filename = extract_filename(file)

body << "--#{@boundary}"
body << %Q{Content-Disposition: form-data; name="#{file_key}"; filename="#{filename}"}
body << %(Content-Disposition: form-data; name="#{file_key}"; filename="#{filename}")
body << 'Content-Type: application/octet-stream'
body << ''
body << file_content
Expand Down Expand Up @@ -284,7 +286,7 @@ def handle_response(response)
raise NutrientDWS::AuthenticationError, 'Invalid or missing API key'
else
# Add debugging info to error message
error_details = ""
error_details = ''
begin
parsed_error = JSON.parse(response.body)
if parsed_error['error'] && parsed_error['error']['failingPaths']
Expand All @@ -297,7 +299,7 @@ def handle_response(response)
# If we can't parse the error, just include the raw body
error_details = "\nRaw response: #{response.body}"
end

raise NutrientDWS::APIError.new(
"API request failed: #{response.message}#{error_details}",
status_code: response.code.to_i,
Expand All @@ -307,4 +309,4 @@ def handle_response(response)
end
end
end
end
end
4 changes: 2 additions & 2 deletions lib/nutrient_dws/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module NutrientDWS
VERSION = "0.1.0"
end
VERSION = '0.1.0'
end
42 changes: 21 additions & 21 deletions nutrient-dws.gemspec
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
# frozen_string_literal: true

require_relative "lib/nutrient_dws/version"
require_relative 'lib/nutrient_dws/version'

Gem::Specification.new do |spec|
spec.name = "nutrient-dws"
spec.name = 'nutrient-dws'
spec.version = NutrientDWS::VERSION
spec.authors = ["Nutrient"]
spec.email = ["support@nutrient.io"]
spec.authors = ['Nutrient']
spec.email = ['support@nutrient.io']

spec.summary = "Ruby client library for Nutrient DWS Processor API"
spec.description = "A Ruby gem that provides a clean, idiomatic interface for interacting with the Nutrient DWS Processor API for document processing operations like conversion, OCR, watermarking, and PDF editing."
spec.homepage = "https://github.com/nutrient-io/nutrient-dws-client-ruby"
spec.required_ruby_version = ">= 2.7.0"
spec.summary = 'Ruby client library for Nutrient DWS Processor API'
spec.description = 'A Ruby gem that provides a clean, idiomatic interface for interacting with the Nutrient DWS Processor API for document processing operations like conversion, OCR, watermarking, and PDF editing.'
spec.homepage = 'https://github.com/nutrient-io/nutrient-dws-client-ruby'
spec.required_ruby_version = '>= 3.0.0'

spec.metadata["allowed_push_host"] = "https://rubygems.org"
spec.metadata["homepage_uri"] = spec.homepage
spec.metadata["source_code_uri"] = spec.homepage
spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md"
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
spec.metadata['homepage_uri'] = spec.homepage
spec.metadata['source_code_uri'] = spec.homepage
spec.metadata['changelog_uri'] = "#{spec.homepage}/blob/main/CHANGELOG.md"

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
Expand All @@ -26,17 +26,17 @@ Gem::Specification.new do |spec|
f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor Gemfile])
end
end
spec.bindir = "exe"
spec.bindir = 'exe'
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.require_paths = ['lib']

# No runtime dependencies - uses only Ruby standard library

# Development dependencies
spec.add_development_dependency "bundler", "~> 2.0"
spec.add_development_dependency "rake", "~> 13.0"
spec.add_development_dependency "rspec", "~> 3.0"
spec.add_development_dependency "dotenv", "~> 2.8"
spec.add_development_dependency "webmock", "~> 3.18"
spec.add_development_dependency "rubocop", "~> 1.21"
end
spec.add_development_dependency 'bundler', '~> 2.0'
spec.add_development_dependency 'dotenv', '~> 2.8'
spec.add_development_dependency 'rake', '~> 13.0'
spec.add_development_dependency 'rspec', '~> 3.0'
spec.add_development_dependency 'rubocop', '~> 1.21'
spec.add_development_dependency 'webmock', '~> 3.18'
end
4 changes: 2 additions & 2 deletions spec/nutrient_dws/error_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
status_code: 400,
response_body: '{"error": "Invalid parameter"}'
)

expect(error.message).to eq('Bad request')
expect(error.status_code).to eq(400)
expect(error.response_body).to eq('{"error": "Invalid parameter"}')
Expand All @@ -68,4 +68,4 @@
expect(error.response_body).to eq('Internal server error')
end
end
end
end
10 changes: 5 additions & 5 deletions spec/nutrient_dws/processor/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@
client.convert(file: 'non_existent_file.docx', to: 'pdf')
end.to raise_error(Errno::ENOENT)
end

it 'raises an error for unsupported output format' do
skip 'sample.docx fixture not found' unless File.exist?(docx_file)

expect do
client.convert(file: docx_file, to: 'unsupported_format')
end.to raise_error(ArgumentError, /Unsupported output format/)
Expand Down Expand Up @@ -169,7 +169,7 @@
it 'successfully redacts text from a PDF' do
skip 'sample.pdf fixture not found' unless File.exist?(pdf_file)

redacted_pdf = client.redact(file: pdf_file, text: ['confidential', 'secret'])
redacted_pdf = client.redact(file: pdf_file, text: %w[confidential secret])
expect(redacted_pdf).to be_a_valid_pdf
end

Expand Down Expand Up @@ -214,11 +214,11 @@
pdf_result = client.convert(file: remote_pdf_url, to: 'pdf')
expect(pdf_result).to be_a_valid_pdf
end

it 'successfully converts remote URLs to image formats' do
png_result = client.convert(file: remote_pdf_url, to: 'png')
expect(png_result).to be_a(String)
expect(png_result).not_to be_empty
end
end
end
end
Loading