class Uricp::Strategy::PipedRemoteGet

Public Instance Methods

appropriate?() click to toggle source
# File lib/uricp/strategy/piped_remote_get.rb, line 6
def appropriate?
  case from.scheme
  when 'http', 'https'
    return proposal unless sequence_complete?
  else
    debug "#{self.class.name}: not appropriate"
    false
  end
end
format_peek() click to toggle source
# File lib/uricp/strategy/piped_remote_get.rb, line 37
def format_peek
  return options['target-format'] || 'raw' if dry_run?

  options['from_uri'].open(headers) do |u|
    encoding(u)
  end
rescue OpenURI::HTTPError => e
  case e.io.status[0]
  when '416'
    'raw'
  else
    raise
  end
rescue SocketError => e
  raise SocketError, options['from_uri'].to_s + ' inaccessible: ' + e.message
end
headers() click to toggle source
# File lib/uricp/strategy/piped_remote_get.rb, line 74
def headers
  headers = { 'Range' => 'bytes=0-7' }
  headers['X-Auth-Token'] = options['authenticator'].call if http_authentication?
  headers
end
proposal() click to toggle source
# File lib/uricp/strategy/piped_remote_get.rb, line 18
def proposal
  @proposed_options = options.dup
  @proposed_options['from_uri'] = PIPE_URI
  if conversion_required?
    @proposed_options['source-format'] = format_peek
    if @proposed_options['source-format'] == @proposed_options['target-format']
      @proposed_options.delete('source-format')
      @proposed_options.delete('target-format')
    end
  end
  if options['max-cache'] &&
     size_peek.to_i > options['max-cache'].to_i
    @proposed_options.delete('cache')
    @proposed_options.delete('cache_name')
    @proposed_options.delete('max-cache')
  end
  self
end
size_peek() click to toggle source
# File lib/uricp/strategy/piped_remote_get.rb, line 54
def size_peek
  return options['max-cache'] if dry_run?

  size_headers = headers
  size_headers['Range'] = 'bytes=0-0'
  options['from_uri'].open(headers) do |u|
    match = %r{bytes\s+(\d+)-(\d+)/(\d+|\*)}i.match(u.meta['content-range'])
    match && match[3].to_i
  end
rescue OpenURI::HTTPError => e
  case e.io.status[0]
  when '416'
    0
  else
    raise
  end
rescue SocketError => e
  raise SocketError, options['from_uri'].to_s + ' inaccessible: ' + e.message
end