Class: Pinch
- Inherits:
-
Object
- Object
- Pinch
- Defined in:
- lib/pinch.rb
Overview
Defined Under Namespace
Classes: TooManyRedirects
Constant Summary collapse
- VERSION =
"0.3.3"
Instance Attribute Summary collapse
-
#get_uri ⇒ Object
readonly
Returns the value of attribute get_uri.
-
#pass ⇒ Object
readonly
Returns the value of attribute pass.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Class Method Summary collapse
-
.content_length(url, user = nil, pass = nil) ⇒ Fixnum
Retrieve the size of the ZIP file.
-
.file_list(url, user = nil, pass = nil) ⇒ Array
List of files inside the zip file.
-
.get(url, file_name, user = nil, pass = nil, &block) ⇒ String
Retrieve a file from inside a zip file, over the network!.
Instance Method Summary collapse
-
#auth(username, password) ⇒ Pinch
Set Username and Password for Basic Authentication.
- #content_length ⇒ Object
- #file_list ⇒ Object
- #get(file_name, &block) ⇒ Object
-
#initialize(url, user = nil, pass = nil, redirects = 5) ⇒ Pinch
constructor
Initializes a new Pinch object.
Constructor Details
#initialize(url, user = nil, pass = nil, redirects = 5) ⇒ Pinch
You might want to use Pinch.get instead.
Initializes a new Pinch object
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/pinch.rb', line 72 def initialize(url, user = nil, pass = nil, redirects = 5) if url.respond_to? :fetch @get_uri = URI.parse(url.fetch(:get)) @head_uri = URI.parse(url.fetch(:head)) else @get_uri = @head_uri = URI.parse(url) end @user = user @pass = pass @files = {} @redirects = redirects end |
Instance Attribute Details
#get_uri ⇒ Object (readonly)
Returns the value of attribute get_uri.
11 12 13 |
# File 'lib/pinch.rb', line 11 def get_uri @get_uri end |
#pass ⇒ Object (readonly)
Returns the value of attribute pass.
11 12 13 |
# File 'lib/pinch.rb', line 11 def pass @pass end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
11 12 13 |
# File 'lib/pinch.rb', line 11 def user @user end |
Class Method Details
.content_length(url, user = nil, pass = nil) ⇒ Fixnum
Retrieve the size of the ZIP file
55 56 57 |
# File 'lib/pinch.rb', line 55 def self.content_length(url, user = nil, pass = nil) new(url, user, pass).content_length end |
.file_list(url, user = nil, pass = nil) ⇒ Array
List of files inside the zip file
40 41 42 |
# File 'lib/pinch.rb', line 40 def self.file_list(url, user = nil, pass = nil) new(url, user, pass).file_list end |
.get(url, file_name, user = nil, pass = nil, &block) ⇒ String
Retrieve a file from inside a zip file, over the network!
25 26 27 |
# File 'lib/pinch.rb', line 25 def self.get(url, file_name, user = nil, pass = nil, &block) new(url, user, pass).get(file_name, &block) end |
Instance Method Details
#auth(username, password) ⇒ Pinch
Set Username and Password for Basic Authentication
96 97 98 99 100 101 |
# File 'lib/pinch.rb', line 96 def auth(username, password) @user = username @pass = password return self end |
#content_length ⇒ Object
You might want to use Pinch.content_length instead
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/pinch.rb', line 124 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 |
#file_list ⇒ Object
You might want to use Pinch.file_list instead.
106 107 108 |
# File 'lib/pinch.rb', line 106 def file_list file_headers.keys end |
#get(file_name, &block) ⇒ Object
You might want to use Pinch.get instead
117 118 119 |
# File 'lib/pinch.rb', line 117 def get(file_name, &block) local_file(file_name, &block) end |