Class: CollectionJSON::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/collection-json/collection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(href) ⇒ Collection

Returns a new instance of Collection.



22
23
24
25
26
27
28
29
30
# File 'lib/collection-json/collection.rb', line 22

def initialize(href)
  @href = CollectionJSON.add_host(href)
  @version = COLLECTION_JSON_VERSION
  @items = []
  @links = []
  @queries = []
  @error = nil
  @template = nil
end

Instance Attribute Details

#errorObject

Returns the value of attribute error.



9
10
11
# File 'lib/collection-json/collection.rb', line 9

def error
  @error
end

#hrefObject (readonly)

Returns the value of attribute href.



9
10
11
# File 'lib/collection-json/collection.rb', line 9

def href
  @href
end

#itemsObject

Returns the value of attribute items.



9
10
11
# File 'lib/collection-json/collection.rb', line 9

def items
  @items
end

Returns the value of attribute links.



9
10
11
# File 'lib/collection-json/collection.rb', line 9

def links
  @links
end

#queriesObject

Returns the value of attribute queries.



9
10
11
# File 'lib/collection-json/collection.rb', line 9

def queries
  @queries
end

#templateObject

Returns the value of attribute template.



9
10
11
# File 'lib/collection-json/collection.rb', line 9

def template
  @template
end

#versionObject

Returns the value of attribute version.



9
10
11
# File 'lib/collection-json/collection.rb', line 9

def version
  @version
end

Class Method Details

.from_hash(hash) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/collection-json/collection.rb', line 12

def self.from_hash(hash)
  self.new(hash[ROOT_NODE]['href']).tap do |collection|
    %w{items links queries error template}.each do |attribute|
      if hash[ROOT_NODE][attribute]
        collection.send("#{attribute}=", hash[ROOT_NODE][attribute])
      end
    end
  end
end

Instance Method Details

#collectionObject



52
53
54
# File 'lib/collection-json/collection.rb', line 52

def collection
  {ROOT_NODE => body}
end

#to_json(*args) ⇒ Object



56
57
58
# File 'lib/collection-json/collection.rb', line 56

def to_json(*args)
  collection.to_json(args)
end