Sha256: 1d027f330b99adff4cace7678f7120a04c7429f77a75fb771f9da777eaa958a2

Contents?: true

Size: 1.44 KB

Versions: 13

Compression:

Stored size: 1.44 KB

Contents

require 'httparty'

module AnswersEngine
  module Client
    class Base
      include HTTParty

      def self.env_auth_token
        ENV['ANSWERSENGINE_TOKEN']
      end

      def env_api_url
        ENV['ANSWERSENGINE_API_URL'].nil? ? 'https://fetch.answersengine.com/api/v1' : ENV['ANSWERSENGINE_API_URL']
      end

      def auth_token
        @auth_token ||= self.class.env_auth_token
      end

      def auth_token= value
        @auth_token = value
      end

      def initialize(opts={})
        self.class.base_uri(env_api_url)
        self.auth_token = opts[:auth_token] unless opts[:auth_token].nil?
        @options = { headers: {
          "Authorization" => "Bearer #{auth_token}",
          "Content-Type" => "application/json",
          }}

        query = {}
        query[:p] = opts[:page] if opts[:page]
        query[:pp] = opts[:per_page] if opts[:per_page]
        query[:fetchfail] = opts[:fetch_fail] if opts[:fetch_fail]
        query[:parsefail] = opts[:parse_fail] if opts[:parse_fail]
        query[:page_type] = opts[:page_type] if opts[:page_type]
        query[:gid] = opts[:gid] if opts[:gid]

        if opts[:query]
          if opts[:query].is_a?(Hash)
            query[:q] = opts[:query].to_json
          elsif opts[:query].is_a?(String)
            query[:q] = JSON.parse(opts[:query]).to_json
          end
        end

        unless query.empty?
          @options.merge!(query: query)
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
answersengine-0.10.0 lib/answersengine/client/base.rb
answersengine-0.9.1 lib/answersengine/client/base.rb
answersengine-0.9.0 lib/answersengine/client/base.rb
answersengine-0.8.0 lib/answersengine/client/base.rb
answersengine-0.7.0 lib/answersengine/client/base.rb
answersengine-0.6.0 lib/answersengine/client/base.rb
answersengine-0.5.0 lib/answersengine/client/base.rb
answersengine-0.4.1 lib/answersengine/client/base.rb
answersengine-0.4.0 lib/answersengine/client/base.rb
answersengine-0.3.3 lib/answersengine/client/base.rb
answersengine-0.3.2 lib/answersengine/client/base.rb
answersengine-0.3.1 lib/answersengine/client/base.rb
answersengine-0.3.0 lib/answersengine/client/base.rb