Sha256: d1758672597428f61587eea67282e55365e7c2003dde21921551b0a5297ed7e8

Contents?: true

Size: 1.47 KB

Versions: 13

Compression:

Stored size: 1.47 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 respond_to?(meth, include_all=false)
      super || resource_object(meth).respond_to?(meth, include_all)
    end

    private

    def get!
      klass = xml['type'] == 'array' ? ResourceCollection : SingleResource
      @resource_object = klass.new(@url, @auth, @xml, @options)
    end

    def collection_call?(meth, *args)
      ([].respond_to?(meth) && !["delete", "id", "[]"].include?(meth.to_s)) ||
      ResourceCollection.instance_methods(false).map(&:to_s).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

13 entries across 13 versions & 1 rubygems

Version Path
almodovar-2.0.2 lib/almodovar/resource.rb
almodovar-2.0.1 lib/almodovar/resource.rb
almodovar-1.8.1 lib/almodovar/resource.rb
almodovar-2.0.0 lib/almodovar/resource.rb
almodovar-1.8.0 lib/almodovar/resource.rb
almodovar-1.7.8 lib/almodovar/resource.rb
almodovar-1.7.7 lib/almodovar/resource.rb
almodovar-1.7.6 lib/almodovar/resource.rb
almodovar-1.7.3 lib/almodovar/resource.rb
almodovar-1.7.2 lib/almodovar/resource.rb
almodovar-1.7.1 lib/almodovar/resource.rb
almodovar-1.7.0 lib/almodovar/resource.rb
almodovar-1.6.0 lib/almodovar/resource.rb