module CodeclimateCi
  class GetGpa
    NULL_VALUE = 0

    def initialize(api_requester, branch)
      @api_requester, @branch = api_requester, branch
    end

    def gpa
      retry_count.times do
        return last_snapshot_gpa if analyzed?
        wait_and_refresh!
      end

      NULL_VALUE
    end

    private

    def wait_and_refresh!
      increment_retry_counter
      Report.result_not_ready(@retry_counter, @branch)
      refresh!
      sleep(sleep_time)
    end

    def increment_retry_counter
      @retry_counter ||= 0
      @retry_counter += 1
    end

    def retry_count
      CodeclimateCi.configuration.retry_count.to_i
    end

    def sleep_time
      CodeclimateCi.configuration.sleep_time.to_i
    end

    def last_snapshot_gpa
      branch_info['last_snapshot']['gpa'].to_f
    end

    def analyzed?
      branch_info.include?('last_snapshot')
    end

    def branch_info
      @branch_info ||= @api_requester.branch_info(@branch)
    end

    def refresh!
      @branch_info = nil
    end
  end
end

module CodeclimateCi
  class GetGpa
    NULL_VALUE = 0

    def initialize(api_requester, branch)
      @api_requester, @branch = api_requester, branch
    end

    def gpa
      retry_counter = 0

      retry_count.times do
        return last_snapshot_gpa if analyzed?
        retry_counter += 1
        wait_and_refresh!(retry_counter)
      end

      NULL_VALUE
    end

    private

    def wait_and_refresh!(retry_counter)
      Report.result_not_ready(retry_counter, @branch)
      refresh!
      sleep(sleep_time)
    end

    def retry_count
      CodeclimateCi.configuration.retry_count.to_i
    end

    def sleep_time
      CodeclimateCi.configuration.sleep_time.to_i
    end

    def last_snapshot_gpa
      branch_info['last_snapshot']['gpa'].to_f
    end

    def analyzed?
      branch_info.include?('last_snapshot')
    end

    def branch_info
      @branch_info ||= @api_requester.branch_info(@branch)
    end

    def refresh!
      @branch_info = nil
    end
  end
end

--- 
+++ 
@@ -7,12 +7,9 @@
     end
 
     def gpa
-      retry_counter = 0
-
       retry_count.times do
         return last_snapshot_gpa if analyzed?
-        retry_counter += 1
-        wait_and_refresh!(retry_counter)
+        wait_and_refresh!
       end
 
       NULL_VALUE
@@ -20,10 +17,16 @@
 
     private
 
-    def wait_and_refresh!(retry_counter)
-      Report.result_not_ready(retry_counter, @branch)
+    def wait_and_refresh!
+      increment_retry_counter
+      Report.result_not_ready(@retry_counter, @branch)
       refresh!
       sleep(sleep_time)
+    end
+
+    def increment_retry_counter
+      @retry_counter ||= 0
+      @retry_counter += 1
     end
 
     def retry_count
Move retry_counter in to wait_and_refresh method in GetGpa class
