Class: SifiApi::ResourceCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/sifi_api/resource_collection.rb

Instance Method Summary collapse

Constructor Details

#initialize(resource_name, resources, paging, connection, user_key) ⇒ ResourceCollection

Returns a new instance of ResourceCollection.



3
4
5
6
7
8
9
# File 'lib/sifi_api/resource_collection.rb', line 3

def initialize(resource_name, resources, paging, connection, user_key)
  @resource_name = resource_name
  @resources = resources
  @paging = paging || {}
  @connection = connection
  @user_key = user_key
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



63
64
65
66
67
# File 'lib/sifi_api/resource_collection.rb', line 63

def method_missing(sym, *args, &block)
  if @resources.respond_to?(sym)
    @resources.send(sym, *args, &block)
  end
end

Instance Method Details

#current_pageObject



47
48
49
# File 'lib/sifi_api/resource_collection.rb', line 47

def current_page
  has_paging? ? @paging["page"] : nil
end

#has_next_page?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/sifi_api/resource_collection.rb', line 15

def has_next_page?
  @paging.has_key?("next")
end

#has_paging?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/sifi_api/resource_collection.rb', line 11

def has_paging?
  @paging.has_key?("page")
end

#has_previous_page?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/sifi_api/resource_collection.rb', line 19

def has_previous_page?
  @paging.has_key?("previous")
end

#next_pageObject



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/sifi_api/resource_collection.rb', line 23

def next_page
  if has_next_page?
    response = @connection.get(@user_key, @paging["next"])
    if response && response.body
      @resources = response.body[@resource_name].map do |record|
        SifiApi.const_get(@resource_name.classify).new(record, @connection, @user_key)
      end
      @paging = response.body["paging"]
    end
  end
end

#page_sizeObject



51
52
53
# File 'lib/sifi_api/resource_collection.rb', line 51

def page_size
  has_paging? ? @paging["size"] : nil
end

#previous_pageObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/sifi_api/resource_collection.rb', line 35

def previous_page
  if has_previous_page?
    response = @connection.get(@user_key, @paging["previous"])
    if response && response.body
      @resources = response.body[@resource_name].map do |record|
        SifiApi.const_get(@resource_name.classify).new(record, @connection, @user_key)
      end
      @paging = response.body["paging"]
    end
  end
end

#to_sObject



69
70
71
# File 'lib/sifi_api/resource_collection.rb', line 69

def to_s
  @resources.inspect
end

#total_pagesObject



55
56
57
# File 'lib/sifi_api/resource_collection.rb', line 55

def total_pages
  has_paging? ? (@paging["total"].to_f/@paging["size"]).ceil : nil
end

#total_recordsObject



59
60
61
# File 'lib/sifi_api/resource_collection.rb', line 59

def total_records
  has_paging? ? @paging["total"] : nil
end