Class: Pinch

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

Overview

Author:

  • Peter Hellberg

  • Edward Patel

Constant Summary collapse

VERSION =
"0.1.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Pinch

Note:

You might want to use Pinch.get instead.

Initializes a new Pinch object

Parameters:

  • url (String)

    Full URL to the ZIP file



58
59
60
61
# File 'lib/pinch.rb', line 58

def initialize(url)
  @uri    = URI.parse(url)
  @files  = {}
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



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/pinch.rb', line 84

def content_length
  @content_length ||= begin
    response = prepared_connection.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)

    response['Content-Length'].to_i
  end
end

#file_listObject

Note:

You might want to use Pinch.file_list instead.



66
67
68
# File 'lib/pinch.rb', line 66

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')


77
78
79
# File 'lib/pinch.rb', line 77

def get(file_name)
  local_file(file_name)
end