Class: Enginn::ResourceIndex Abstract

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/enginn/resource_index.rb

Overview

This class is abstract.

Override the ResourceIndex.resource method to implement.

A ResourceIndex is a collection of fetchable Resource.

It can be filtered and paginated through the chainable methods #per, #page, and #where. It also includes the Enumerable module so methods such as #each, #map or #to_a are available.

Actual API requests are only issued when the #each method (or any method from Enumerable) is called. While #each-ing, new API request will be issued when the end of a page is reached. Note that when using #page, only the given page is reached for. One can also force fetching manually using #fetch.

Examples:

takes = project.takes.where(synthesis_text_cont: 'hello')
takes.map(&:character_name)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project) ⇒ ResourceIndex

Returns a new instance of ResourceIndex.

Parameters:



42
43
44
45
46
47
# File 'lib/enginn/resource_index.rb', line 42

def initialize(project)
  @project = project
  @filters = {}
  @pagination = { current: 1 }
  @errors = []
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



38
39
40
# File 'lib/enginn/resource_index.rb', line 38

def errors
  @errors
end

#filtersObject

Returns the value of attribute filters.



39
40
41
# File 'lib/enginn/resource_index.rb', line 39

def filters
  @filters
end

#paginationObject

Returns the value of attribute pagination.



39
40
41
# File 'lib/enginn/resource_index.rb', line 39

def pagination
  @pagination
end

#projectObject (readonly)

Returns the value of attribute project.



38
39
40
# File 'lib/enginn/resource_index.rb', line 38

def project
  @project
end

Class Method Details

.pathString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (String)


32
33
34
# File 'lib/enginn/resource_index.rb', line 32

def self.path
  resource.path
end

.resourceEnginn::Resource

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Define the type of Enginn::Resource to use with this kind of Enginn::ResourceIndex.

Returns:



26
27
28
# File 'lib/enginn/resource_index.rb', line 26

def self.resource
  raise "resource is not overriden for #{self}"
end

Instance Method Details

#each {|item| ... } ⇒ self

Yield Parameters:

Returns:

  • (self)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/enginn/resource_index.rb', line 51

def each(&block)
  fetch!
  @collection.each(&block)
  return self if @pagination[:locked]

  while @pagination[:current] < @pagination[:last]
    pagination[:current] += 1
    fetch!
    @collection.each(&block)
  end

  @pagination = { current: 1 }
  self
end

#fetchBoolean

Same as #fetch! but return false instead of raising an exception. Also fill in #errors with the server response.

Returns:

  • (Boolean)

See Also:



107
108
109
110
111
112
113
# File 'lib/enginn/resource_index.rb', line 107

def fetch
  fetch!
  true
rescue Faraday::Error => e
  @errors << e.response
  false
end

#fetch!true

Fetch the current page from the API. Resulting items of the collection are wrapped in the corresponding Enginn::Resource subclass.

Returns:

  • (true)

    if the request has been successfull



94
95
96
97
98
99
100
101
# File 'lib/enginn/resource_index.rb', line 94

def fetch!
  response = request
  @pagination = response[:pagination]
  @collection = response[:result].map do |attributes|
    self.class.resource.new(@project, attributes)
  end
  true
end

#inspectString

Returns:

  • (String)


116
117
118
119
120
121
# File 'lib/enginn/resource_index.rb', line 116

def inspect
  attributes = instance_variables.map do |var|
    "#{var}=#{instance_variable_get(var)}"
  end
  "#<#{self.class} #{attributes.join(', ')}>"
end

#page(page) ⇒ Enginn::ResourceIndex

Returns A new index with updated pagination.

Parameters:

  • page (Integer)

    The page number

Returns:



68
69
70
71
72
# File 'lib/enginn/resource_index.rb', line 68

def page(page)
  new_index = clone
  new_index.pagination = @pagination.merge(current: page, locked: true)
  new_index
end

#per(per) ⇒ Enginn::ResourceIndex

Returns A new index with updated pagination.

Parameters:

  • per (Integer)

    The number of items per page

Returns:



76
77
78
79
80
# File 'lib/enginn/resource_index.rb', line 76

def per(per)
  new_index = clone
  new_index.pagination = @pagination.merge(per: per)
  new_index
end

#routeString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (String)


125
126
127
# File 'lib/enginn/resource_index.rb', line 125

def route
  "#{@project.route}/#{self.class.path}"
end

#where(filters) ⇒ Enginn::ResourceIndex

Returns A new filtered index.

Parameters:

  • filters (Hash)

    Filters as you would use them in the ‘q` object with the API.

Returns:



84
85
86
87
88
# File 'lib/enginn/resource_index.rb', line 84

def where(filters)
  new_index = clone
  new_index.filters = @filters.merge(filters || {})
  new_index
end