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

- old
+ new

@@ -1,39 +1,51 @@ module SirenClient class Entity include Enumerable + include Modules::WithRawResponse + attr_accessor :href attr_reader :payload, :classes, :properties, :entities, :rels, :links, :actions, :title, :type, :config def initialize(data, config={}) + super() @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.' + raise InvalidURIError, 'An invalid url was passed to SirenClient::Entity.new.' end begin SirenClient.logger.debug "GET #{data}" @payload = HTTParty.get(data, @config).parsed_response rescue URI::InvalidURIError => e raise InvalidURIError, e.message rescue JSON::ParserError => e raise InvalidResponseError, e.message end elsif data.class == Hash - @payload = data + @payload = data else - raise ArgumentError, "You must pass in either a url(String) or an entity(Hash) to SirenClient::Entity.new" + raise ArgumentError, "You must pass in either a url(String) or an entity(Hash) to SirenClient::Entity.new" end parse_data end # Execute an entity sub-link if called directly # otherwise just return the entity. def [](i) - @entities[i].href.empty? ? @entities[i] : @entities[i].go rescue nil + if @entities[i].href.empty? + @entities[i] + else + if next_response_is_raw? + disable_raw_response + @entities[i].with_raw_response.go + else + @entities[i].go + end + end end def each(&block) @entities.each(&block) rescue nil end @@ -56,11 +68,16 @@ end ### Entity sub-links only def go return if self.href.empty? - self.class.new(self.href, @config) + if next_response_is_raw? + disable_raw_response + generate_raw_response(self.href, @config) + else + self.class.new(self.href, @config) + end end def method_missing(method, *args) method_str = method.to_s return @entities.length if method_str == 'length' @@ -68,24 +85,41 @@ @properties.each do |key, prop| return prop if method_str == key end # Does it match an entity sub-link's class? @entities.each do |ent| - return ent.go if ent.href && - (ent.classes.map { |c| c.underscore }).include?(method_str.underscore) + if ent.href && (ent.classes.map { |c| underscore_name c }).include?(underscore_name(method_str)) + if next_response_is_raw? + disable_raw_response + return ent.with_raw_response.go + else + return ent.go + end + end 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 + if method_str == underscore_name(key) + if next_response_is_raw? + disable_raw_response + return link.with_raw_response.go + else + return link.go + end + end end # Does it match an action, if so return the action. @actions.each do |key, action| - return action if method_str == key.underscore + return action if method_str == underscore_name(key) end raise NoMethodError, "The method \"#{method_str}\" does not match a property, action, or link on SirenClient::Entity." end private + + def underscore_name(str) + str.underscore.gsub(' ', '_') + end def parse_data return if @payload.nil? @classes = @payload['class'] || [] @properties = @payload['properties'] || { }