Class: Chimps::Download

Inherits:
Object show all
Includes:
Utils::UsesCurl
Defined in:
lib/chimps/workflows/download.rb

Overview

A download is composted of an initial POST request which obtains a signed and expiring token from Infochimps followed by a GET to the URL provided in the token.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::UsesCurl

#curl, #curl_params, #curl_program

Constructor Details

#initialize(dataset) ⇒ Download

Create a new download for the dataset named by the given slug or ID.

Parameters:



18
19
20
# File 'lib/chimps/workflows/download.rb', line 18

def initialize dataset
  self.dataset = dataset
end

Instance Attribute Details

#datasetObject

The slug or (ID) of the dataset to download



9
10
11
# File 'lib/chimps/workflows/download.rb', line 9

def dataset
  @dataset
end

Instance Method Details

#download(path) ⇒ Integer

Download data to path.

If path is a directory then the resulting file will be put there with a basename determined sensibly from signed_url. Otherwise it will be placed at path itself.

Parameters:

Returns:

  • (Integer)

    the exit code of the curl command used to download the data



30
31
32
33
34
35
36
# File 'lib/chimps/workflows/download.rb', line 30

def download path
  if File.directory?(path)
    basename = File.basename(signed_url).split('?').first
    path     = File.join(path, basename)
  end
  curl signed_url, :output => path
end

#signed_urlString

Return the signed URL as parsed from the download token.

Returns:

  • (String)

    the token’s signed URL

Raises:



67
68
69
70
71
# File 'lib/chimps/workflows/download.rb', line 67

def signed_url
  token.parse
  raise Error.new("Malformed download token received from Infochimps") unless token['download_token'].is_a?(Hash) && token['download_token']['signed_url']
  token['download_token']['signed_url']
end

#tokenChimps::Response

A download token from Infochimps containing a signed URL from which data can be downloaded.

Returns:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/chimps/workflows/download.rb', line 49

def token
  @token ||= token_request.post do |response, request, result, &block|
    case response.code
    when 301, 302, 307
      response.follow_redirection(request, result, &block)
    when 401
      raise Error.new("Not authorized to download #{dataset}")
    when 200
      response.return!(request, result, &block)
    else
      raise Error.new("Could not obtain download token from Infochimps")
    end
  end
end

#token_requestChimps::Request

The request for obtaining a download token from Infochimps.

Returns:



41
42
43
# File 'lib/chimps/workflows/download.rb', line 41

def token_request
  @token_request ||= Request.new("/download_tokens", :body => {:download_token => {:dataset_id => dataset}}, :sign_if_possible => true)
end