Sha256: 1df1fdd04db154c2f4ee8961ed8f58cf2f9576c4fba7e6d6f55420c94fdf9d72

Contents?: true

Size: 1.23 KB

Versions: 16

Compression:

Stored size: 1.23 KB

Contents

module ThreeScaleToolbox
  module ResourceReader
    ##
    # Load resource from different types of sources.
    # Supported types are: file, URL, stdin
    # Loaded content is returned
    def load_resource(resource)
      # Json format is parsed as well
      YAML.safe_load(read_content(resource))
    rescue Psych::SyntaxError => e
      raise ThreeScaleToolbox::Error, "JSON/YAML validation failed: #{e.message}"
    end

    ##
    # Reads resources from different types of sources.
    # Supported types are: file, URL, stdin
    # Resource raw content is returned
    def read_content(resource)
      case resource
      when '-'
        method(:read_stdin)
      when /\A#{URI::DEFAULT_PARSER.make_regexp}\z/
        method(:read_url)
      else
        method(:read_file)
      end.call(resource)
    end

    # Detect format from file extension
    def read_file(filename)
      raise ThreeScaleToolbox::Error, "File not found: #{filename} " unless File.file?(filename)
      raise ThreeScaleToolbox::Error, "File not readable: #{filename} " unless File.readable?(filename)

      File.read(filename)
    end

    def read_stdin(_resource)
      STDIN.read
    end

    def read_url(resource)
      Net::HTTP.get(URI.parse(resource))
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
3scale_toolbox-0.19.3 lib/3scale_toolbox/resource_reader.rb
3scale_toolbox-0.19.2 lib/3scale_toolbox/resource_reader.rb
3scale_toolbox-0.19.1 lib/3scale_toolbox/resource_reader.rb
3scale_toolbox-0.19.0 lib/3scale_toolbox/resource_reader.rb
3scale_toolbox-0.18.3 lib/3scale_toolbox/resource_reader.rb
3scale_toolbox-0.18.2 lib/3scale_toolbox/resource_reader.rb
3scale_toolbox-0.18.0 lib/3scale_toolbox/resource_reader.rb
3scale_toolbox-0.17.1 lib/3scale_toolbox/resource_reader.rb
3scale_toolbox-0.17.0 lib/3scale_toolbox/resource_reader.rb
3scale_toolbox-0.16.0 lib/3scale_toolbox/resource_reader.rb
3scale_toolbox-0.15.0 lib/3scale_toolbox/resource_reader.rb
3scale_toolbox-0.14.0 lib/3scale_toolbox/resource_reader.rb
3scale_toolbox-0.13.0 lib/3scale_toolbox/resource_reader.rb
3scale_toolbox-0.12.4 lib/3scale_toolbox/resource_reader.rb
3scale_toolbox-0.12.3 lib/3scale_toolbox/resource_reader.rb
3scale_toolbox-0.12.2 lib/3scale_toolbox/resource_reader.rb