Skip to content

Commit

Permalink
Parser
Browse files Browse the repository at this point in the history
- fixed parsing of links if there is only one resource present in the collection
- added to_s method to entity
- improved rendering of source in json representation of link
  • Loading branch information
ffeldhaus committed Jan 3, 2013
1 parent ce4ac07 commit be2cdc7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
4 changes: 4 additions & 0 deletions lib/occi/core/entity.rb
Expand Up @@ -190,6 +190,10 @@ def to_header
header
end

def to_s
self.location
end

def inspect
JSON.pretty_generate(JSON.parse(to_json))
end
Expand Down
2 changes: 1 addition & 1 deletion lib/occi/core/link.rb
Expand Up @@ -77,7 +77,7 @@ def check(model)
def as_json(options={ })
link = super
link.rel = @rel if @rel
link.source = self.source.to_s if self.source.kind_of? String if self.source
link.source = self.source.to_s if self.source.to_s
link.target = self.target.to_s if self.target
link
end
Expand Down
21 changes: 19 additions & 2 deletions lib/occi/parser.rb
Expand Up @@ -190,11 +190,28 @@ def self.text_entity(text, entity_type)
# @return [OCCI::Collection]
def self.json(json)
collection = OCCI::Collection.new
hash = Hashie::Mash.new(JSON.parse(json))
hash = Hashie::Mash.new(JSON.parse(json))
collection.kinds.concat hash.kinds.collect { |kind| OCCI::Core::Kind.new(kind.scheme, kind.term, kind.title, kind.attributes, kind.related, kind.actions) } if hash.kinds
collection.mixins.concat hash.mixins.collect { |mixin| OCCI::Core::Mixin.new(mixin.scheme, mixin.term, mixin.title, mixin.attributes, mixin.related, mixin.actions) } if hash.mixins
collection.actions.concat hash.actions.collect { |action| OCCI::Core::Action.new(action.scheme, action.term, action.title, action.attributes) } if hash.actions
collection.resources.concat hash.resources.collect { |resource| OCCI::Core::Resource.new(resource.kind, resource.mixins, resource.attributes, resource.actions, resource.links) } if hash.resources
collection.links.concat hash.links.collect { |link| OCCI::Core::Link.new(link.kind, link.mixins, link.attributes) } if hash.links
collection.links.concat hash.links.collect { |link| OCCI::Core::Link.new(link.kind, link.mixins, link.attributes, [], nil, link.target) } if hash.links

if collection.resources.size == 1 && collection.links.size > 0
if collection.resources.first.links.empty?
collection.links.each { |link| link.source = collection.resources.first }
collection.resources.first.links = collection.links
end
end

# TODO: replace the following mechanism with one in the Links class
# replace link locations with link objects in all resources
collection.resources.each do |resource|
resource.links.collect! do |resource_link|
lnk = collection.links.select { |link| resource_link == link.to_s }.first
lnk ||= resource_link
end
end
collection
end

Expand Down

0 comments on commit be2cdc7

Please sign in to comment.