Class: Chimps::Workflows::Up

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

Overview

Uploads data to Infochimps by first asking for authorization, creating an archive, obtaining a token, uploading data, and notifing Infochimps.

A helper object from Chimps::Workflows::Upload is delegated to for each step:

  • authorization & obtaining a token: Chimps::Workflows::Upload::UploadToken

  • creating an archive: Chimps::Workflows::Upload::Bundler

  • uploading data: Chimps::Workflows::Upload::Uploader

  • notifying Infochimps: Chimps::Workflows::Upload::Notifier

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Up

Create a new Uploader from the given parameters.

If :fmt is provided it will be used as the data format to annotate the upload with. If not, Chimps will try to guess.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • dataset (String, Integer)

    the ID or handle of the dataset to which data should be uploaded

  • paths (Array<String>)

    the paths to aggregate and upload

  • archive (String, IMW::Resource) — default: IMW::Workflows::Downloader#default_archive_path

    the path to the archive to create

  • fmt (String)

    the data format to annotate the upload with



50
51
52
53
54
55
# File 'lib/chimps/workflows/up.rb', line 50

def initialize options={}
  self.dataset = options[:dataset] or raise PackagingError.new("Must provide the ID or handle of a dataset to upload data to.")
  self.paths   = options[:paths]
  self.archive = options[:archive]        
  self.fmt     = options[:fmt]
end

Instance Attribute Details

#archiveObject

The path to the archive to create when uploading.



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

def archive
  @archive
end

#datasetObject

The ID or handle of the dataset to download.



27
28
29
# File 'lib/chimps/workflows/up.rb', line 27

def dataset
  @dataset
end

#fmtObject

The format to annotate the upload with.



34
35
36
# File 'lib/chimps/workflows/up.rb', line 34

def fmt
  @fmt
end

#pathsObject

An array of paths to files and directories to package into an archive.



31
32
33
# File 'lib/chimps/workflows/up.rb', line 31

def paths
  @paths
end

Instance Method Details

#ask_for_token!Object

Obtain an upload token from Infochimps.

Delegates to Chimps::Workflows::Upload::UploadToken



128
129
130
# File 'lib/chimps/workflows/up.rb', line 128

def ask_for_token!
  upload_token.get
end

#authorization_tokenChimps::Workflows::Upload::UploadToken

The token authorizing an upload.



75
76
77
# File 'lib/chimps/workflows/up.rb', line 75

def authorization_token
  @authorization_token ||= Chimps::Workflows::Upload::UploadToken.new(dataset)
end

#authorize_for_upload!Object

Authorize the Chimps user for this upload.

Delegates to Chimps::Workflows::Upload::UploadToken



114
115
116
# File 'lib/chimps/workflows/up.rb', line 114

def authorize_for_upload!
  authorization_token.get
end

#bundle!Object

Bundle the data together.

Delegates to Chimps::Workflows::Upload::Bundler



121
122
123
# File 'lib/chimps/workflows/up.rb', line 121

def bundle!
  bundler.bundle!
end

#bundlerChimps::Workflows::Upload::Bundler

The bundler that will aggregate data for the upload.



82
83
84
# File 'lib/chimps/workflows/up.rb', line 82

def bundler
  @bundler ||= Chimps::Workflows::Upload::Bundler.new(dataset, paths, :fmt => fmt, :archive => archive)
end

#execute!Object

Upload data to Infochimps by first asking for authorization, creating an archive, obtaining a token, uploading data, and notifing Infochimps.



60
61
62
63
64
65
66
# File 'lib/chimps/workflows/up.rb', line 60

def execute!
  authorize_for_upload!
  bundle!
  ask_for_token!
  upload!
  notify_infochimps!
end

#notifierChimps::Workflows::Upload::Notifer

The notifier that will inform Infochimps of the new data.

Returns:

  • (Chimps::Workflows::Upload::Notifer)


103
104
105
# File 'lib/chimps/workflows/up.rb', line 103

def notifier
  @notifier ||= Chimps::Workflows::Upload::Notifier.new(upload_token, bundler)
end

#notify_infochimps!Chimps::Response

Make a final POST request to Infochimps, creating the final resource.

Returns:



143
144
145
# File 'lib/chimps/workflows/up.rb', line 143

def notify_infochimps!
  notifier.post
end

#upload!Object

Upload the data to Infochimps.

Delegates to Chimps::Workflows::Upload::Uploader



135
136
137
# File 'lib/chimps/workflows/up.rb', line 135

def upload!
  uploader.upload!
end

#upload_tokenChimps::Workflows::Upload::UploadToken

The token consumed for an upload.



89
90
91
# File 'lib/chimps/workflows/up.rb', line 89

def upload_token
  @upload_token ||= Chimps::Workflows::Upload::UploadToken.new(dataset, :fmt => bundler.fmt, :pkg_fmt => bundler.pkg_fmt)
end

#uploaderChimps::Workflows::Upload::Uploader

The uploader that will actually send data to Infochimps.



96
97
98
# File 'lib/chimps/workflows/up.rb', line 96

def uploader
  @uploader ||= Chimps::Workflows::Upload::Uploader.new(upload_token, bundler)
end