Class: Fotolia::Categories

Inherits:
Object
  • Object
show all
Defined in:
lib/fotolia/categories.rb

Overview

Base class for ConceptualCategories and RepresentativeCategories.

Instance Method Summary collapse

Constructor Details

#initialize(fotolia_client) ⇒ Categories

Parameters

fotolia_client

A Fotolia::Base object



10
11
12
# File 'lib/fotolia/categories.rb', line 10

def initialize(fotolia_client)
  @fotolia = fotolia_client
end

Instance Method Details

#find(category = nil) ⇒ Object

Returns an array of Category objects. If no category is given, fetches the root level category. Otherwise the child categories of the given cat are returned.

Raises a RuntimeError if not called on a ConceptualCategories or RepresentativeCategories object, i. e. @method and @klass has to be set.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fotolia/categories.rb', line 23

def find(category = nil)
  raise 'You have to use ConceptualCategories or RepresentativeCategories!' unless(@method && @klass)

  k = if(category) then category.id else :root end

  if(@categories && @categories[k]) # check if categories have been loaded already
    @categories[k] # use cached categories
  else
    # get cats from fotolia
    res = if(category && (category.kind_of?(String) || category.kind_of?(Fixnum)))
      @fotolia.remote_call(@method, @fotolia.language.id, category.to_i)
    elsif(category)
      @fotolia.remote_call(@method, @fotolia.language.id, category.id.to_i)
    else
      @fotolia.remote_call(@method, @fotolia.language.id)
    end

    @categories = Hash.new unless(@categories)

    @categories[k] = res.collect{|c| @klass.new(@fotolia, {'key' => c.first, 'parent_category' => category}.merge(c.last))}
  end
end

#root_levelObject

Returns an array of all root level Category objects. See #find.



49
50
51
# File 'lib/fotolia/categories.rb', line 49

def root_level
  self.find
end