pinch 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. data/Rakefile +1 -1
  2. data/lib/pinch.rb +49 -49
  3. data/spec/pinch_spec.rb +2 -2
  4. metadata +1 -1
data/Rakefile CHANGED
@@ -5,5 +5,5 @@ task :default => :test
5
5
  Rake::TestTask.new(:test) do |t|
6
6
  t.test_files = FileList['spec/*_spec.rb']
7
7
  t.ruby_opts = ['-rubygems'] if defined? Gem
8
- t.ruby_opts << '-I.'
8
+ t.ruby_opts << '-I. -w'
9
9
  end
@@ -3,13 +3,10 @@ require 'net/http'
3
3
  require 'zlib'
4
4
 
5
5
  class Pinch
6
- VERSION = "0.0.3"
6
+ VERSION = "0.0.4"
7
7
 
8
8
  attr_reader :uri
9
9
  attr_reader :file_name
10
- attr_reader :content_length
11
- attr_reader :central_directory
12
- attr_reader :file_headers
13
10
 
14
11
  def self.get(url, file_name)
15
12
  new(url).data(file_name)
@@ -20,29 +17,8 @@ class Pinch
20
17
  end
21
18
 
22
19
  def initialize(url)
23
- @uri = URI.parse(url)
24
- @files = {}
25
- end
26
-
27
- def file_headers
28
- @file_headers ||= begin
29
- raise RuntimeError, "Couldn’t find the central directory." if central_directory.nil?
30
-
31
- headers = {}
32
- tmp = central_directory
33
-
34
- begin
35
- cd = tmp.unpack('VvvvvvvVVVvvvvvVV')
36
- break if cd[1] == 0
37
-
38
- length = 46+cd[10]+cd[11]+cd[12]
39
- current_file_name = tmp[46...46+cd[10]]
40
- tmp = tmp[length..-1]
41
- headers[current_file_name] = cd
42
- end while true
43
-
44
- headers
45
- end
20
+ @uri = URI.parse(url)
21
+ @files = {}
46
22
  end
47
23
 
48
24
  def file_list
@@ -50,6 +26,24 @@ class Pinch
50
26
  end
51
27
 
52
28
  def data(file_name)
29
+ local_file(file_name)
30
+ end
31
+
32
+ private
33
+
34
+ def local_file(file_name)
35
+ #0 uint32 localFileHeaderSignature
36
+ #1 uint16 versionNeededToExtract
37
+ #2 uint16 generalPurposeBitFlag
38
+ #3 uint16 compressionMethod
39
+ #4 uint16 fileLastModificationTime
40
+ #5 uint16 fileLastModificationDate
41
+ #6 uint32 CRC32
42
+ #7 uint32 compressedSize
43
+ #8 uint32 uncompressedSize
44
+ #9 uint16 fileNameLength
45
+ #10 uint16 extraFieldLength
46
+
53
47
  raise Errno::ENOENT if file_headers[file_name].nil?
54
48
 
55
49
  req = Net::HTTP::Get.new(uri.path)
@@ -64,21 +58,6 @@ class Pinch
64
58
  http.request(req)
65
59
  end
66
60
 
67
- ###########################################################################
68
- # Local file header
69
- ###########################################################################
70
-
71
- #0 uint32 localFileHeaderSignature
72
- #1 uint16 versionNeededToExtract
73
- #2 uint16 generalPurposeBitFlag
74
- #3 uint16 compressionMethod
75
- #4 uint16 fileLastModificationTime
76
- #5 uint16 fileLastModificationDate
77
- #6 uint32 CRC32
78
- #7 uint32 compressedSize
79
- #8 uint32 uncompressedSize
80
- #9 uint16 fileNameLength
81
- #10 uint16 extraFieldLength
82
61
 
83
62
  local_file_header = res.body.unpack('VvvvvvVVVvv')
84
63
  file_data = res.body[30+local_file_header[9]+local_file_header[10]..-1]
@@ -94,6 +73,27 @@ class Pinch
94
73
  end
95
74
  end
96
75
 
76
+ def file_headers
77
+ @file_headers ||= begin
78
+ raise RuntimeError, "Couldn’t find the central directory." if central_directory.nil?
79
+
80
+ headers = {}
81
+ tmp = central_directory
82
+
83
+ begin
84
+ cd = tmp.unpack('VvvvvvvVVVvvvvvVV')
85
+ break if cd[1] == 0
86
+
87
+ length = 46+cd[10]+cd[11]+cd[12]
88
+ current_file_name = tmp[46...46+cd[10]]
89
+ tmp = tmp[length..-1]
90
+ headers[current_file_name] = cd
91
+ end while true
92
+
93
+ headers
94
+ end
95
+ end
96
+
97
97
  def central_directory
98
98
  #0 uint32 centralDirectoryFileHeaderSignature
99
99
  #1 uint16 versionMadeBy
@@ -131,13 +131,6 @@ class Pinch
131
131
  end
132
132
  end
133
133
 
134
- # Retrieve the content length of the file
135
- def content_length
136
- @content_length ||= Net::HTTP.start(@uri.host, @uri.port) { |http|
137
- http.head(@uri.path)
138
- }['Content-Length'].to_i
139
- end
140
-
141
134
  def end_of_central_directory_record
142
135
  #0 uint16 numberOfThisDisk;
143
136
  #1 uint16 diskWhereCentralDirectoryStarts;
@@ -165,4 +158,11 @@ class Pinch
165
158
  [hex.split("504b0506").last].pack("H*").unpack("vvvvVVv")
166
159
  end
167
160
  end
161
+
162
+ # Retrieve the content length of the file
163
+ def content_length
164
+ @content_length ||= Net::HTTP.start(@uri.host, @uri.port) { |http|
165
+ http.head(@uri.path)
166
+ }['Content-Length'].to_i
167
+ end
168
168
  end
@@ -13,7 +13,7 @@ describe Pinch do
13
13
 
14
14
  it "should return the contents of the file" do
15
15
  data = Pinch.get @url, @file
16
- data.must_match /Morphic graphics architecture/
16
+ data.must_match(/Morphic graphics architecture/)
17
17
  data.size.must_equal 26424
18
18
  end
19
19
  end
@@ -26,7 +26,7 @@ describe Pinch do
26
26
 
27
27
  it "should return the contents of the file" do
28
28
  data = Pinch.get @url, @file
29
- data.must_match /Daring Escape/
29
+ data.must_match(/Daring Escape/)
30
30
  data.size.must_equal 2288
31
31
  end
32
32
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: pinch
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.3
5
+ version: 0.0.4
6
6
  platform: ruby
7
7
  authors:
8
8
  - Peter Hellberg