Module: GovDelivery::TMS::CollectionResource::InstanceMethods

Includes:
Base
Defined in:
lib/govdelivery/tms/collection_resource.rb

Instance Attribute Summary collapse

Attributes included from Base

#client, #errors, #href, #new_record

Instance Method Summary collapse

Methods included from Base

included

Instance Attribute Details

#collectionObject

Returns the value of attribute collection.



8
9
10
# File 'lib/govdelivery/tms/collection_resource.rb', line 8

def collection
  @collection
end

Instance Method Details

#build(attributes = nil) ⇒ Object



30
31
32
33
34
35
# File 'lib/govdelivery/tms/collection_resource.rb', line 30

def build(attributes = nil)
  thing = instance_class(self.class).new(client, href, attributes || {})
  thing.new_record = true
  collection << thing
  thing
end

#getObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/govdelivery/tms/collection_resource.rb', line 19

def get
  response = client.get(href)
  initialize_collection_from_items(response.body)
  # setup page links from header
  links = LinkHeader.parse(response.headers['link']).to_a.collect do |a|
    { a[1][0].last => a[0] }
  end
  parse_links(links)
  self
end

#initialize(client, href, items = nil) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/govdelivery/tms/collection_resource.rb', line 10

def initialize(client, href, items = nil)
  super(client, href)
  if items
    initialize_collection_from_items(items)
  else
    self.collection = []
  end
end

#to_jsonObject



37
38
39
# File 'lib/govdelivery/tms/collection_resource.rb', line 37

def to_json
  @collection.map(&:to_json)
end

#to_sObject



41
42
43
# File 'lib/govdelivery/tms/collection_resource.rb', line 41

def to_s
  "<#{self.class.inspect} href=#{href} collection=#{collection.inspect}>"
end