From c8f84bc88d095e031daa6baf65128ac1a66659bc Mon Sep 17 00:00:00 2001 From: guilherme silveira Date: Sun, 24 Oct 2010 18:34:50 -0200 Subject: [PATCH 1/2] adding refresh support --- lib/restfulie/common.rb | 3 +- lib/restfulie/common/core_ext/hash.rb | 6 ++++ lib/restfulie/common/links.rb | 9 +++++ lib/restfulie/common/representation.rb | 5 +-- spec/unit/common/links_spec.rb | 46 ++++++++++++++++++++++++++ 5 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 lib/restfulie/common/links.rb create mode 100644 spec/unit/common/links_spec.rb diff --git a/lib/restfulie/common.rb b/lib/restfulie/common.rb index d2e87a61..efb7b070 100644 --- a/lib/restfulie/common.rb +++ b/lib/restfulie/common.rb @@ -4,14 +4,15 @@ require 'rubygems' require 'active_support' require 'action_controller' -require 'restfulie/common/core_ext' module Restfulie module Common autoload :Error, 'restfulie/common/error' + autoload :Links, 'restfulie/common/links' autoload :Logger, 'restfulie/common/logger' autoload :Representation, 'restfulie/common/representation' autoload :Converter, 'restfulie/common/converter' end end +require 'restfulie/common/core_ext' diff --git a/lib/restfulie/common/core_ext/hash.rb b/lib/restfulie/common/core_ext/hash.rb index f00bab11..b997a436 100644 --- a/lib/restfulie/common/core_ext/hash.rb +++ b/lib/restfulie/common/core_ext/hash.rb @@ -1,12 +1,18 @@ class Hash + def links(*args) links = fetch("link", []) Restfulie::Common::Converter::Xml::Links.new(links) end + + include Restfulie::Common::Links + def method_missing(sym, *args) self[sym.to_s].nil? ? super(sym, args) : self[sym.to_s] end + def respond_to?(sym) include?(sym.to_s) || super(sym) end + end diff --git a/lib/restfulie/common/links.rb b/lib/restfulie/common/links.rb new file mode 100644 index 00000000..93e61933 --- /dev/null +++ b/lib/restfulie/common/links.rb @@ -0,0 +1,9 @@ +module Restfulie + module Common + module Links + def refresh + links.self.follow.get + end + end + end +end diff --git a/lib/restfulie/common/representation.rb b/lib/restfulie/common/representation.rb index 13fb60f7..94c2f822 100644 --- a/lib/restfulie/common/representation.rb +++ b/lib/restfulie/common/representation.rb @@ -1,6 +1,3 @@ module Restfulie::Common::Representation - autoload :Atom, 'restfulie/common/representation/atom' - autoload :Generic, 'restfulie/common/representation/generic' - autoload :Json, 'restfulie/common/representation/json' - autoload :Links, 'restfulie/common/representation/links' + Dir["#{File.dirname(__FILE__)}/representation/*.rb"].each {|f| autoload File.basename(f)[0..-4].camelize.to_sym, f } end diff --git a/spec/unit/common/links_spec.rb b/spec/unit/common/links_spec.rb new file mode 100644 index 00000000..3db79b1d --- /dev/null +++ b/spec/unit/common/links_spec.rb @@ -0,0 +1,46 @@ +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') + +describe Restfulie::Common::Links do + + class RefreshieLinks + include Restfulie::Common::Links + def initialize(what) + @what = what + end + def links + RefreshieSelf.new(@what) + end + end + + class RefreshieSelf + def initialize(what) + @what = what + end + def self + RefreshieFollow.new(@what) + end + end + + class RefreshieGet + def initialize(what) + @what = what + end + def get + @what + end + end + + class RefreshieFollow + def initialize(what) + @what = what + end + def follow + RefreshieGet.new(@what) + end + end + + it "should get self when refreshing itself" do + RefreshieLinks.new("myself").refresh.should == "myself" + end + +end From 14d28fa57b4d286509dab8c76e2f3edeb1771ef6 Mon Sep 17 00:00:00 2001 From: guilherme silveira Date: Sun, 24 Oct 2010 18:35:54 -0200 Subject: [PATCH 2/2] adding support to refresh for atom and json --- lib/restfulie/common/representation/atom/base.rb | 2 ++ lib/restfulie/common/representation/json/keys_as_methods.rb | 2 ++ 2 files changed, 4 insertions(+) diff --git a/lib/restfulie/common/representation/atom/base.rb b/lib/restfulie/common/representation/atom/base.rb index a9bd80d8..0acabdcf 100644 --- a/lib/restfulie/common/representation/atom/base.rb +++ b/lib/restfulie/common/representation/atom/base.rb @@ -104,6 +104,8 @@ def contributors @contributors end + include Restfulie::Common::Links + # It has one required attribute, href, and five optional attributes: rel, type, hreflang, title, and length def links return @links if @links diff --git a/lib/restfulie/common/representation/json/keys_as_methods.rb b/lib/restfulie/common/representation/json/keys_as_methods.rb index 13153a64..a256ffa4 100644 --- a/lib/restfulie/common/representation/json/keys_as_methods.rb +++ b/lib/restfulie/common/representation/json/keys_as_methods.rb @@ -44,6 +44,8 @@ def __metaclass__ class << self; self; end end + include Restfulie::Common::Links + # easy accessors to links def links some_links = self["link"]