Sha256: bb129ae823a3a08605d18b342915fbc55451d09f201b1f326716d9215352a63a

Contents?: true

Size: 1.13 KB

Versions: 7

Compression:

Stored size: 1.13 KB

Contents

module Recurly
  module Formats
    class XmlWithPaginationFormat
      include ActiveResource::Formats::XmlFormat

      def decode(xml)
        data = super

        if data.is_a?(Hash) and data["type"] == "collection" and data["current_page"]
          data = paginate_data(data)
        end

        data
      end

      # convert the data into a paginated resultset (array with singleton methods)
      def paginate_data(data)

        # find the first array and use that as the resultset (lame workaround)
        results = data.values.select{|v| v.is_a?(Array)}.first || []

        # define total_entries accessor on result object
        total_entries = data["total_entries"] || 0
        results.instance_eval "def total_entries; #{total_entries.to_i}; end"

        # define current_page accessor on result object
        current_page = data["current_page"] || 0
        results.instance_eval "def current_page; #{current_page.to_i}; end"

        # define per_page accessor on result object
        per_page = data["per_page"] || 0
        results.instance_eval "def per_page; #{per_page.to_i}; end"

        results
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
recurly-0.2.7 lib/recurly/formats/xml_with_pagination.rb
recurly-0.2.6 lib/recurly/formats/xml_with_pagination.rb
recurly-0.2.5 lib/recurly/formats/xml_with_pagination.rb
recurly-0.2.4 lib/recurly/formats/xml_with_pagination.rb
recurly-0.2.3 lib/recurly/formats/xml_with_pagination.rb
recurly-0.2.2 lib/recurly/formats/xml_with_pagination.rb
recurly-0.2.1 lib/recurly/formats/xml_with_pagination.rb