module AppiumRspecSetup
  class Gemfile
    def self.configure_gemfile_and_bundle!
      file_array = File.readlines('GemFile')

      insert_hash = {
          'appium_lib' => "gem 'appium_lib'",
          'rspec' => "gem 'rspec', require: 'spec'",
          'pry' => "gem 'pry'"
      }

      offset = -1

      insert_hash.each do |substring_key, line_for_file_value|
        file_array.insert(offset, line_for_file_value) unless file_array.any? do |line|
          line.include? substring_key
        end
      end

      File.open('GemFile', 'w') do |file|
        file.puts file_array
      end

      %x{bundle}
    end
  end
end