Class: AbstractItem

Inherits:
Podio::Item
  • Object
show all
Defined in:
app/models/abstract_item.rb

Constant Summary collapse

IS_LIKED =

COMMENT SECTION

CONSTANTS

:is_liked
CREATED_BY =
:created_by
CREATED_BY_TYPE =
:type
CREATED_BY_AVATAR =
:avatar
CREATED_BY_USER_ID =
:user_id
CREATED_BY_IMAGE =
:image
CREATED_BY_LAST_SEEN_ON =
:last_seen_on
CREATED_BY_AVATAR_TYPE =
:avatar_type
CREATED_BY_NAME =
:name
CREATED_BY_ID =
:id
CREATED_BY_AVATAR_ID =
:avatar_id
CREATED_BY_URL =
:url
VALUE =
:value
CREATED_ON =
:created_on
FILES =
:files
COMMENT_ID =
:comment_id
RICH_VALUE =
:rich_value
LIKE_COUNT =
:like_count
@@abbrErr =

To change this template use File | Settings | File Templates.

"PAI"

Class Method Summary collapse

Class Method Details

.range(app_id, external_ids = [{}], attrs = {:order => "ASC", :field => "item_id", :limit => -1, :offset => -1}, filter_by = [{}]) ⇒ Object

Find all items in the app



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/models/abstract_item.rb', line 7

def self.range(app_id, external_ids = [{}], attrs = {:order => "ASC", :field => "item_id", :limit => -1, :offset => -1}, filter_by = [{}])
  #begin
  #collection = self.find_by_filter_values(app_id, {}, { :limit => 1, :offset => 1 })
  attrs[:order] = attrs[:order].nil? ? "ASC" : attrs[:order]
  attrs[:field] = attrs[:field].nil? ? "item_id" : attrs[:field]
  attrs[:limit] = (attrs[:limit].nil? || attrs[:limit] == -1) ? 30 : attrs[:limit]
  attrs[:offset] = (attrs[:offset].nil? || attrs[:offset] == -1) ? 0 : attrs[:offset]

  # Check the app if it exists?
  if (AbstractApplication.app_exists?(app_id) == false)
    raise "AbstractItem - #{@@abbrErr}001 The app doesn't exist or service account doesn't have access to it"
  end

  # Start getting data
  collection = nil

  begin
    collection = self.find_by_filter_values(app_id, {}, {:limit => attrs[:limit], :offset => attrs[:offset]})
  rescue
    raise "AbstractItem - #{@@abbrErr}002 The access to the app was restricted"
  end

  data = collection[:all]

  tArray = Array.new
  tAttributes = Array.new
  # Flag for getting all existent attributes
  tAttributesFlag = true

  data.each do |item|
    tHash = Hash.new

    # Populate items into a customized array of items
    tHash['item_id'] = item.attributes[:item_id]
    external_ids.each do |id|
      if (id[:simple])
        value = field_values_by_external_id(item, id[:external_id], {:simple => true})

        if !value.nil?
          tHash[id[:external_id]] = value
          if (tAttributesFlag)
            tAttributes << id[:external_id]
          end
        end
      else
        value = field_values_by_external_id(item, id[:external_id], {})

        if !value.nil?
          tHash[id[:external_id]] = value
          if (tAttributesFlag)
            tAttributes << id[:external_id]
          end
        end
      end
    end

    # Set flag to false to prevent recursively adding fields
    tAttributesFlag = false
    tArray << tHash

  end

  #raise "#{tAttributes.count.to_s}"

  # Change and specify datatypes if needed
  tArray.each do |item|
    tAttributes.each do |attr|
      item[attr] = Float(item[attr]) rescue item[attr].to_s
    end
  end

  # Sort items in the customized array of items
  if (attrs[:order] == "ASC")
    tArray.sort! { |a, b| a[attrs[:field]] <=> b[attrs[:field]] }
  elsif (attrs[:order] == "DESC")
    tArray.sort! { |a, b| a[attrs[:field]] <=> b[attrs[:field]] }
    tArray.reverse!
  end

  tArray
  #rescue Exception => ex
  #raise 'Access denied, please check your podio access permission!'
  #end
end