Class: DevinApi::Collection
- Inherits:
-
Object
- Object
- DevinApi::Collection
- Includes:
- Enumerable
- Defined in:
- lib/devin_api/collection.rb
Overview
Collection class for handling resource lists and pagination
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#resource_class ⇒ Object
readonly
Returns the value of attribute resource_class.
-
#resources ⇒ Object
readonly
Returns the value of attribute resources.
Instance Method Summary collapse
- #create(attributes = {}) ⇒ Object
- #each ⇒ Object
- #fetch ⇒ Object
- #find(options = {}) ⇒ Object
-
#initialize(client, resource_class, options = {}) ⇒ Collection
constructor
A new instance of Collection.
- #next_page ⇒ Object
- #path ⇒ Object
Constructor Details
#initialize(client, resource_class, options = {}) ⇒ Collection
Returns a new instance of Collection.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/devin_api/collection.rb', line 10 def initialize(client, resource_class, = {}) @client = client @resource_class = resource_class @options = # Process array options @options.each do |key, value| @options[key] = value.join(',') if value.is_a?(Array) end end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
8 9 10 |
# File 'lib/devin_api/collection.rb', line 8 def client @client end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/devin_api/collection.rb', line 8 def @options end |
#resource_class ⇒ Object (readonly)
Returns the value of attribute resource_class.
8 9 10 |
# File 'lib/devin_api/collection.rb', line 8 def resource_class @resource_class end |
#resources ⇒ Object (readonly)
Returns the value of attribute resources.
8 9 10 |
# File 'lib/devin_api/collection.rb', line 8 def resources @resources end |
Instance Method Details
#create(attributes = {}) ⇒ Object
62 63 64 65 |
# File 'lib/devin_api/collection.rb', line 62 def create(attributes = {}) response = client.post(path, attributes) resource_class.new(client, response) end |
#each ⇒ Object
21 22 23 24 |
# File 'lib/devin_api/collection.rb', line 21 def each(&) fetch if @resources.nil? @resources.each(&) end |
#fetch ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/devin_api/collection.rb', line 26 def fetch response = client.get(path, @options) resource_key = "#{resource_class.resource_name.downcase}s" @resources = if response[resource_key] response[resource_key].map do |attrs| resource_class.new(client, attrs) end else [] end @resources end |
#find(options = {}) ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/devin_api/collection.rb', line 41 def find( = {}) if [:id] response = client.get("#{path}/#{[:id]}") resource_class.new(client, response) else self end end |
#next_page ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/devin_api/collection.rb', line 50 def next_page raise DevinApi::Error::PaginationNotSupported, "Pagination is not supported for #{resource_class.name}" unless resource_class.pagination_supported? = @options.dup [:offset] = calculate_next_offset self.class.new(@client, @resource_class, ) end |
#path ⇒ Object
58 59 60 |
# File 'lib/devin_api/collection.rb', line 58 def path resource_class.resource_path end |