Sha256: f54d840c81c639380b0b06c72ae2c19bcb262434e13cfb677b0c57e78ae2ebfc

Contents?: true

Size: 1.1 KB

Versions: 2

Compression:

Stored size: 1.1 KB

Contents

module SentimentAl
  class Service
    class << self
      attr_reader :params, :response, :unincluded_params

      # => Run the service if it has
      # all the necessary data
      #
      def call(params)
        @params = params
        unincluded_error unless required_params?
        
        HttpRequest.new(params) do |request|
          @response = request.start
        end

        self
      end

      def successfully?
        response.code.start_with?('2')
      end

      def response_to_hash
        JSON.parse(response.body)
      end

      private

      # => Search information about
      # missing data in parameters and
      # response true/false
      #
      def required_params?
        @unincluded_params = []

        required_params.each do |param|
          unless params.include?(param)
            unincluded_params << param
          end
        end

        unincluded_params.empty?
      end

      # => *
      def required_params
        %i(text)
      end

      # => *
      def unincluded_error
        raise Exception, "required params #{unincluded_params}"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sentiment-all-1.2.1beta lib/sentiment_al/service.rb
sentiment-all-1.2.0beta lib/sentiment_al/service.rb