class Bbs::Downloader

Attributes

encoding[R]

Public Class Methods

new(encoding = 'UTF-8') click to toggle source
# File lib/bbiff/bbs_reader.rb, line 76
def initialize(encoding = 'UTF-8')
  @encoding = encoding
  @resource_cache = {}
end

Public Instance Methods

download_binary(uri) click to toggle source

ASCII-8BIT エンコーディングの文字列を返す。

# File lib/bbiff/bbs_reader.rb, line 82
def download_binary(uri)
  resource = @resource_cache[uri]
  if resource && resource.data.size > 0

    Net::HTTP.start(uri.host, uri.port) do |http|
      request = Net::HTTP::Get.new(uri)
      request['range'] = "bytes=#{resource.data.bytesize}-"
      response = http.request(request)
      response.body.force_encoding('ASCII-8BIT')
      p response.code if $DEBUG
      p response.to_hash if $DEBUG
      case response
      when Net::HTTPPartialContent
        p :partial if $DEBUG
        resource.data += response.body
      when Net::HTTPRequestedRangeNotSatisfiable
        p :not_satisfiable if $DEBUG
        # 多分DATは更新されていない
      when Net::HTTPOK
        p :ok if $DEBUG
        @resource_cache[uri] = Resource.new(response.body)
        return response.body
      else
        raise DownloadFailure.new(response)
      end
      return resource.data
    end
  else
    p :no_resource_yet if $DEBUG
    body = download_binary_nocache(uri)
    @resource_cache[uri] = Resource.new(body)
    return body
  end
end
download_binary_nocache(uri) click to toggle source
# File lib/bbiff/bbs_reader.rb, line 117
def download_binary_nocache(uri)
  response = nil
  Net::HTTP.start(uri.host, uri.port) do |http|
    request = Net::HTTP::Get.new(uri)
    response = http.request(request)
    response.body.force_encoding('ASCII-8BIT')
    p response.code if $DEBUG
    p response.to_hash if $DEBUG
    case response
    when Net::HTTPOK
    else
      raise DownloadFailure.new(response)
    end
  end
  return response.body
end
download_text(uri) click to toggle source
# File lib/bbiff/bbs_reader.rb, line 134
def download_text(uri)
  # dup は重要。
  download_binary(uri).dup.force_encoding(encoding).encode('UTF-8')
end