Class: The86::Client::ResourceCollection
- Inherits:
-
Object
- Object
- The86::Client::ResourceCollection
- Includes:
- Enumerable
- Defined in:
- lib/the86-client/resource_collection.rb
Instance Attribute Summary collapse
-
#parameters ⇒ Object
Returns the value of attribute parameters.
Instance Method Summary collapse
- #<<(record) ⇒ Object
- #[](index) ⇒ Object
- #build(attributes) ⇒ Object
- #create(attributes) ⇒ Object
- #each ⇒ Object
-
#find(id, attribute = :id) ⇒ Object
Find and load a resource.
-
#initialize(connection, path, klass, parent, records = nil) ⇒ ResourceCollection
constructor
Connection is a The86::Client::Connection instance.
-
#more ⇒ Object
Load the next page of records, based on the pagination header, e.g.
-
#more? ⇒ Boolean
Whether there are more resources on a subsequent page.
-
#to_a ⇒ Object
Cache array representation.
- #with_parameters(parameters) ⇒ Object
Constructor Details
#initialize(connection, path, klass, parent, records = nil) ⇒ ResourceCollection
Connection is a The86::Client::Connection instance. Path is the API-relative path, e.g. “users”. Klass is class of each record in the collection, e.g. User Parent is the parent resource of this collection and its items. Records is an array of hashes, for pre-populating the collection.
e.g. when an API response contains collections of child resources.
14 15 16 17 18 19 20 |
# File 'lib/the86-client/resource_collection.rb', line 14 def initialize(connection, path, klass, parent, records = nil) @connection = connection @path = path @klass = klass @parent = parent @records = records end |
Instance Attribute Details
#parameters ⇒ Object
Returns the value of attribute parameters.
30 31 32 |
# File 'lib/the86-client/resource_collection.rb', line 30 def parameters @parameters end |
Instance Method Details
#<<(record) ⇒ Object
96 97 98 |
# File 'lib/the86-client/resource_collection.rb', line 96 def << record (@records || []) << record end |
#[](index) ⇒ Object
92 93 94 |
# File 'lib/the86-client/resource_collection.rb', line 92 def [](index) to_a[index] end |
#build(attributes) ⇒ Object
22 23 24 |
# File 'lib/the86-client/resource_collection.rb', line 22 def build(attributes) @klass.new(attributes.merge(parent: @parent)) end |
#create(attributes) ⇒ Object
26 27 28 |
# File 'lib/the86-client/resource_collection.rb', line 26 def create(attributes) build(attributes).tap(&:save) end |
#each ⇒ Object
56 57 58 59 60 |
# File 'lib/the86-client/resource_collection.rb', line 56 def each records.each do |attributes| yield build(attributes) end end |
#find(id, attribute = :id) ⇒ Object
Find and load a resource.
If Resource#url_id is overridden, specify the attribute name. TODO: the resource should know its URL attribute name.
Note that this currently triggers an HTTP GET, then a POST:
conversation.find(10).posts.create(attributes)
As an alternative, this only triggers the HTTP POST:
conversation.build(id: 10).posts.create(attributes)
52 53 54 |
# File 'lib/the86-client/resource_collection.rb', line 52 def find(id, attribute = :id) build(id: id).load end |
#more ⇒ Object
Load the next page of records, based on the pagination header, e.g. Link: <example.org/api/v1/groups/a/conversations?bumped_before=time>; rel=“next”
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/the86-client/resource_collection.rb', line 64 def more if more? url = Addressable::URI.parse(http_response.links[:next]) self.class.new( @connection, url.path, @klass, @parent ).tap do |collection| collection.parameters = url.query_values end else raise PaginationError, %{Collection has no 'Link: <url>; rel="next"' header} end end |
#more? ⇒ Boolean
Whether there are more resources on a subsequent page. See documentation for #more method.
82 83 84 |
# File 'lib/the86-client/resource_collection.rb', line 82 def more? http_response.links.key? :next end |
#to_a ⇒ Object
Cache array representation. Save building Resources for each record multiple times.
88 89 90 |
# File 'lib/the86-client/resource_collection.rb', line 88 def to_a @_to_a ||= super end |
#with_parameters(parameters) ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/the86-client/resource_collection.rb', line 32 def with_parameters(parameters) self.class.new( @connection, @path, @klass, @parent ).tap do |collection| collection.parameters = parameters end end |