diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..6fd6061 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +.rubocop_todo.yml linguist-generated diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..ca79ca5 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,6 @@ +version: 2 +updates: + - package-ecosystem: github-actions + directory: / + schedule: + interval: weekly diff --git a/.github/workflows/rubocop.yml b/.github/workflows/rubocop.yml new file mode 100644 index 0000000..4dee9e8 --- /dev/null +++ b/.github/workflows/rubocop.yml @@ -0,0 +1,30 @@ +name: RuboCop + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +permissions: + contents: read + +jobs: + test: + name: RuboCop + runs-on: ubuntu-latest + + steps: + - name: Install libmagic-dev + run: | + sudo apt-get update + sudo apt-get -yq --no-install-suggests --no-install-recommends install libmagic-dev + - uses: actions/checkout@v4 + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: '3.4' + rubygems: latest + bundler-cache: true + - name: RuboCop + run: bundle exec rubocop -f github diff --git a/.github/workflows/ruby.yml b/.github/workflows/ruby.yml index 007245a..23084a7 100644 --- a/.github/workflows/ruby.yml +++ b/.github/workflows/ruby.yml @@ -16,9 +16,13 @@ jobs: strategy: matrix: - ruby-version: ['2.3', '2.4', '2.5', '2.6', '2.7'] + ruby-version: ['3.0', '3.1', '3.2', '3.3', '3.4'] channel: [stable] + include: + - ruby-version: 'ruby-head' + channel: experimental + continue-on-error: ${{ matrix.channel != 'stable' }} steps: diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..f5e755b --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,30 @@ +inherit_from: .rubocop_todo.yml + +require: + - rubocop-packaging + - rubocop-performance + - rubocop-rake + - rubocop-rspec + +AllCops: + NewCops: enable + TargetRubyVersion: 3.0 + Exclude: + - .git/**/* + - .github/**/* + - gemfiles/**/* + - node_modules/**/* + - tmp/**/* + - vendor/**/* + +Layout/LineLength: + Enabled: false + +RSpec/ExampleLength: + Enabled: false + +RSpec/MultipleExpectations: + Enabled: false + +Style/ArgumentsForwarding: + Enabled: false diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml new file mode 100644 index 0000000..6cf76d7 --- /dev/null +++ b/.rubocop_todo.yml @@ -0,0 +1,433 @@ +# This configuration was generated by +# `rubocop --auto-gen-config` +# on 2025-01-16 17:28:45 UTC using RuboCop version 1.70.0. +# The point is for the user to remove these configuration records +# one by one as the offenses are removed from the code base. +# Note that changes in the inspected code, or installation of new +# versions of RuboCop, may require this file to be generated again. + +# Offense count: 2 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyleAlignWith. +# SupportedStylesAlignWith: either, start_of_block, start_of_line +Layout/BlockAlignment: + Exclude: + - 'spec/lib/colore/errors_spec.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +Layout/BlockEndNewline: + Exclude: + - 'spec/lib/colore/client_spec.rb' + +# Offense count: 11 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyle, IndentOneStep, IndentationWidth. +# SupportedStyles: case, end +Layout/CaseIndentation: + Exclude: + - 'bin/colore-client' + - 'lib/colore/errors.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EmptyLineBetweenMethodDefs, EmptyLineBetweenClassDefs, EmptyLineBetweenModuleDefs, DefLikeMacros, AllowAdjacentOneLineDefs, NumberOfEmptyLines. +Layout/EmptyLineBetweenDefs: + Exclude: + - 'lib/colore/errors.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: AllowAliasSyntax, AllowedMethods. +# AllowedMethods: alias_method, public, protected, private +Layout/EmptyLinesAroundAttributeAccessor: + Exclude: + - 'lib/colore/errors.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +Layout/EmptyLinesAroundExceptionHandlingKeywords: + Exclude: + - 'lib/colore/client.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: AllowForAlignment, AllowBeforeTrailingComments, ForceEqualSignAlignment. +Layout/ExtraSpacing: + Exclude: + - 'lib/colore/client.rb' + +# Offense count: 11 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyle. +# SupportedStyles: symmetrical, new_line, same_line +Layout/MultilineMethodCallBraceLayout: + Exclude: + - 'spec/lib/colore/client_spec.rb' + +# Offense count: 10 +# This cop supports safe autocorrection (--autocorrect). +Layout/SpaceAfterColon: + Exclude: + - 'bin/colore-client' + - 'lib/colore/client.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +Layout/SpaceAfterComma: + Exclude: + - 'lib/colore/client.rb' + +# Offense count: 4 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyle. +# SupportedStyles: space, no_space +Layout/SpaceAroundEqualsInParameterDefault: + Exclude: + - 'lib/colore/client.rb' + - 'lib/colore/errors.rb' + +# Offense count: 7 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: AllowForAlignment, EnforcedStyleForExponentOperator, EnforcedStyleForRationalLiterals. +# SupportedStylesForExponentOperator: space, no_space +# SupportedStylesForRationalLiterals: space, no_space +Layout/SpaceAroundOperators: + Exclude: + - 'bin/colore-client' + - 'spec/lib/colore/errors_spec.rb' + +# Offense count: 8 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces. +# SupportedStyles: space, no_space +# SupportedStylesForEmptyBraces: space, no_space +Layout/SpaceBeforeBlockBraces: + Exclude: + - 'spec/lib/colore/client_spec.rb' + +# Offense count: 6 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBrackets. +# SupportedStyles: space, no_space, compact +# SupportedStylesForEmptyBrackets: space, no_space +Layout/SpaceInsideArrayLiteralBrackets: + Exclude: + - 'spec/lib/colore/client_spec.rb' + +# Offense count: 4 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces, SpaceBeforeBlockParameters. +# SupportedStyles: space, no_space +# SupportedStylesForEmptyBraces: space, no_space +Layout/SpaceInsideBlockBraces: + Exclude: + - 'spec/lib/colore/client_spec.rb' + +# Offense count: 91 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyle. +# SupportedStyles: space, compact, no_space +Layout/SpaceInsideParens: + Exclude: + - 'bin/colore-client' + - 'lib/colore/client.rb' + - 'lib/colore/errors.rb' + - 'spec/lib/colore/client_spec.rb' + +# Offense count: 2 +# This cop supports safe autocorrection (--autocorrect). +Lint/AmbiguousOperatorPrecedence: + Exclude: + - 'bin/colore-client' + - 'spec/spec_helper.rb' + +# Offense count: 1 +# This cop supports unsafe autocorrection (--autocorrect-all). +Lint/RedundantRequireStatement: + Exclude: + - 'bin/colore-client' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: AutoCorrect, AllowUnusedKeywordArguments, IgnoreEmptyMethods, IgnoreNotImplementedMethods, NotImplementedExceptions. +# NotImplementedExceptions: NotImplementedError +Lint/UnusedMethodArgument: + Exclude: + - 'lib/colore/client.rb' + +# Offense count: 6 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: AutoCorrect. +Lint/UselessAssignment: + Exclude: + - 'bin/colore-client' + - 'lib/colore/client.rb' + +# Offense count: 1 +# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes. +Metrics/AbcSize: + Max: 35 + +# Offense count: 1 +# Configuration parameters: CountComments, CountAsOne. +Metrics/ClassLength: + Max: 153 + +# Offense count: 3 +# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns. +Metrics/MethodLength: + Max: 29 + +# Offense count: 2 +# Configuration parameters: CountKeywordArgs, MaxOptionalParameters. +Metrics/ParameterLists: + Max: 7 + +# Offense count: 1 +# Configuration parameters: ExpectMatchingDefinition, CheckDefinitionPathHierarchy, CheckDefinitionPathHierarchyRoots, Regex, IgnoreExecutableScripts, AllowedAcronyms. +# CheckDefinitionPathHierarchyRoots: lib, spec, test, src +# AllowedAcronyms: CLI, DSL, ACL, API, ASCII, CPU, CSS, DNS, EOF, GUID, HTML, HTTP, HTTPS, ID, IP, JSON, LHS, QPS, RAM, RHS, RPC, SLA, SMTP, SQL, SSH, TCP, TLS, TTL, UDP, UI, UID, UUID, URI, URL, UTF8, VM, XML, XMPP, XSRF, XSS +Naming/FileName: + Exclude: + - 'Rakefile.rb' + - 'lib/colore-client.rb' + +# Offense count: 2 +# Configuration parameters: EnforcedStyle, CheckMethodNames, CheckSymbols, AllowedIdentifiers, AllowedPatterns. +# SupportedStyles: snake_case, normalcase, non_integer +# AllowedIdentifiers: capture3, iso8601, rfc1123_date, rfc822, rfc2822, rfc3339, x86_64 +Naming/VariableNumber: + Exclude: + - 'spec/lib/colore/errors_spec.rb' + +# Offense count: 1 +# This cop supports unsafe autocorrection (--autocorrect-all). +RSpec/BeEq: + Exclude: + - 'spec/lib/colore/client_spec.rb' + +# Offense count: 14 +# This cop supports safe autocorrection (--autocorrect). +RSpec/ContextMethod: + Exclude: + - 'spec/lib/colore/client_spec.rb' + - 'spec/lib/colore/errors_spec.rb' + +# Offense count: 14 +# Configuration parameters: Prefixes, AllowedPatterns. +# Prefixes: when, with, without +RSpec/ContextWording: + Exclude: + - 'spec/lib/colore/client_spec.rb' + - 'spec/lib/colore/errors_spec.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +RSpec/EmptyLineAfterFinalLet: + Exclude: + - 'spec/lib/colore/errors_spec.rb' + +# Offense count: 2 +# Configuration parameters: Max, AllowedIdentifiers, AllowedPatterns. +RSpec/IndexedLet: + Exclude: + - 'spec/lib/colore/errors_spec.rb' + +# Offense count: 1 +# Configuration parameters: . +# SupportedStyles: have_received, receive +RSpec/MessageSpies: + EnforcedStyle: receive + +# Offense count: 15 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyle. +# SupportedStyles: not_to, to_not +RSpec/NotToNot: + Exclude: + - 'spec/lib/colore/client_spec.rb' + +# Offense count: 1 +RSpec/StubbedMock: + Exclude: + - 'spec/lib/colore/client_spec.rb' + +# Offense count: 5 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyle. +# SupportedStyles: separated, grouped +Style/AccessorGrouping: + Exclude: + - 'lib/colore/client.rb' + - 'lib/colore/errors.rb' + +# Offense count: 9 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyle, ProceduralMethods, FunctionalMethods, AllowedMethods, AllowedPatterns, AllowBracesOnProceduralOneLiners, BracesRequiredMethods. +# SupportedStyles: line_count_based, semantic, braces_for_chaining, always_braces +# ProceduralMethods: benchmark, bm, bmbm, create, each_with_object, measure, new, realtime, tap, with_object +# FunctionalMethods: let, let!, subject, watch +# AllowedMethods: lambda, proc, it +Style/BlockDelimiters: + Exclude: + - 'spec/lib/colore/client_spec.rb' + - 'spec/lib/colore/errors_spec.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +Style/ColonMethodCall: + Exclude: + - 'spec/spec_helper.rb' + +# Offense count: 4 +# Configuration parameters: AllowedConstants. +Style/Documentation: + Exclude: + - 'spec/**/*' + - 'test/**/*' + - 'lib/colore/client.rb' + - 'lib/colore/errors.rb' + +# Offense count: 2 +# This cop supports safe autocorrection (--autocorrect). +Style/FileWrite: + Exclude: + - 'bin/colore-client' + +# Offense count: 9 +# This cop supports unsafe autocorrection (--autocorrect-all). +# Configuration parameters: EnforcedStyle. +# SupportedStyles: always, always_true, never +Style/FrozenStringLiteralComment: + Exclude: + - 'Rakefile' + - 'bin/colore-client' + - 'lib/colore-client.rb' + - 'lib/colore/client.rb' + - 'lib/colore/client/version.rb' + - 'lib/colore/errors.rb' + - 'spec/lib/colore/client_spec.rb' + - 'spec/lib/colore/errors_spec.rb' + - 'spec/spec_helper.rb' + +# Offense count: 2 +# This cop supports unsafe autocorrection (--autocorrect-all). +Style/GlobalStdStream: + Exclude: + - 'bin/colore-client' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyle, EnforcedShorthandSyntax, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols. +# SupportedStyles: ruby19, hash_rockets, no_mixed_keys, ruby19_no_mixed_keys +# SupportedShorthandSyntax: always, never, either, consistent, either_consistent +Style/HashSyntax: + Exclude: + - 'Rakefile' + +# Offense count: 3 +# This cop supports safe autocorrection (--autocorrect). +Style/KeywordParametersOrder: + Exclude: + - 'lib/colore/client.rb' + +# Offense count: 6 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyle. +# SupportedStyles: require_parentheses, require_no_parentheses, require_no_parentheses_except_multiline +Style/MethodDefParentheses: + Exclude: + - 'lib/colore/client.rb' + - 'lib/colore/errors.rb' + - 'spec/spec_helper.rb' + +# Offense count: 3 +# This cop supports unsafe autocorrection (--autocorrect-all). +# Configuration parameters: EnforcedStyle. +# SupportedStyles: literals, strict +Style/MutableConstant: + Exclude: + - 'bin/colore-client' + - 'lib/colore/client.rb' + - 'lib/colore/client/version.rb' + +# Offense count: 3 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: AllowedMethods. +# AllowedMethods: be, be_a, be_an, be_between, be_falsey, be_kind_of, be_instance_of, be_truthy, be_within, eq, eql, end_with, include, match, raise_error, respond_to, start_with +Style/NestedParenthesizedCalls: + Exclude: + - 'spec/lib/colore/errors_spec.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: PreferredDelimiters. +Style/PercentLiteralDelimiters: + Exclude: + - 'bin/colore-client' + +# Offense count: 1 +# This cop supports unsafe autocorrection (--autocorrect-all). +# Configuration parameters: AllowedCompactTypes. +# SupportedStyles: compact, exploded +Style/RaiseArgs: + EnforcedStyle: compact + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +Style/RedundantCondition: + Exclude: + - 'bin/colore-client' + +# Offense count: 2 +# This cop supports safe autocorrection (--autocorrect). +Style/RedundantPercentQ: + Exclude: + - 'colore-client.gemspec' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: AllowAsExpressionSeparator. +Style/Semicolon: + Exclude: + - 'bin/colore-client' + +# Offense count: 2 +# This cop supports unsafe autocorrection (--autocorrect-all). +# Configuration parameters: RequireEnglish. +# SupportedStyles: use_perl_names, use_english_names, use_builtin_english_names +Style/SpecialGlobalVars: + EnforcedStyle: use_perl_names + +# Offense count: 4 +# This cop supports unsafe autocorrection (--autocorrect-all). +# Configuration parameters: Mode. +Style/StringConcatenation: + Exclude: + - 'bin/colore-client' + - 'spec/spec_helper.rb' + +# Offense count: 8 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline. +# SupportedStyles: single_quotes, double_quotes +Style/StringLiterals: + Exclude: + - 'bin/colore-client' + - 'lib/colore/client.rb' + - 'lib/colore/errors.rb' + - 'spec/lib/colore/client_spec.rb' + +# Offense count: 2 +# This cop supports safe autocorrection (--autocorrect). +Style/SuperWithArgsParentheses: + Exclude: + - 'lib/colore/errors.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: MinSize. +# SupportedStyles: percent, brackets +Style/SymbolArray: + EnforcedStyle: brackets diff --git a/Gemfile b/Gemfile index fa75df1..004bbea 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,20 @@ +# frozen_string_literal: true + source 'https://rubygems.org' gemspec + +gem 'byebug' +gem 'rake' +gem 'rspec' +gem 'ruby-filemagic' +gem 'simplecov' +gem 'vcr' +gem 'webmock' +gem 'yard' + +gem 'rubocop', require: false +gem 'rubocop-packaging', require: false +gem 'rubocop-performance', require: false +gem 'rubocop-rake', require: false +gem 'rubocop-rspec', require: false diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..6a21092 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2012 Joe Blackman, 2025 IFAD + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.md b/README.md index c865342..d8bbb1e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # Colore::Client [![Ruby specs](https://github.com/ifad/colore-client/actions/workflows/ruby.yml/badge.svg)](https://github.com/ifad/colore-client/actions/workflows/ruby.yml) +[![RuboCop](https://github.com/ifad/colore-client/actions/workflows/rubocop.yml/badge.svg)](https://github.com/ifad/colore-client/actions/workflows/rubocop.yml) [![Inline docs](https://inch-ci.org/github/ifad/colore-client.svg?branch=master)](https://inch-ci.org/github/ifad/colore-client) [![Code Climate](https://codeclimate.com/github/ifad/colore-client/badges/gpa.svg)](https://codeclimate.com/github/ifad/colore-client) diff --git a/bin/colore-client b/bin/colore-client index c223e26..0c5ae98 100755 --- a/bin/colore-client +++ b/bin/colore-client @@ -29,7 +29,7 @@ REQUESTS = %w( request_conversion delete_document delete_version get_document get_document_info convert - ) +) OptionParser.new do |opts| opts.banner = 'Usage: colore_client -r {request} [opts]' @@ -109,7 +109,6 @@ begin else STDOUT.write resp end - rescue Colore::Errors::APIError => e puts "Received error from colore: #{e.http_code}, #{e.message}" pp e.rsp_backtrace if backtrace diff --git a/colore-client.gemspec b/colore-client.gemspec index 349e327..bb049a7 100644 --- a/colore-client.gemspec +++ b/colore-client.gemspec @@ -1,25 +1,29 @@ -Gem::Specification.new do |gem| - gem.name = 'colore-client' - gem.version = '0.2.0' - gem.authors = [ 'Joe Blackman' ] - gem.email = [ 'j.blackman@ifad.org' ] - gem.description = %q(Ruby client to consume Colore services) - gem.summary = %q(Ruby client to consume Colore services) - gem.homepage = '' +# frozen_string_literal: true - gem.add_dependency 'rest-client', '>=1.7.2' - gem.add_dependency 'hashugar', '>= 1.0.0' +require File.expand_path('lib/colore/client/version', __dir__) - gem.add_development_dependency 'rspec', '>= 3.2.0' - gem.add_development_dependency 'vcr' - gem.add_development_dependency 'webmock' - gem.add_development_dependency 'simplecov' - gem.add_development_dependency 'yard' - gem.add_development_dependency 'rake' - gem.add_development_dependency 'byebug' - gem.add_development_dependency 'ruby-filemagic' +Gem::Specification.new do |spec| + spec.name = 'colore-client' + spec.version = Colore::Client::VERSION + spec.authors = ['Joe Blackman'] + spec.email = ['j.blackman@ifad.org'] + spec.description = %q(Ruby client to consume Colore services) + spec.summary = %q(Ruby client to consume Colore services) - gem.files = `git ls-files`.split($/) - gem.test_files = gem.files.grep %r[^(test|spec|features)] - gem.require_paths = ['lib'] + spec.homepage = 'https://github.com/ifad/colore-client' + spec.license = 'MIT' + + spec.files = Dir.glob('{LICENSE,README.md,lib/**/*.rb,bin/*}', File::FNM_DOTMATCH) + spec.require_paths = ['lib'] + + spec.metadata = { + 'bug_tracker_uri' => 'https://github.com/ifad/colore-client/issues', + 'homepage_uri' => 'https://github.com/ifad/colore-client', + 'source_code_uri' => 'https://github.com/ifad/colore-client', + 'rubygems_mfa_required' => 'true' + } + + spec.required_ruby_version = '>= 3.0' + + spec.add_dependency 'faraday', '>= 2.12' end diff --git a/lib/colore-client.rb b/lib/colore-client.rb index 76e1baa..cb20c98 100644 --- a/lib/colore-client.rb +++ b/lib/colore-client.rb @@ -1,2 +1,2 @@ -require_relative 'colore/errors.rb' -require_relative 'colore/client.rb' +require_relative 'colore/errors' +require_relative 'colore/client' diff --git a/lib/colore/client.rb b/lib/colore/client.rb index 02239ae..945dca0 100644 --- a/lib/colore/client.rb +++ b/lib/colore/client.rb @@ -1,8 +1,11 @@ +require 'faraday' require 'json' -require 'tempfile' -require 'rest_client' -require 'securerandom' require 'logger' +require 'securerandom' +require 'tempfile' +require 'uri' + +require_relative 'client/version' module Colore # The name of the 'current' version @@ -17,6 +20,15 @@ def self.generate_doc_id SecureRandom.uuid end + def self.uri_parser + @uri_parser ||= + if defined?(URI::RFC2396_PARSER) + URI::RFC2396_PARSER + else + URI::DEFAULT_PARSER + end + end + # Constructor # @param base_uri [String] The base URI of the Colore service that you wish to attach to # @param app [String] The name of your application (all documents will be stored under this name) @@ -27,6 +39,7 @@ def initialize( base_uri: 'http://localhost:9240/', app:, logger: Logger.new(nil @app = app @backtrace = backtrace @logger = logger + @connection = Faraday.new(headers: { 'User-Agent' => "Colore Client #{Colore::Client::VERSION} (#{app})" }) end # Generates a document id that is reasonably guaranteed to be unique for your app @@ -105,7 +118,7 @@ def update_document( doc_id:, filename:, content: nil, author: nil, actions:nil, # @param doc_id [String] the document's unique identifier # @param title [String] A short description of the document def update_title( doc_id:, title: ) - send_request :post, "document/#{@app}/#{doc_id}/title/#{URI.escape title}", {}, :json + send_request :post, "document/#{@app}/#{doc_id}/title/#{self.class.uri_parser.escape title}", {}, :json end # Requests a conversion of an existing document @@ -198,37 +211,47 @@ def send_request type, path, params={}, expect=:binary url = URI.join(@base_uri, path).to_s logger.debug( "Send #{type}: #{url}" ) logger.debug( " params: #{params.inspect}" ) - response = nil - case type - when :get - response = RestClient.get url + response = + if type == :get + connection.get(url) else - response = RestClient.send type.to_sym, url, params - end - if expect == :json - logger.debug( " received : #{response}") - return JSON.parse(response) - else - logger.debug( " received : [BINARY #{response.size} bytes]") - return response - end - rescue Errno::ECONNREFUSED - raise Errors::ColoreUnavailable.new - rescue RestClient::InternalServerError, RestClient::BadRequest, RestClient::Conflict => e - logger.debug( " received #{e.class.name}: #{e.message}") - error = nil - begin - error = Colore::Errors.from JSON.parse(e.http_body), e.http_body - rescue StandardError => ex - error = Colore::Errors.from nil, e.http_body + connection.send(type.to_sym, url, params) + end + + if response.success? + response_body = response.body + + if expect == :json + logger.debug( " received : #{response_body}") + return JSON.parse(response_body) + else + logger.debug( " received : [BINARY #{response_body.bytesize} bytes]") + return response_body + end end + + logger.debug( " received #{response.status}: #{response.reason_phrase}") + + error = + begin + Colore::Errors.from JSON.parse(response.body), response.body + rescue StandardError + Colore::Errors.from nil, response.body + end + raise error + + rescue Faraday::ConnectionFailed + raise Errors::ColoreUnavailable.new end + private + + attr_reader :connection + # # Saves the content into a tempfile, rather than trying to read it all into memory # This allows us to handle passing an IO for a 300MB file without crashing. - # RestClient needs a file for uploads. # def with_tempfile content, &block Tempfile.open( 'colore' ) do |tf| diff --git a/lib/colore/client/version.rb b/lib/colore/client/version.rb new file mode 100644 index 0000000..317ef11 --- /dev/null +++ b/lib/colore/client/version.rb @@ -0,0 +1,5 @@ +module Colore + class Client + VERSION = '1.0.0' + end +end diff --git a/spec/lib/colore/client_spec.rb b/spec/lib/colore/client_spec.rb index 12c3242..f0e0546 100644 --- a/spec/lib/colore/client_spec.rb +++ b/spec/lib/colore/client_spec.rb @@ -27,7 +27,7 @@ end it 'raises ColoreUnavailable on ECONNREFUSED' do - expect(RestClient).to receive(:get) { raise Errno::ECONNREFUSED.new } + expect(client.send(:connection)).to receive(:get) { raise Faraday::ConnectionFailed } expect{client.ping}.to raise_error(Colore::Errors::ColoreUnavailable) end end