lib/siren_client/entity.rb in siren_client-0.2.0 vs lib/siren_client/entity.rb in siren_client-0.2.1

- old
+ new

@@ -1,15 +1,15 @@ module SirenClient class Entity include Enumerable attr_accessor :href - attr_reader :payload, :classes, :properties, :entities, :rels, + attr_reader :payload, :classes, :properties, :entities, :rels, :links, :actions, :title, :type, :config def initialize(data, config={}) - @config = { format: :json }.merge config + @config = { format: :json }.merge config if data.class == String unless data.class == String && data.length > 0 raise InvalidURIError, 'An invalid url was passed to SirenClient::Entity.new.' end begin @@ -40,12 +40,12 @@ def search(criteria) return false unless criteria if criteria.is_a? String return entities.select do |ent| - true if ent.classes.include?(criteria) || - ent.rels.include?(criteria) || + true if ent.classes.include?(criteria) || + ent.rels.include?(criteria) || ent.href == criteria end elsif criteria.is_a? Regexp return entities.select do |ent| true if ent.classes.any? { |klass| criteria.match(klass) } || @@ -58,31 +58,31 @@ ### Entity sub-links only def go return if self.href.empty? self.class.new(self.href, @config) end - + def method_missing(method, *args) method_str = method.to_s return @entities.length if method_str == 'length' # Does it match a property, if so return the property value. @properties.each do |key, prop| return prop if method_str == key end - # Does it match an entity sub-link's class? + # Does it match an entity sub-link's class? @entities.each do |ent| - return ent.go if ent.href && + return ent.go if ent.href && (ent.classes.map { |c| c.underscore }).include?(method_str.underscore) end # Does it match a link, if so traverse it and return the entity. @links.each do |key, link| return link.go if method_str == key.underscore end # Does it match an action, if so return the action. @actions.each do |key, action| return action if method_str == key.underscore end - raise NoMethodError, 'The method does not match a property, action, or link on SirenClient::Entity.' + raise NoMethodError, "The method \"#{method_str}\" does not match a property, action, or link on SirenClient::Entity." end private def parse_data