Class: Chimps::Workflows::Upload::UploadToken

Inherits:
Object
  • Object
show all
Defined in:
lib/chimps/workflows/upload/token.rb

Overview

Encapsulates the process of obtaining an upload token for a dataset from Infochimps.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dataset, options = {}) ⇒ UploadToken

Instantiate a new UploadToken for the given dataset with the given fmt and pkg_fmt.

Parameters:

  • dataset (String, Integer)

    the ID or slug of the dataset to upload data for

  • fmt (String)

    the data format (csv, xls, tsv, &c.) of the data

  • pkg_fmt (String)

    the package format (zip, tar.bz2, tar.gz, &c.) of the data



30
31
32
33
34
# File 'lib/chimps/workflows/upload/token.rb', line 30

def initialize dataset, options={}
  @dataset = dataset
  @fmt     = options[:fmt]
  @pkg_fmt = options[:pkg_fmt]
end

Instance Attribute Details

#datasetObject

The ID or slug of the dataset for which to obtain an upload token.



11
12
13
# File 'lib/chimps/workflows/upload/token.rb', line 11

def dataset
  @dataset
end

#fmtObject

The format (csv, xls, tsv, &c.) of the data in the upload.



14
15
16
# File 'lib/chimps/workflows/upload/token.rb', line 14

def fmt
  @fmt
end

#pkg_fmtObject

The package format (zip, tar.bz2, &c.) of the data in the upload.



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

def pkg_fmt
  @pkg_fmt
end

#responseObject

The response from Infochimps to the request for an upload token.



22
23
24
# File 'lib/chimps/workflows/upload/token.rb', line 22

def response
  @response
end

Instance Method Details

#[](param) ⇒ Object

Delegate slicing to the returned response.



37
38
39
# File 'lib/chimps/workflows/upload/token.rb', line 37

def [] param
  response && response[param]
end

#bucketString

Parses the ‘url’ property of the response from Infochimps to determine the bucket name.

Returns:



68
69
70
# File 'lib/chimps/workflows/upload/token.rb', line 68

def bucket
  File.basename(response['url'])
end

#getObject

Make the request to get an upload token from Infochimps



56
57
58
59
60
61
62
# File 'lib/chimps/workflows/upload/token.rb', line 56

def get
  @response = Request.new(path, :params => params, :signed => true).get
  if response.error?
    response.print
    raise AuthenticationError.new("Unauthorized for an upload token for dataset #{dataset}")
  end
end

#paramsHash

Parameters passed to Infochimps to request an upload token.

Returns:



51
52
53
# File 'lib/chimps/workflows/upload/token.rb', line 51

def params
  { :package => { :fmt => fmt, :pkg_fmt => pkg_fmt } }
end

#pathString

The path on Infochimps to submit upload token requests to.

Returns:



44
45
46
# File 'lib/chimps/workflows/upload/token.rb', line 44

def path
  "/datasets/#{dataset}/packages/new.json"
end