Sha256: 4734c360d531d450737c5b9f1fac8d7d2024357bcd98076418969d7aea0e07a6

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

require 'httparty'

module AnswersEngine
  module Client
    class Base
      include HTTParty
      base_uri(ENV['ANSWERSENGINE_API_URL'].nil? ? 'https://fetch.answersengine.com/api/v1' : ENV['ANSWERSENGINE_API_URL'])

      def self.env_auth_token
        ENV['ANSWERSENGINE_TOKEN']
      end

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

      def auth_token= value
        @auth_token = value
      end

      def initialize(opts={})
        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

1 entries across 1 versions & 1 rubygems

Version Path
answersengine-0.2.33 lib/answersengine/client/base.rb