Sha256: f145b6431b8765dae78a3a25f4d9bf1d5b7fe369224e06aec53e6fd947664470

Contents?: true

Size: 1022 Bytes

Versions: 2

Compression:

Stored size: 1022 Bytes

Contents

module Deviant
  class Client
    def initialize(options)
      @options = options
    end

    def store(name, message, data = {})
      entry = {name: name, message: message, date: Time.now}.merge(data)
      return store_async(entry) if @options[:sidekiq][:enabled]

      index { store(entry) }
    end

    def fetch(query, tags = [])
      query = build_query(query)
      search do
        query { string query }
        filter :terms, tags: tags if tags.size > 0
        sort { by :date, 'desc' }
      end
    end

    def index(&block)
      Tire.index @options[:name], &block
    end

    def search(&block)
      Tire.search @options[:name], &block
    end

    private

    def store_async(entry)
      DeviantWorker.perform_async(entry)
    end

    def build_query(query)
      if query.is_a?(Hash)
        return query.to_a.map { |item|
          "#{item[0]}:#{item[1]}"
        }.join(' AND ')
      elsif query.is_a?(Array)
        query.join(' AND ')
      else
        query
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
deviant-0.0.2 lib/deviant/client.rb
deviant-0.0.1 lib/deviant/client.rb