Sha256: 17d12e06f9e26b363c75426a22fbf6e954133ddbe09540f499e5dd7c61833cb5

Contents?: true

Size: 1.35 KB

Versions: 9

Compression:

Stored size: 1.35 KB

Contents

module RemoteResource
  module Builder
    extend ActiveSupport::Concern

    module ClassMethods

      def build_resource_from_response(response)
        build_resource response.sanitized_response_body, response_hash(response)
      end

      def build_resource(collection, response_hash = {})
        if collection.is_a? Hash
          new collection.merge response_hash
        end
      end

      def build_collection_from_response(response)
        build_collection response.sanitized_response_body, response_hash(response)
      end

      def build_collection(collection, response_hash = {})
        if collection.is_a? Array
          RemoteResource::Collection.new self, collection, response_hash
        end
      end

      private

      def response_hash(response_object)
        { _response: response_object, meta: response_object.sanitized_response_meta }
      end
    end

    def rebuild_resource_from_response(response)
      rebuild_resource response.sanitized_response_body, response_hash(response)
    end

    def rebuild_resource(collection, response_hash = {})
      if collection.is_a? Hash
        self.attributes = collection.merge(response_hash)
      else
        self.attributes = response_hash
      end and self
    end

    private

    def response_hash(response_object)
      self.class.send :response_hash, response_object
    end

  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
ddy_remote_resource-1.0.0.rc2 lib/remote_resource/builder.rb
ddy_remote_resource-0.4.11 lib/remote_resource/builder.rb
ddy_remote_resource-1.0.0.rc1 lib/remote_resource/builder.rb
ddy_remote_resource-0.4.10 lib/remote_resource/builder.rb
ddy_remote_resource-0.4.9 lib/remote_resource/builder.rb
ddy_remote_resource-0.4.8 lib/remote_resource/builder.rb
ddy_remote_resource-0.4.7 lib/remote_resource/builder.rb
ddy_remote_resource-0.4.6 lib/remote_resource/builder.rb
ddy_remote_resource-0.4.5 lib/remote_resource/builder.rb