Sha256: fe77d68137fd491b4184eeb9bdebb97471ef040fd09e0a833474e09fb3f58d4f

Contents?: true

Size: 1.49 KB

Versions: 8

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

module Desertcart
  class Searcher < LedgerSync::Ledgers::Searcher
    def resources
      case response.status
      when 200
        response.body
                .fetch(ledger_resource_type.pluralize)
                .map { |o| deserialized_resource(hash: o) }
      else
        []
      end
    end

    def next?
      case response.status
      when 200
        response.body.dig('meta', 'pagination', 'next_page')
      else
        false
      end
    end

    def deserialized_resource(hash:)
      searcher_deserializer.deserialize(
        hash: hash,
        resource: resource_class.new
      )
    end

    def response
      @response ||= client.read(
        path: ledger_resources_path,
        params: query
      )
    end

    def resource_class
      @resource_class ||= self.class.inferred_resource_class
    end

    def searcher_deserializer_class
      @searcher_deserializer_class ||= self.class.inferred_serialization_class(
        type: 'SearcherDeserializer'
      )
    end

    def searcher_deserializer
      searcher_deserializer_class.new
    end

    def ledger_namespace_path
      @ledger_namespace_path ||= nil
    end

    def ledger_resources_path
      @ledger_resources_path ||= [
        ledger_namespace_path,
        ledger_resource_type.pluralize
      ].compact.join('/')
    end

    def ledger_resource_type
      @ledger_resource_type ||= client.class.ledger_resource_type_for(
        resource_class: resource_class
      )
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
desertcart-1.2.0 lib/desertcart/searcher.rb
desertcart-1.1.5 lib/desertcart/searcher.rb
desertcart-1.1.4 lib/desertcart/searcher.rb
desertcart-1.1.3 lib/desertcart/searcher.rb
desertcart-1.1.2 lib/desertcart/searcher.rb
desertcart-1.1.1 lib/desertcart/searcher.rb
desertcart-1.1.0 lib/desertcart/searcher.rb
desertcart-1.0.0 lib/desertcart/searcher.rb