Sha256: dd2b4352b14fc36daf67ea11513a627fa0f02328543a08df433762e511fc2cde

Contents?: true

Size: 1.53 KB

Versions: 3

Compression:

Stored size: 1.53 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, option={})
        # setup options
        option[:keep_mtime] ||= true

        # copy
        http_get {|rec| dest.write rec.body}

        # modify mtime
        dest.mtime = self.mtime if option[:keep_mtime]
      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

3 entries across 3 versions & 1 rubygems

Version Path
pione-0.3.2 lib/pione/location/http-location.rb
pione-0.3.1 lib/pione/location/http-location.rb
pione-0.3.0 lib/pione/location/http-location.rb