class Fanforce::API::Response attr_reader :curl_command, :requested_url, :requested_params def self.process(response, request, requested_url, requested_params) if response.code > 201 puts "FANFORCE API CRAPPED OUT: #{response.code}" puts response.args.to_json puts '--------------------------------------' puts response.args[:payload] puts '--------------------------------------' puts response.body puts '--------------------------------------' puts response.net_http_res end 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