Sha256: c90fab21cff1d9106744bd9f8a2c5ad11c93e4cf03a2f4a700bf25cebb4c21e3

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

module SlackRubyBotServer
  module Api
    module Presenters
      module PaginatedPresenter
        include Roar::JSON::HAL
        include Roar::Hypermedia
        include Grape::Roar::Representer

        property :total_count

        link :self do |opts|
          "#{request_url(opts)}#{query_string_for_cursor(nil, opts)}"
        end

        link :next do |opts|
          "#{request_url(opts)}#{query_string_for_cursor(represented.next, opts)}" if represented.next
        end

        private

        def request_url(opts)
          request = Grape::Request.new(opts[:env])
          "#{request.base_url}#{opts[:env]['PATH_INFO']}"
        end

        # replace the page and offset parameters in the query string
        def query_string_for_cursor(cursor, opts)
          qs = Hashie::Mash.new(Rack::Utils.parse_nested_query(opts[:env]['QUERY_STRING']))
          if cursor
            qs.merge!(cursor: cursor)
            qs.delete(:offset)
          end
          "?#{qs.to_query}" unless qs.empty?
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
slack-ruby-bot-server-0.1.1 lib/slack-ruby-bot-server/api/presenters/paginated_presenter.rb
slack-ruby-bot-server-0.1.0 lib/slack-ruby-bot-server/api/presenters/paginated_presenter.rb