class Fanforce::Api::Response attr_reader :curl_command, :request_url, :response_body, :response_json def initialize(response, request, url, req_params) case response.code when 200, 201 begin @response_body = response.to_s @response_json = Fanforce::Utils.decode_json(response) @curl_command = Fanforce::Utils.curl_command(request.method, url, req_params) @request_url = url if !@response_json[:results].nil? @results = @response_json[:results] @current_results = @response_json[:current_results] @total_results = @response_json[:total_results] @current_page = @response_json[:current_page] @total_pages = @response_json[:total_pages] if request.method == :get and @response_json[:total_pages] > @response_json[:current_page] next_page = @response_json[:current_page] + 1 @next_url = "#{url}?#{Fanforce::Utils.to_query_string(req_params.merge(page: next_page))}" end if request.method == :get and @response_json[:current_page] > 1 prev_page = @response_json[:current_page] - 1 @prev_url = "#{url}?#{Fanforce::Utils.to_query_string(req_params.merge(page: prev_page))}" end else @result = @response_json end rescue raise end else raise Fanforce::Api::Error.new(response, request, url, req_params) end end def to_json to_hash.to_json end def to_hash @response_json end def [](key) @response_json[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