class Fanforce::Api::Response attr_reader :curl_command, :requested_url, :requested_params def initialize(response, request, requested_url, requested_params) @response = response @request = request @requested_url = requested_url @requested_params = requested_params raise Fanforce::Api::ServerError.new(response, request, requested_url, requested_params) if response.code > 201 begin @response_hash = Fanforce::Utils.decode_json(response) rescue Exception => e raise Fanforce::Api::DecodingError.new(e, response, request, requested_url, requested_params) end @response_body = response.to_s @curl_command = Fanforce::Utils.curl_command(request.method, requested_url, requested_params) if @response_hash[:results].nil? @result = @response_hash else @results = @response_hash[:results] @current_results = @response_hash[:current_results] @total_results = @response_hash[:total_results] @current_page = @response_hash[:current_page] @total_pages = @response_hash[:total_pages] if request.method == :get and @response_hash[:total_pages] > @response_hash[:current_page] next_page = @response_hash[:current_page] + 1 @next_url = "#{requested_url}?#{Fanforce::Utils.to_query_string(requested_params.merge(page: next_page))}" end if request.method == :get and @response_hash[:current_page] > 1 prev_page = @response_hash[:current_page] - 1 @prev_url = "#{requested_url}?#{Fanforce::Utils.to_query_string(requested_params.merge(page: prev_page))}" end end end def code @response.code end def to_json to_hash.to_json end def to_hash @response_hash end def to_s @response_body end def [](key) @response_hash[key] end def result @result || (raise NoMethodError, 'This API response returns more than 1 record. Try using "results".') end def results @results || (raise NoMethodError, 'This API response only returns 1 record. Try using "result".') end def current_results @current_results || 1 end def total_results @total_results || 1 end def current_page @current_page || 1 end def total_pages @total_pages || 1 end def prev_url @prev_url || nil end def next_url @next_url || nil end end