Class: Pinch

Inherits:
Object
  • Object
show all
Defined in:
lib/pinch.rb

Overview

Author:

  • Peter Hellberg

  • Edward Patel

Defined Under Namespace

Classes: RangeHeaderException, TooManyRedirects

Constant Summary collapse

VERSION =
"0.2.1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, redirects = 5) ⇒ Pinch

Note:

You might want to use Pinch.get instead.

Initializes a new Pinch object

Parameters:

  • url (String)

    Full URL to the ZIP file

  • redirects (Strin) (defaults to: 5)

    (Optional) Number of redirects to follow



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

#uriObject (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

Examples:


Pinch.content_length('http://peterhellberg.github.com/pinch/test.zip') #=> 2516612

Parameters:

  • url (String)

    Full URL to the ZIP file

Returns:

  • (Fixnum)

    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

Examples:


Pinch.file_list('http://peterhellberg.github.com/pinch/test.zip').first #=> "data.json"

Parameters:

  • url (String)

    Full URL to the ZIP file

Returns:

  • (Array)

    List of all the files in the ZIP archive



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!

Examples:


puts Pinch.get('http://peterhellberg.github.com/pinch/test.zip', 'data.json')

Parameters:

  • url (String)

    Full URL to the ZIP file

  • file_name (String)

    Name of the file inside the ZIP archive

Returns:

  • (String)

    File data, ready to be displayed/saved



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_lengthObject

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_listObject

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

Examples:


puts Pinch.new('http://peterhellberg.github.com/pinch/test.zip').get('data.json')


79
80
81
# File 'lib/pinch.rb', line 79

def get(file_name)
  local_file(file_name)
end