Sha256: 4a7fe9dabc31add97b11937535f8ccaebff90ba46c615931350adaadb668e39a

Contents?: true

Size: 1.78 KB

Versions: 15

Compression:

Stored size: 1.78 KB

Contents

require 'json'

module PortaText
  module Command
    # The base command class.
    #
    # Author::    Marcelo Gornstein (mailto:marcelog@portatext.com)
    # Copyright:: Copyright (c) 2015 PortaText
    # License::   Apache-2.0
    class Base
      attr_writer :client

      def get
        run :get
      end

      def post
        run :post
      end

      def put
        run :put
      end

      def patch
        run :patch
      end

      def delete
        run :delete
      end

      def set(key, value)
        @args[key] = value
        self
      end

      def content_type(_method)
        return 'text/csv' unless @args[:file].nil?
        return 'audio/mpeg' unless @args[:sound_file].nil?
        'application/json'
      end

      def accept_content_type(_method)
        return '*/*' unless @args[:accept_any_file].nil?
        return 'text/csv' unless @args[:accept_file].nil?
        return 'audio/mpeg' unless @args[:accept_sound_file].nil?
        'application/json'
      end

      def body(_method)
        return "file:#{@args[:file]}" unless @args[:file].nil?
        return '' if @args.size.eql? 0
        @args.to_json
      end

      def initialize
        @args = {}
      end

      private

      # rubocop:disable Metrics/MethodLength
      def run(method)
        a_type = accept_content_type method
        command_endpoint = endpoint(method)
        file = @args[:accept_file]
        file ||= @args[:accept_any_file]
        file ||= @args[:accept_sound_file]
        @args.delete :accept_file
        @args.delete :accept_any_file
        @args.delete :accept_sound_file
        @client.run(
          command_endpoint, method, content_type(method),
          a_type, body(method), file
        )
      end
      # rubocop:enable Metrics/MethodLength
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
portatext-1.6.1 lib/portatext/command/base.rb
portatext-1.6.0 lib/portatext/command/base.rb
portatext-1.5.23 lib/portatext/command/base.rb
portatext-1.5.22 lib/portatext/command/base.rb
portatext-1.5.21 lib/portatext/command/base.rb
portatext-1.5.20 lib/portatext/command/base.rb
portatext-1.5.19 lib/portatext/command/base.rb
portatext-1.5.18 lib/portatext/command/base.rb
portatext-1.5.17 lib/portatext/command/base.rb
portatext-1.5.16 lib/portatext/command/base.rb
portatext-1.5.15 lib/portatext/command/base.rb
portatext-1.5.14 lib/portatext/command/base.rb
portatext-1.5.13 lib/portatext/command/base.rb
portatext-1.5.12 lib/portatext/command/base.rb
portatext-1.5.11 lib/portatext/command/base.rb