Sha256: 46a4da744b39d2568aaf000479c244a24c8bd2ff0756b60f303994301c6d913b

Contents?: true

Size: 1.7 KB

Versions: 6

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

require 'uri'

module Billomat
  # This class provides the possibility to query the resources.
  class Search
    # Creates a new search object.
    #
    # @param [Class] resource The resource class to be queried
    # @param [Hash] hash The query
    def initialize(resource, hash)
      @resource = resource
      @hash     = hash
    end

    # @return [String] the path including the query
    def path
      "#{@resource.base_path}?#{hash_to_query}"
    end

    # Runs the query and calls the gateway.
    # Currently it will always return an empty array when no query is provided.
    #
    # @return [Array<Billomat::Model::Base>]
    def run
      return [] if @hash.compact.empty?

      to_array(Billomat::Gateway.new(:get, path).run)
    end

    # Corrects the response to always return an array.
    #
    # @todo Due to a strange API behaviour we have to fix the response here.
    #   This may be fixed in a new API version.
    #
    # @param [Hash] resp The response from the gateway
    # @return [Array<Billomat::Model::Base>]
    def to_array(resp)
      data = resp["#{name}s"][name]
      case data
      when Hash
        [@resource.new(data)]
      when Array
        data.map { |d| @resource.new(d) }
      else
        []
      end
    end

    # @return [String] the name of the resource
    def name
      @resource.resource_name
    end

    # @param [Hash] resp The response from the gateway
    # @return [Integer] the number of records found
    def count(resp)
      return 0 if resp.nil?

      resp["#{name}s"]['@total'].to_i
    end

    private

    # @return [String] the query as www encoded string
    def hash_to_query
      URI.encode_www_form(@hash)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
billomat-1.2.5 lib/billomat/search.rb
billomat-1.2.4 lib/billomat/search.rb
billomat-1.2.3 lib/billomat/search.rb
billomat-1.2.2 lib/billomat/search.rb
billomat-1.2.1 lib/billomat/search.rb
billomat-1.2.0 lib/billomat/search.rb