Class: Pinch
- Inherits:
-
Object
- Object
- Pinch
- Defined in:
- lib/pinch.rb
Overview
Defined Under Namespace
Classes: RangeHeaderException, TooManyRedirects
Constant Summary collapse
- VERSION =
"0.2.1"
Instance Attribute Summary collapse
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Class Method Summary collapse
-
.content_length(url) ⇒ Fixnum
Retrieve the size of the ZIP file.
-
.file_list(url) ⇒ Array
List of files inside the zip file.
-
.get(url, file_name) ⇒ String
Retrieve a file from inside a zip file, over the network!.
Instance Method Summary collapse
- #content_length ⇒ Object
- #file_list ⇒ Object
- #get(file_name) ⇒ Object
-
#initialize(url, redirects = 5) ⇒ Pinch
constructor
Initializes a new Pinch object.
Constructor Details
#initialize(url, redirects = 5) ⇒ Pinch
Note:
You might want to use Pinch.get instead.
Initializes a new Pinch object
59 60 61 62 63 |
# File 'lib/pinch.rb', line 59 def initialize(url, redirects = 5) @uri = URI.parse(url) @files = {} @redirects = redirects end |
Instance Attribute Details
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
10 11 12 |
# File 'lib/pinch.rb', line 10 def uri @uri end |
Class Method Details
.content_length(url) ⇒ Fixnum
Retrieve the size of the ZIP file
48 49 50 |
# File 'lib/pinch.rb', line 48 def self.content_length(url) new(url).content_length end |
.file_list(url) ⇒ Array
List of files inside the zip file
35 36 37 |
# File 'lib/pinch.rb', line 35 def self.file_list(url) new(url).file_list end |
.get(url, file_name) ⇒ String
Retrieve a file from inside a zip file, over the network!
22 23 24 |
# File 'lib/pinch.rb', line 22 def self.get(url, file_name) new(url).get(file_name) end |
Instance Method Details
#content_length ⇒ Object
Note:
You might want to use Pinch.content_length instead
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/pinch.rb', line 86 def content_length @content_length ||= begin response = connection(@uri).start { |http| http.head(@uri.path) } # Raise exception if the response code isn’t in the 2xx range response.error! unless response.kind_of?(Net::HTTPSuccess) # Raise exception if the server doesn’t support the Range header unless (response['Accept-Ranges'] or "").include?('bytes') raise RangeHeaderException, "Range HTTP header not supported on #{@uri.host}" end response['Content-Length'].to_i rescue Net::HTTPRetriableError => e @uri = URI.parse(e.response['Location']) if (@redirects -= 1) > 0 retry else raise TooManyRedirects, "Gave up at on #{@uri.host}" end end end |
#file_list ⇒ Object
Note:
You might want to use Pinch.file_list instead.
68 69 70 |
# File 'lib/pinch.rb', line 68 def file_list file_headers.keys end |
#get(file_name) ⇒ Object
Note:
You might want to use Pinch.get instead
79 80 81 |
# File 'lib/pinch.rb', line 79 def get(file_name) local_file(file_name) end |