Class: Pinch

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

Overview

Author:

  • Peter Hellberg

  • Edward Patel

Defined Under Namespace

Classes: TooManyRedirects

Constant Summary collapse

VERSION =
"0.3.3"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, user = nil, pass = nil, redirects = 5) ⇒ Pinch

Note:

You might want to use Pinch.get instead.

Initializes a new Pinch object

Parameters:

  • url (String or Hash)

    Full URL to the ZIP file or hash with different URLs for HTTP verbs, e.g.

    :head => 'my-url-signed-for-head-verb'
    :get => 'my-url-signed-for-get-verb'
    

  • user (String) (defaults to: nil)

    (Optional) Username for Basic Authentication

  • pass (String) (defaults to: nil)

    (Optional) Password for Basic Authentication

  • redirects (Fixnum) (defaults to: 5)

    (Optional) Number of redirects to follow



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_uriObject (readonly)

Returns the value of attribute get_uri.



11
12
13
# File 'lib/pinch.rb', line 11

def get_uri
  @get_uri
end

#passObject (readonly)

Returns the value of attribute pass.



11
12
13
# File 'lib/pinch.rb', line 11

def pass
  @pass
end

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

Examples:


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

Parameters:

  • url (String)

    Full URL to the ZIP file

  • user (String) (defaults to: nil)

    (Optional) Username for Basic Authentication

  • pass (String) (defaults to: nil)

    (Optional) Password for Basic Authentication

Returns:

  • (Fixnum)

    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

Examples:


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

Parameters:

  • url (String)

    Full URL to the ZIP file

  • user (String) (defaults to: nil)

    (Optional) Username for Basic Authentication

  • pass (String) (defaults to: nil)

    (Optional) Password for Basic Authentication

Returns:

  • (Array)

    List of all the files in the ZIP archive



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!

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

  • user (String) (defaults to: nil)

    (Optional) Username for Basic Authentication

  • pass (String) (defaults to: nil)

    (Optional) Password for Basic Authentication

Returns:

  • (String)

    File data, ready to be displayed/saved



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

Examples:


puts Pinch.new('http://code.mrgossett.com/pinch_test.zip').auth('pinch_test','thisisjustatest').get('data.json')

Parameters:

  • username (String)

    (Optional) Username for Basic Authentication

  • password (String)

    (Optional) Password for Basic Authentication

Returns:

  • (Pinch)

    Returns self to support chained calls



96
97
98
99
100
101
# File 'lib/pinch.rb', line 96

def auth(username, password)
  @user = username
  @pass = password

  return self
end

#content_lengthObject

Note:

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_listObject

Note:

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

Note:

You might want to use Pinch.get instead

Examples:


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


117
118
119
# File 'lib/pinch.rb', line 117

def get(file_name, &block)
  local_file(file_name, &block)
end