Sha256: a32f93df05b379f8e87636b18cb55aa3ec8443314bef9b833ceca4150561cdda

Contents?: true

Size: 782 Bytes

Versions: 2

Compression:

Stored size: 782 Bytes

Contents

require 'json'
require 'net/http'

module Regaliator
  class Response
    PAGINATION_HEADER = 'X-Pagination'.freeze

    attr_reader :http_response

    def initialize(http_response)
      @http_response = http_response
    end

    def data
      @data ||= begin
        JSON::parse(http_response.body).tap do |hsh|
          hsh.merge!('pagination' => pagination) if paginated?
        end
      rescue
        { message: 'Server returned a non-json error' }
      end
    end

    def success?
      @success ||= http_response.kind_of?(Net::HTTPSuccess)
    end

    def fail?
      !success?
    end

    def paginated?
      http_response.key?(PAGINATION_HEADER)
    end

    def pagination
      @pagination ||= JSON::parse(http_response[PAGINATION_HEADER])
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
regaliator-4.0.1 lib/regaliator/response.rb
regaliator-4.0.0 lib/regaliator/response.rb