Sha256: f7e39a1a9843cc0aa1ab8fa9c4d65ecad30d7e383b7596ae3526ec9eafb9645f

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

module Pione
  module Location
    class HTTPLocation < DataLocation
      set_scheme "http"
      set_real_appendable false
      set_writable false

      def read
        http_get {|res| res.body}
      end

      def mtime
        http_head {|res| Time.httpdate(res['last-modified']) }
      end

      def size
        http_head {|res| res.content_length } || read.size
      end

      def exist?
        http_head {|res| true}
      rescue
        false
      end

      def file?
        exist?
      end

      def directory?
        false
      end

      def copy(dest)
        http_get {|rec| dest.write rec.body}
      end

      private

      # Send a request HTTP Get and evaluate the block with the response.
      def http_get(&b)
        http = Net::HTTP.new(@uri.host, @uri.port)
        req = Net::HTTP::Get.new(@uri.path)
        res = http.request(req)
        if res.kind_of?(Net::HTTPSuccess)
          return b.call(res)
        else
          raise NotFound.new(@uri)
        end
      end

      # Send a request HTTP Head and evaluate the block with the response.
      def http_head(&b)
        http = Net::HTTP.new(@uri.host, @uri.port)
        req = Net::HTTP::Head.new(@uri.path)
        res = http.request(req)
        if res.kind_of?(Net::HTTPSuccess)
          return b.call(res)
        else
          raise NotFound(@uri)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pione-0.2.2 lib/pione/location/http-location.rb