Sha256: d75fcdffc43619f702f6566ad5b035d1cb9156835c01848e01fa427c449d8969

Contents?: true

Size: 1 KB

Versions: 3

Compression:

Stored size: 1 KB

Contents

require 'open-uri'
require 'restclient'

module Awestruct
  module Extensions
    module RemotePartial

      def remote_partial(url)
        url_tmp = url.sub('http://', '')
        r = 'remote_partial/' + url_tmp[/(.*)\/[^\/].+$/, 1]
        tmp = File.join(tmp(site.tmp_dir, r), File.basename(url_tmp))
        get_or_cache(tmp, url)
      end

      alias_method :remotePartial, :remote_partial

      def get_or_cache(tmp_file, url)
        response_body = ""
        if !File.exist?tmp_file
          puts url
          response_body = RestClient.get(url, :cache => false) { |response, request, result, &block|
            case response.code
            when 404
                response
            else
              response.return!(request, result, &block)
            end
          }.body;
          File.open(tmp_file, 'w') do |out|
            out.write response_body
          end
        else
          response_body = File.read(tmp_file)
        end
        return response_body
      end


    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
awestruct-0.4.8 lib/awestruct/extensions/remotePartial.rb
awestruct-0.4.7 lib/awestruct/extensions/remotePartial.rb
awestruct-0.4.6 lib/awestruct/extensions/remotePartial.rb