Sha256: 3c5ff9f79c69a87e4b490ae8a25d7462db9afd76677941b8df0cf1bd89e44531

Contents?: true

Size: 1.65 KB

Versions: 2

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

module Europeana
  module Blacklight
    ##
    # Europeana API response for BL
    class Response < HashWithIndifferentAccess
      require 'europeana/blacklight/response/pagination'
      require 'europeana/blacklight/response/facets'
      require 'europeana/blacklight/response/more_like_this'

      include Pagination
      include Facets
      include MoreLikeThis

      attr_reader :request_params
      attr_accessor :document_model, :blacklight_config

      def initialize(data, request_params, options = {})
        super(data)
        @request_params = request_params
        self.document_model = options[:document_model] || Document
        self.blacklight_config = options[:blacklight_config]
      end

      def update(other_hash)
        other_hash.each_pair { |key, value| self[key] = value }
        self
      end

      def params
        self['params'] ? self['params'] : request_params
      end

      def rows
        params[:rows].to_i
      end

      def sort
        params[:sort]
      end

      def documents
        @documents ||= (key?('object') ? [self['object']] : (self['items'] || [])).map do |doc|
          document_model.new(doc, self)
        end
      end
      alias_method :docs, :documents

      def grouped
        []
      end

      def group(_key)
        nil
      end

      def grouped?
        false
      end

      def export_formats
        documents.map { |x| x.export_formats.keys }.flatten.uniq
      end

      def total
        self[:totalResults].to_s.to_i
      end

      def start
        params[:start].to_s.to_i - 1
      end

      def empty?
        total == 0
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
europeana-blacklight-1.3.1 lib/europeana/blacklight/response.rb
europeana-blacklight-1.3.0 lib/europeana/blacklight/response.rb