Sha256: f7d864fda231716efe073a5ddcee16cdc2d350aa4cd4b53a5caf56c52aa71c07

Contents?: true

Size: 1.43 KB

Versions: 14

Compression:

Stored size: 1.43 KB

Contents

module Almodovar
  class Resource
    include HttpAccessor

    undef_method :id if instance_methods.include?("id")
    undef_method :type if instance_methods.include?("type")
  
    delegate :inspect, :to => :get!
  
    def self.from_xml(xml, auth = nil)
      new(nil, auth, Nokogiri::XML.parse(xml).root)
    end
  
    def initialize(url, auth, xml = nil, options = {})
      @url = url
      @auth = auth
      @xml = xml
      @options = options
    end
  
    def method_missing(meth, *args, &blk)
      resource_object(meth).send(meth, *args, &blk)
    end
    
    def get!
      klass = xml['type'] == 'array' ? ResourceCollection : SingleResource
      @resource_object = klass.new(@url, @auth, @xml, @options)
    end
    
    def respond_to?(meth)
      super || resource_object(meth).respond_to?(meth)
    end
  
    private
  
    def collection_call?(meth, *args)
      ([].respond_to?(meth) && !["delete", "id", "[]"].include?(meth.to_s)) ||
      ["create"].include?(meth.to_s) ||
      (meth.to_s == "[]" && args.size == 1 && args.first.is_a?(Fixnum))
    end
    
    def resource_object(meth)
      @resource_object ||= resource_class(meth).new(@url, @auth, @xml, @options)
    end
  
    def resource_class(meth, *args)
      @resource_class ||= collection_call?(meth, *args) ? ResourceCollection : SingleResource
    end
  
  end
  
  def self.Resource(url, auth = nil, params = {})
    Resource.new(url, auth, nil, params)
  end
  
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
almodovar-0.9.8 lib/almodovar/resource.rb
almodovar-0.9.7 lib/almodovar/resource.rb
almodovar-0.9.6 lib/almodovar/resource.rb
almodovar-0.9.5 lib/almodovar/resource.rb
almodovar-0.9.4 lib/almodovar/resource.rb
almodovar-0.9.2 lib/almodovar/resource.rb
almodovar-0.9.3 lib/almodovar/resource.rb
almodovar-0.9.1 lib/almodovar/resource.rb
almodovar-0.9.0 lib/almodovar/resource.rb
almodovar-0.8.0 lib/almodovar/resource.rb
almodovar-0.7.0 lib/almodovar/resource.rb
almodovar-0.6.2 lib/almodovar/resource.rb
almodovar-0.6.1 lib/almodovar/resource.rb
almodovar-0.6.0 lib/almodovar/resource.rb