Sha256: 7308809912292c6ecde9c81053575b62d82ec2e0629c1a8f1a5ad90947720edc

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

module OpenAmplify

  module  Analysis

    class Context
      VALID_OPTIONS_KEYS = OpenAmplify::Configuration::VALID_OPTIONS_KEYS + [:input]

      attr_accessor *VALID_OPTIONS_KEYS

      attr_accessor :client
      attr_reader   :result

      def initialize(client, input, options)
        self.client = client
        self.input  = input

        merged_options = OpenAmplify.options.merge(options)
        OpenAmplify::Configuration::VALID_OPTIONS_KEYS.each do |key|
          send("#{key}=", merged_options[key])
        end
      end

      # The empty?, to_s, to_str are to make our object play
      # with others that expect a string.
      #
      # Example, you can do this:
      #   puts result
      #
      # instead of:
      #   puts result.to_s

      def empty?
        result.empty?
      end

      def to_s
        "#{result}"
      end
      alias_method :to_str, :to_s

      # Support various conversion helpers

      %w(xml json json_js rdf rdfa csv signals pretty dart oas).each do |format|
        define_method("to_#{format}") do
          @result = call(:output_format => format)
        end
      end

      private

      def result
        @result ||= call
      end

      def call(options={})
        @result = client.request_analysis(request_analysis_options.merge(options))
      end

      def request_analysis_options
        Hash[* VALID_OPTIONS_KEYS.map { |key| [key, send(key) ]}.flatten ]
      end

    end # Context

  end # Analysis

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
openamplify-0.3.0 lib/openamplify/analysis/context.rb