Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,3 @@ gem 'rake'
group :documentation do
gem 'yard'
end

platforms :rbx do
gem 'racc'
gem 'rubysl', '~> 2.0'
gem 'rubysl-test-unit'
gem 'psych'
end
1 change: 0 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ task :default => :test

Rake::TestTask.new(:test) do |t|
t.test_files = FileList['spec/*_spec.rb']
t.ruby_opts = ['-rubygems'] if defined? Gem
t.ruby_opts << '-w -I.'
end
56 changes: 7 additions & 49 deletions lib/pinch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,6 @@ def self.file_list(url, user = nil, pass = nil)
new(url, user, pass).file_list
end

##
# Retrieve the size of the ZIP file
#
# @param [String] url Full URL to the ZIP file
# @param [String] user (Optional) Username for Basic Authentication
# @param [String] pass (Optional) Password for Basic Authentication
# @return [Fixnum] Size of the ZIP file
# @example
#
# Pinch.content_length('http://peterhellberg.github.com/pinch/test.zip') #=> 2516612
#
def self.content_length(url, user = nil, pass = nil)
new(url, user, pass).content_length
end

##
# Initializes a new Pinch object
#
Expand Down Expand Up @@ -118,30 +103,6 @@ def get(file_name, &block)
local_file(file_name, &block)
end

##
# @note You might want to use Pinch.content_length instead
#
def content_length
@content_length ||= begin
request = Net::HTTP::Head.new(@head_uri.request_uri)
request.basic_auth(@user, @pass) unless @user.nil? || @pass.nil?
response = connection(@head_uri).request(request)

# Raise exception if the response code isn’t in the 2xx range
response.error! unless response.kind_of?(Net::HTTPSuccess)

response['Content-Length'].to_i
rescue Net::HTTPRetriableError => e
@head_uri = URI.parse(e.response['Location'])

if (@redirects -= 1) > 0
retry
else
raise TooManyRedirects, "Gave up at on #{@head_uri.host}"
end
end
end

private

def local_file(file_name)
Expand Down Expand Up @@ -170,11 +131,11 @@ def local_file(file_name)
file_headers[file_name][12]

if block_given?
fetch_data(offset_start, offset_end) do |response|
fetch_data(offset_start..offset_end) do |response|
yield PinchResponse.new(response)
end
else
response = fetch_data(offset_start, offset_end)
response = fetch_data(offset_start..offset_end)

local_file_header = response.body.unpack('VvvvvvVVVvv')
file_data = response.body[30+local_file_header[9]+local_file_header[10]..-1]
Expand Down Expand Up @@ -234,8 +195,7 @@ def central_directory
offset_start = end_of_central_directory_record[5]
offset_end = end_of_central_directory_record[5] + end_of_central_directory_record[4]

response = fetch_data(offset_start, offset_end)

response = fetch_data(offset_start..offset_end)

if ['200', '206'].include?(response.code)
response.body
Expand All @@ -255,10 +215,8 @@ def end_of_central_directory_record
#6 uint16 ZIPfileCommentLength;

@end_of_central_directory_record ||= begin
# Retrieve a 4k of data from the end of the zip file
offset = content_length >= 4096 ? content_length-4096 : 0

response = fetch_data(offset, content_length)
# Retrieve last 4k of data from the zip file
response = fetch_data(-4096)

# Unpack the body into a hex string then split on
# the end record signature, and finally unpack the last one.
Expand All @@ -276,10 +234,10 @@ def end_of_central_directory_record

##
# Get range of data from URL
def fetch_data(offset_start, offset_end, &block)
def fetch_data(range, &block)
request = Net::HTTP::Get.new(@get_uri.request_uri)
request.basic_auth(@user, @pass) unless @user.nil? || @pass.nil?
request.set_range(offset_start..offset_end)
request.set_range(range) if range
connection(@get_uri).request(request, &block)
end

Expand Down
11 changes: 6 additions & 5 deletions pinch.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ $:.unshift lib unless $:.include?(lib)
require 'pinch'

Gem::Specification.new do |s|
s.required_ruby_version = '>= 1.8.7'
s.required_ruby_version = '>= 3.0.0'
s.require_paths << 'lib'

s.name = "pinch"
Expand All @@ -16,7 +16,6 @@ Gem::Specification.new do |s|
s.authors = ["Peter Hellberg", "Edward Patel"]
s.license = "MIT-LICENSE"

s.has_rdoc = true
s.rdoc_options = ['--main', 'README.rdoc', '--charset=UTF-8']
s.extra_rdoc_files = ['README.rdoc', 'MIT-LICENSE']

Expand All @@ -25,7 +24,9 @@ Gem::Specification.new do |s|
s.files = Dir.glob("lib/**/*") +
%w(MIT-LICENSE README.rdoc Rakefile .gemtest)

s.add_development_dependency 'minitest', '~> 5.2'
s.add_development_dependency 'webmock', '~> 1.16'
s.add_development_dependency 'vcr', '~> 2.8'
s.add_dependency 'net-http'

s.add_development_dependency 'minitest'
s.add_development_dependency 'webmock'
s.add_development_dependency 'vcr'
end
103 changes: 36 additions & 67 deletions spec/pinch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,15 @@
require File.dirname(__FILE__) + '/../lib/pinch'

describe Pinch do
describe "when calling get on a compressed ZIP file" do
it "should return the contents of the file" do
VCR.use_cassette('squeak') do
@url = 'http://ftp.sunet.se/pub/lang/smalltalk/Squeak/current_stable/Squeak3.8-6665-full.zip'
@file = 'ReadMe.txt'

data = Pinch.get @url, @file
data.must_match(/Morphic graphics architecture/)
data.size.must_equal 26431
end
end
end

describe "when calling get on a ZIP file that is not compressed" do
it "should return the contents of the file" do
VCR.use_cassette('canabalt') do
@url = 'http://memention.com/ericjohnson-canabalt-ios-ef43b7d.zip'
@file = 'ericjohnson-canabalt-ios-ef43b7d/README.TXT'

data = Pinch.get @url, @file
data.must_match(/Daring Escape/)
data.size.must_equal 2288
assert_match(/Daring Escape/, data)
assert_equal(2288, data.size)
end
end
end
Expand All @@ -66,92 +53,74 @@
it "should retrieve the contents of the file data.json" do
VCR.use_cassette('test_zip') do
data = Pinch.get @url, @file
data.must_equal @data
data.size.must_equal 114
assert_equal @data, data
assert_equal 114, data.size
end
end

it "should yield to the block with PinchResponse object similar to HTTPResponse" do
body = ''
VCR.use_cassette('test_zip_with_block') do
Pinch.get(@url, @file) do |response|
response.must_be_kind_of PinchResponse
assert_kind_of PinchResponse, response
response.read_body do |chunk|
body << chunk
end
end
end
body.must_equal @data
assert_equal @data, body
end

it "should retrieve the contents of the file data.json when passed a HTTPS url" do
VCR.use_cassette('ssl_test') do
@url = 'https://dl.dropboxusercontent.com/u/2230186/pinch_test.zip'
@url = 'https://peterhellberg.github.io/pinch/test.zip'

data = Pinch.get @url, @file
data.must_equal @data
data.size.must_equal 114
assert_equal @data, data
assert_equal 114, data.size
end
end

it "should contain three files" do
VCR.use_cassette('test_file_count') do
Pinch.file_list(@url).size.must_equal 3
assert_equal 3, Pinch.file_list(@url).size
end
end
end

describe "when calling get on the example ZIP file behind HTTP Basic Authentication" do
before do
@url = 'http://assets.c7.se/data/pinch/auth/pinch_test.zip'
@file = 'data.json'
@data = "{\"gem\":\"pinch\",\"authors\":[\"Peter Hellberg\",\"Edward Patel\"],\"github_url\":\"https://github.com/peterhellberg/pinch\"}\n"
end

it "should retrieve the contents of the file data.json with valid authentication" do
VCR.use_cassette('valid_basic_auth') do
data = Pinch.get @url, @file, 'pinch_test', 'thisisjustatest'
data.must_equal @data
data.size.must_equal 114
end
end

it "should not retrieve the contents of the file data.json with invalid authentication" do
VCR.use_cassette('invalid_basic_auth') do
lambda {
Pinch.get @url, @file, 'invalid_username', 'invalid_password'
}.must_raise Net::HTTPServerException
end
end
end
# This location is no longer protected by basic auth.
#
# describe "when calling get on the example ZIP file behind HTTP Basic Authentication" do
# before do
# @url = 'https://assets.c7.se/data/pinch/auth/pinch_test.zip'
# @file = 'data.json'
# @data = "{\"gem\":\"pinch\",\"authors\":[\"Peter Hellberg\",\"Edward Patel\"],\"github_url\":\"https://github.com/peterhellberg/pinch\"}\n"
# end

# it "should retrieve the contents of the file data.json with valid authentication" do
# VCR.use_cassette('valid_basic_auth') do
# data = Pinch.get @url, @file, 'pinch_test', 'thisisjustatest'
# assert_equal @data, data
# assert_equal 114, data.size
# end
# end

# it "should not retrieve the contents of the file data.json with invalid authentication" do
# VCR.use_cassette('invalid_basic_auth') do
# assert_raises(Net::HTTPClientException) do
# Pinch.get @url, @file, 'invalid_username', 'invalid_password'
# end
# end
# end
# end

describe "Pinch.file_list" do
it "should return a list with all the file names in the ZIP file" do
VCR.use_cassette('file_list') do
@url = 'http://memention.com/ericjohnson-canabalt-ios-ef43b7d.zip'

file_list = Pinch.file_list(@url)
file_list.size.must_equal 491
end
end
end

describe "Pinch.content_length" do
before do
@url = 'http://peterhellberg.github.io/pinch/test.zip'
end

it "should return the size of the ZIP file" do
VCR.use_cassette('content_length') do
Pinch.content_length(@url).must_equal 2516612
end
end

it "should raise an exception if the file doesn't exist" do
VCR.use_cassette('content_length_404') do
lambda {
Pinch.content_length(@url+'404')
}.must_raise Net::HTTPServerException
assert_equal 491, file_list.size
end
end
end
Expand Down