Class: WpApiClient::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/wp_api_client/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resources, headers = nil) ⇒ Collection

Returns a new instance of Collection.



7
8
9
10
11
12
13
14
# File 'lib/wp_api_client/collection.rb', line 7

def initialize(resources, headers = nil)
  resources = [resources] unless resources.is_a? Array
  @resources = resources.map { |object| WpApiClient::Entities::Base.build(object) }
  if headers
    @links = parse_link_header(headers['Link'])
    @total_available = headers['X-WP-TOTAL'].to_i
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



28
29
30
# File 'lib/wp_api_client/collection.rb', line 28

def method_missing(method, *args)
  @resources.send(method, *args)
end

Instance Attribute Details

#resourcesObject

Returns the value of attribute resources.



5
6
7
# File 'lib/wp_api_client/collection.rb', line 5

def resources
  @resources
end

#total_availableObject

Returns the value of attribute total_available.



5
6
7
# File 'lib/wp_api_client/collection.rb', line 5

def total_available
  @total_available
end

Instance Method Details

#each(&block) ⇒ Object



16
17
18
# File 'lib/wp_api_client/collection.rb', line 16

def each(&block)
  @resources.each{|p| block.call(p)}
end

#next_pageObject



20
21
22
# File 'lib/wp_api_client/collection.rb', line 20

def next_page
  @links[:next] && @links[:next]
end

#previous_pageObject



24
25
26
# File 'lib/wp_api_client/collection.rb', line 24

def previous_page
  @links[:prev] && @links[:prev]
end