Class: Vend::ResourceCollection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/vend/resource_collection.rb

Overview

This is an enumerable class which allows iteration over a collection of resources. This class will automatically fetch paginated results if the target_class supports it.

Defined Under Namespace

Classes: AlreadyScopedError, PageOutOfBoundsError, ScopeNotFoundError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, target_class, endpoint, request_args = {}) ⇒ ResourceCollection

Returns a new instance of ResourceCollection.



19
20
21
22
23
24
# File 'lib/vend/resource_collection.rb', line 19

def initialize(client, target_class, endpoint, request_args = {})
  @client       = client
  @target_class = target_class
  @endpoint     = endpoint
  @request_args = target_class.default_collection_request_args.merge(request_args)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



69
70
71
72
73
74
75
# File 'lib/vend/resource_collection.rb', line 69

def method_missing(method_name, *args, &block)
  if accepts_scope?(method_name)
    scope(method_name, *args)
  else
    super
  end
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



14
15
16
# File 'lib/vend/resource_collection.rb', line 14

def client
  @client
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



14
15
16
# File 'lib/vend/resource_collection.rb', line 14

def endpoint
  @endpoint
end

#request_argsObject (readonly)

Returns the value of attribute request_args.



14
15
16
# File 'lib/vend/resource_collection.rb', line 14

def request_args
  @request_args
end

#target_classObject (readonly)

Returns the value of attribute target_class.



14
15
16
# File 'lib/vend/resource_collection.rb', line 14

def target_class
  @target_class
end

Instance Method Details

#eachObject



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/vend/resource_collection.rb', line 26

def each
  # If each has previously been invoked on this collection, the response
  # member will already be set, causing last_page? to immeadiatly return
  # true.  So reset it here.
  self.response = nil

  until last_page?
    target_class.build_from_json(client, get_next_page).map do |resource|
      yield resource
    end
  end
  self
end

#endpoint_with_scopesObject



109
110
111
# File 'lib/vend/resource_collection.rb', line 109

def endpoint_with_scopes
  endpoint + scopes.join
end

#get_or_create_page_scopeObject



99
100
101
102
# File 'lib/vend/resource_collection.rb', line 99

def get_or_create_page_scope
  scope(:page, page) unless has_scope? :page
  get_scope :page
end

#get_scope(name) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/vend/resource_collection.rb', line 89

def get_scope(name)
  result = scopes.find { |scope| scope.name == name }
  if result.nil?
    raise ScopeNotFoundError.new(
      "Scope: #{name} was not found in #{scopes}."
    )
  end
  result
end

#has_scope?(name) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/vend/resource_collection.rb', line 65

def has_scope?(name)
  scopes.any? { |s| s.name == name }
end

#increment_pageObject



82
83
84
85
86
87
# File 'lib/vend/resource_collection.rb', line 82

def increment_page
  if paged?
    page_scope = get_or_create_page_scope
    page_scope.value = page_scope.value + 1
  end
end

#last_page?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/vend/resource_collection.rb', line 44

def last_page?
  pagination && pagination.last_page?
end

#paged?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/vend/resource_collection.rb', line 48

def paged?
  pagination && pagination.paged?
end

#paginationObject



40
41
42
# File 'lib/vend/resource_collection.rb', line 40

def pagination
  PaginationInfo.new(response) if response.instance_of? Hash
end

#respond_to?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
# File 'lib/vend/resource_collection.rb', line 77

def respond_to?(method_name)
  return true if accepts_scope?(method_name)
  super
end

#scope(name, value) ⇒ Object

Adds a new URL scope parameter to this ResourceCollection. Calling scope(:foo, ‘bar’) will effectively append ‘/foo/bar’ to the resource URL.

Raises:



59
60
61
62
63
# File 'lib/vend/resource_collection.rb', line 59

def scope(name, value)
  raise AlreadyScopedError if has_scope?(name)
  scopes << Scope.new(name, value)
  self
end

#scopesObject



52
53
54
# File 'lib/vend/resource_collection.rb', line 52

def scopes
  @scopes ||= []
end

#urlObject



104
105
106
107
# File 'lib/vend/resource_collection.rb', line 104

def url
  increment_page
  endpoint_with_scopes
end