Class: Chimps::Upload

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

Overview

An upload at Infochimps is a process attached to a dataset which carries a state.

A dataset typically does not have an “upload” associated with it but anyone authorized to update the dataset can create an upload for it. This upload object is empty by default. You can submit files or links to upload. When you’re done you can submit the entire upload for processing. You can view the status of the upload at any time.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Chimps::Utils::UsesCurl

#curl, #curl_params, #curl_program

Constructor Details

#initialize(slug) ⇒ Chimps::Upload

Create a new Upload for the dataset with the given slug or ID.



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

def initialize slug
  self.slug = slug
end

Instance Attribute Details

#slugObject

The slug or (ID) of the dataset to upload for



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

def slug
  @slug
end

Instance Method Details

#createChimps::Response

Create this upload on Infochimps.

Returns:



37
38
39
40
41
42
43
44
45
# File 'lib/chimps/workflows/upload.rb', line 37

def create
  follow_redirects_on :post, "/datasets/#{slug}/upload.json", :body => true do |response, request, result, &block|
    if response.code == 409
      response              # upload already exists
    else
      response.return!(request, result, &block)
    end
  end
end


78
79
80
# File 'lib/chimps/workflows/upload.rb', line 78

def create_links *links
  follow_redirects_on :put, "/datasets/#{slug}/upload.json", :body => { :upload => { :add_links    => links }}
end

#destroyObject



90
91
92
# File 'lib/chimps/workflows/upload.rb', line 90

def destroy
  follow_redirects_on :delete, "/datasets/#{slug}/upload.json"
end

#follow_redirects_on(method, url, options = {}, &block) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/chimps/workflows/upload.rb', line 98

def follow_redirects_on method, url, options={}, &block
  Request.new(url, {:sign => true}.merge(options)).send(method) do |response, request, result, &block|
    if [301, 302, 307].include?(response.code)
      response.follow_redirection(request, result, &block)
    else
      if response.code != 200 && block_given?
        response.return!(request, result, &block)
      else
        response.return!(request, result)
      end
    end
  end
end

#remove_files(*uuids) ⇒ Object



74
75
76
# File 'lib/chimps/workflows/upload.rb', line 74

def remove_files *uuids
  follow_redirects_on :put, "/datasets/#{slug}/upload.json", :body => { :upload => { :remove_files => uuids }}
end


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

def remove_links *uuids
  follow_redirects_on :put, "/datasets/#{slug}/upload.json", :body => { :upload => { :remove_links => uuids }}
end

#restartObject



94
95
96
# File 'lib/chimps/workflows/upload.rb', line 94

def restart
  follow_redirects_on :delete, "/datasets/#{slug}/upload.json", :query => { :restart => true }
end

#showChimps::Response

Show this upload.

Returns:



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

def show
  follow_redirects_on :get, "/datasets/#{slug}/upload.yaml"
end

#startObject



86
87
88
# File 'lib/chimps/workflows/upload.rb', line 86

def start
  follow_redirects_on :put, "/datasets/#{slug}/upload.json", :query => { :submit => true }
end

#update(params = {}) ⇒ Object



47
48
49
# File 'lib/chimps/workflows/upload.rb', line 47

def update params={}
  follow_redirects_on :put, "/datasets/#{slug}/upload.json", params
end

#upload_file(path, token) ⇒ Object

Raises:



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/chimps/workflows/upload.rb', line 61

def upload_file path, token
  token.parse
  p token
  raise UploadError.new("#{path} does not exist")                          unless File.exist?(path)
  raise UploadError.new("#{path} is a directory -- can only upload files") if File.directory?(path)
  params = %w[AWSAccessKeyId acl key policy success_action_status signature].map do |param|
    [param, token[param]]
  end
  params << ['file', '@' + path] # this is how you tell curl to upload a file
  Chimps.log.info("Uploading #{path} for dataset #{slug}")
  curl token['url'], :method => "POST", :params => params
end

#upload_files(*paths) ⇒ Object



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

def upload_files *paths
  paths.map { |p| File.expand_path(p) }.each do |path|
    upload_file(upload_token)
  end
end

#upload_tokenObject



57
58
59
# File 'lib/chimps/workflows/upload.rb', line 57

def upload_token
  follow_redirects_on :get, "/datasets/#{slug}/upload.json", :query => { :token => true }
end