Sha256: eed29502a36facb791884afb4e10070d83fd854a7374bf973f19b147500ddebe

Contents?: true

Size: 1.19 KB

Versions: 8

Compression:

Stored size: 1.19 KB

Contents

class Evil::Client
  # Utility to format body/query into one of formats: :json, :form, :multipart
  module Formatter
    extend self

    # Loads concrete formatters called by factory method [#call]
    require_relative "formatter/text"
    require_relative "formatter/form"
    require_relative "formatter/multipart"

    # Factory that knows how to format source depending on given format
    #
    # @param  [Object] source
    # @param  [:json, :form, :multipart, :text] format
    # @option opts [String] :boundary The boundary for a multipart body
    # @return [String] formatted body
    #
    def call(source, format, **opts)
      return unless source
      return to_json(source) if format == :json
      return to_yaml(source) if format == :yaml
      return to_form(source) if format == :form
      return to_text(source) if format == :text
      to_multipart(source, opts)
    end

    private

    def to_json(source)
      JSON.dump source
    end

    def to_yaml(source)
      YAML.dump source
    end

    def to_text(source)
      Text.call source
    end

    def to_form(source)
      Form.call source
    end

    def to_multipart(source, opts)
      Multipart.call [source], opts
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
evil-client-3.0.2 lib/evil/client/formatter.rb
evil-client-3.0.1 lib/evil/client/formatter.rb
evil-client-3.0.0 lib/evil/client/formatter.rb
evil-client-2.1.1 lib/evil/client/formatter.rb
evil-client-2.1.0 lib/evil/client/formatter.rb
evil-client-2.0.0 lib/evil/client/formatter.rb
evil-client-1.1.0 lib/evil/client/formatter.rb
evil-client-1.0.0 lib/evil/client/formatter.rb