Sha256: 8a56909fb0ce7ab5aaff1ad6f4f2b77399c493503856ece3f7ce720ec56dc760

Contents?: true

Size: 1.4 KB

Versions: 2

Compression:

Stored size: 1.4 KB

Contents

module PuppetRepl
  module Support
    module Play

      def play_back(config={})
        if config[:play]
          if config[:play] =~ /^http/
            play_back_url(config[:play])
          elsif File.exists? config[:play]
            play_back_string(File.read(config[:play]))
          else config[:play]
            out_buffer.puts "puppet-repl can't play #{config[:play]}'"
          end
        end
      end

      def convert_to_text(url)
        require 'uri'
        url_data = URI(url)
        case url_data.host
        when /^github.com/
          if url_data.path =~ /blob/
            url.gsub('blob', 'raw')
          end
        when /^gist.github.com/
          unless url_data.path =~ /raw/
            url = url += '.txt'
          end
        when /^gitlab.com/
          if url_data.path =~ /snippets/
            url += '/raw' unless url_data.path =~ /raw/
            url
          else
            url.gsub('blob', 'raw')
          end
        else
          url
        end
      end

      def play_back_url(url)
        begin
          require 'open-uri'
          require 'net/http'
          converted_url = convert_to_text(url)
          str = open(converted_url).read
          play_back_string(str)
        rescue SocketError
          abort "puppet-repl can't play `#{converted_url}'"
        end
      end

      def play_back_string(str)
        handle_input(str)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
puppet-repl-0.2.1 lib/puppet-repl/support/play.rb
puppet-repl-0.2.0 lib/puppet-repl/support/play.rb