Sha256: 4d53116eb3ecd3e2e142de005e1e5c633b4ac95668ecc4556ab5a335efa06c0d

Contents?: true

Size: 970 Bytes

Versions: 12

Compression:

Stored size: 970 Bytes

Contents

module Unsplash # :nodoc:

  # Decorates Array of klass-type objects with total and total_pages attributes
  class SearchResult < SimpleDelegator
    attr_reader :total, :total_pages

    def initialize(decorated, klass)
      @total = decorated["total"]
      @total_pages = decorated["total_pages"]

      list = decorated["results"].map do |content|
        klass.new content.to_hash
      end

      super(list)
    end
  end

  # Unsplash Search operations
  class Search < Client

    class << self
      # Helper class to facilitate search on multiple classes
      # @param url [String] Url to be searched into
      # @param klass [Class] Class to instantiate the contents with
      # @param params [Hash] Params for the search
      # @return [SearchResult] Decorated Array of klass-type objects
      def search(url, klass, params)
        list = JSON.parse(connection.get(url, params).body)
        SearchResult.new(list, klass)
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
unsplash-3.1.1 lib/unsplash/search.rb
unsplash-3.0.0 lib/unsplash/search.rb
unsplash-2.1.0 lib/unsplash/search.rb
unsplash-2.0.1 lib/unsplash/search.rb
unsplash-2.0.0 lib/unsplash/search.rb
unsplash-1.5.6 lib/unsplash/search.rb
unsplash_mango-1.0.0 lib/unsplash/search.rb
unsplash-1.5.5 lib/unsplash/search.rb
unsplash-1.5.4 lib/unsplash/search.rb
unsplash-1.5.3 lib/unsplash/search.rb
unsplash-1.5.2 lib/unsplash/search.rb
unsplash-1.5.1 lib/unsplash/search.rb