class Fanforce::Api::Response attr_reader :curl_command, :requested_url, :requested_params def self.process(response, request, requested_url, 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 if response_hash[:results] Fanforce::Api::Results.new(response_hash, response, request, requested_url, requested_params) else Fanforce::Api::Result.new(response_hash, response, request, requested_url, requested_params) end end end class Fanforce::Api::Result < Hash attr_reader :requested_url, :requested_params def initialize(response_hash, response, request, requested_url, requested_params) @response = response @request = request @requested_url = requested_url @requested_params = requested_params @response_hash = response_hash self.replace(response_hash) end def data self end alias result data def curl_command @curl_command ||= Fanforce::Utils.curl_command(@request.method, @requested_url, @requested_params) end def body; @response.to_s end def current_results; 1 end def total_results; 1 end def current_page; 1 end def total_pages; 1 end def prev_page; nil end def next_page; nil end def prev_page_url; nil end def next_page_url; nil end def code; @response.code end end class Fanforce::Api::Results < Array attr_reader :requested_url, :requested_params def initialize(response_hash, response, request, requested_url, requested_params) @response = response @request = request @requested_url = requested_url @requested_params = requested_params @response_hash = response_hash self.replace(response_hash[:results]) end def data self end alias results data def curl_command @curl_command ||= Fanforce::Utils.curl_command(@request.method, @requested_url, @requested_params) end def body; @response.to_s end def current_results @current_results ||= @response_hash[:current_results] end def total_results; @total_results ||= @response_hash[:total_results] end def current_page; @current_page ||= @response_hash[:current_page] end def total_pages; @total_pages ||= @response_hash[:total_pages] end def prev_page @prev_page ||= (@request.method == :get and current_page > 1) ? current_page - 1 : nil end def next_page @next_page ||= (@request.method == :get and total_pages > current_page) ? current_page + 1 : nil end def prev_page_url; @prev_page_url ||= prev_page ? "#{@requested_url}?#{Fanforce::Utils.to_query_string(@requested_params.merge(page: current_page-1))}" : nil end def next_page_url; @next_page_url ||= next_page ? "#{@requested_url}?#{Fanforce::Utils.to_query_string(@requested_params.merge(page: current_page+1))}" : nil end def code; @response.code end end