Class: Chimps::Upload
- 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
-
#slug ⇒ Object
The slug or (ID) of the dataset to upload for.
Instance Method Summary collapse
-
#create ⇒ Chimps::Response
Create this upload on Infochimps.
- #create_links(*links) ⇒ Object
- #destroy ⇒ Object
- #follow_redirects_on(method, url, options = {}, &block) ⇒ Object
-
#initialize(slug) ⇒ Chimps::Upload
constructor
Create a new Upload for the dataset with the given
slug
or ID. - #remove_files(*uuids) ⇒ Object
- #remove_links(*uuids) ⇒ Object
- #restart ⇒ Object
-
#show ⇒ Chimps::Response
Show this upload.
- #start ⇒ Object
- #update(params = {}) ⇒ Object
- #upload_file(path, token) ⇒ Object
- #upload_files(*paths) ⇒ Object
- #upload_token ⇒ Object
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
#slug ⇒ Object
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
#create ⇒ Chimps::Response
Create this upload on Infochimps.
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 |
#create_links(*links) ⇒ Object
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 |
#destroy ⇒ Object
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, ={}, &block Request.new(url, {:sign => true}.merge()).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 |
#remove_links(*uuids) ⇒ Object
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 |
#restart ⇒ Object
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 |
#show ⇒ Chimps::Response
Show this upload.
30 31 32 |
# File 'lib/chimps/workflows/upload.rb', line 30 def show follow_redirects_on :get, "/datasets/#{slug}/upload.yaml" end |
#start ⇒ Object
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
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.(p) }.each do |path| upload_file(upload_token) end end |
#upload_token ⇒ Object
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 |