Sha256: 86787593ba3a1d9b03f7a1f2ac209a67ddd487e4d143d6dd503e1991b6064255

Contents?: true

Size: 1.16 KB

Versions: 14

Compression:

Stored size: 1.16 KB

Contents

class Middleman::Extensions::DataLoaders < ::Middleman::Extension
  def initialize(app, options_hash={})
    super

    require 'open-uri'
  end

  helpers do
    def pwd
      File.dirname(File.expand_path(current_page.path, root))
    end

    def load_data(path, options = nil)
      resource = if path.start_with? 'http'
        load_remote_data(path, options)
      else
        sitemap.find_resource_by_path(relative_dir(current_page.path, path).to_s)
      end

      raise "#{path} not found" unless resource

      if ['.yaml', '.yml'].include? resource.ext
        yaml = resource.render
      elsif resource.ext == '.json'
        json = resource.render
      end

      if json
        JSON.parse(resource.render)
      elsif yaml
        YAML.load(resource.render)
      end
    end

    private

    def load_remote_data(url, options)
      begin
        if(options)
          resource = open(url, http_basic_authentication: [options[:user], options[:password]])
        else
          resource = open(url)
        end
      rescue
        raise "Couldn't load the remote: #{url}."
      end
      {
        ext: '.json',
        render: resource.read
      }
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
sculptor-0.0.11.1 lib/sculptor/extensions/data_loaders.rb
sculptor-0.0.11 lib/sculptor/extensions/data_loaders.rb
sculptor-0.0.10 lib/sculptor/extensions/data_loaders.rb
sculptor-0.0.9.3 lib/sculptor/extensions/data_loaders.rb
sculptor-0.0.9.2 lib/sculptor/extensions/data_loaders.rb
sculptor-0.0.9.1 lib/sculptor/extensions/data_loaders.rb
sculptor-0.0.9 lib/sculptor/extensions/data_loaders.rb
sculptor-0.0.8 lib/sculptor/extensions/data_loaders.rb
sculptor-0.0.7 lib/sculptor/extensions/data_loaders.rb
sculptor-0.0.6 lib/sculptor/extensions/data_loaders.rb
sculptor-0.0.5 lib/sculptor/extensions/data_loaders.rb
sculptor-0.0.4 lib/sculptor/extensions/data_loaders.rb
sculptor-0.0.3 lib/sculptor/extensions/data_loaders.rb
sculptor-0.0.2 lib/sculptor/extensions/data_loaders.rb