Class: CrowdFlower::Job

Inherits:
Base
  • Object
show all
Defined in:
lib/crowdflower/job.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#last_response

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

connect, #connect, connect!, connect_config!, connect_domain!, connection, #connection, delete, get, post, put, verify_response

Constructor Details

#initialize(job_id, connection = nil, last_response = nil) ⇒ Job

Returns a new instance of Job.



6
7
8
9
10
# File 'lib/crowdflower/job.rb', line 6

def initialize(job_id, connection = nil, last_response = nil)
  super connection, last_response
  @id = job_id
  connect
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



4
5
6
# File 'lib/crowdflower/job.rb', line 4

def id
  @id
end

#resObject

Returns the value of attribute res.



3
4
5
# File 'lib/crowdflower/job.rb', line 3

def res
  @res
end

Class Method Details

.allObject



20
21
22
23
# File 'lib/crowdflower/job.rb', line 20

def self.all
  connect
  get(resource_uri)
end

.create(title) ⇒ Object

Creates a new empty Job in CrowdFlower.



41
42
43
44
45
46
# File 'lib/crowdflower/job.rb', line 41

def self.create(title)
  connect
  res = self.post("#{resource_uri}.json", :query => {:job => {:title => title } }, :headers => { "Content-Length" => "0" } )
  verify_response res
  self.new(res["id"], nil, res)
end

.resource_uriObject



16
17
18
# File 'lib/crowdflower/job.rb', line 16

def self.resource_uri
  "/jobs"
end

.upload(file, content_type, job = nil, opts = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/crowdflower/job.rb', line 25

def self.upload(file, content_type, job = nil, opts = {})
  connect
  
  job_uri = job ? "/#{job.kind_of?(Job) ? job.id : job}" : ""
  conn = job.kind_of?(Job) ? job.connection : self.connection
  upload_uri = "#{resource_uri}/#{job_uri}/upload".squeeze("/")
  res = conn.post(upload_uri, 
    :body => File.read(file), 
    :headers => {"content-type" => content_type},
    :query => opts)

  verify_response res
  job.kind_of?(Job) ? job : self.new(res["id"], conn, res)
end

Instance Method Details

#add_tags(tags) ⇒ Object



142
143
144
# File 'lib/crowdflower/job.rb', line 142

def add_tags(tags)
  connection.post("#{resource_uri}/#{@id}/tags", {:body => { :tags => tags } } ) 
end

#cancelObject



108
109
110
# File 'lib/crowdflower/job.rb', line 108

def cancel
  connection.get("#{resource_uri}/#{@id}/cancel")
end

#channelsObject



122
123
124
# File 'lib/crowdflower/job.rb', line 122

def channels
  connection.get("#{resource_uri}/#{@id}/channels")
end

#copy(opts = {}) ⇒ Object

Copies a job and optionally gold or all units. Parameters

opts: Hash
  gold: when set to true copies gold units
  all_units: when set to true copies all units


67
68
69
70
71
# File 'lib/crowdflower/job.rb', line 67

def copy(opts = {})
  res = connection.get("#{resource_uri}/#{@id}/copy", {:query => opts})
  self.class.verify_response res
  self.class.new(res["id"], nil, res)
end

#deleteObject



118
119
120
# File 'lib/crowdflower/job.rb', line 118

def delete
  connection.delete("#{resource_uri}/#{@id}.json")
end

#disable_channel(channel_name) ⇒ Object



130
131
132
# File 'lib/crowdflower/job.rb', line 130

def disable_channel(channel_name)
  connection.post("#{resource_uri}/#{@id}/disable_channel", {:body => { :channel_name => channel_name } } )
end

#download_csv(type = :full, filename = nil) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/crowdflower/job.rb', line 85

def download_csv(type = :full, filename = nil)
  filename ||= "#{type.to_s[0].chr}#{@id}.zip"
  res = connection.get("#{resource_uri}/#{@id}.csv", {:format => "zip", :query => {:type => type}})
  self.class.verify_response res
  puts "Status... #{res.code.inspect}"
  if res.code == 202
    puts "CSV Generating... Trying again in 10 seconds."
    Kernel.sleep 10
    download_csv(type, filename)
  else
    puts "CSV written to: #{File.expand_path(filename)}"
    File.open(filename, "w") {|f| f.puts res.body }
  end
end

#enable_channels(channels) ⇒ Object



126
127
128
# File 'lib/crowdflower/job.rb', line 126

def enable_channels(channels)
  connection.post("#{resource_uri}/#{@id}/channels", {:body => { :channels => channels } } )
end

#get(id = nil) ⇒ Object



48
49
50
# File 'lib/crowdflower/job.rb', line 48

def get(id = nil)
  connection.get("#{resource_uri}/#{@id || id}")
end

#legendObject



81
82
83
# File 'lib/crowdflower/job.rb', line 81

def legend
  connection.get("#{resource_uri}/#{@id}/legend")
end

#pauseObject



100
101
102
# File 'lib/crowdflower/job.rb', line 100

def pause
  connection.get("#{resource_uri}/#{@id}/pause")
end

#remove_tags(tags) ⇒ Object



146
147
148
# File 'lib/crowdflower/job.rb', line 146

def remove_tags(tags)
  connection.delete("#{resource_uri}/#{@id}/tags", {:body => { :tags => tags } } ) 
end

#resource_uriObject



12
13
14
# File 'lib/crowdflower/job.rb', line 12

def resource_uri
  Job.resource_uri
end

#resumeObject



104
105
106
# File 'lib/crowdflower/job.rb', line 104

def resume
  connection.get("#{resource_uri}/#{@id}/resume")
end

#statusObject



73
74
75
# File 'lib/crowdflower/job.rb', line 73

def status
  connection.get("#{resource_uri}/#{@id}/ping")
end

#tagsObject



134
135
136
# File 'lib/crowdflower/job.rb', line 134

def tags
  connection.get("#{resource_uri}/#{@id}/tags") 
end

#unitsObject

Returns an accessor for all the Units associated with this job. This enables you to do things like:

  • job.units.all

  • job.units.get [id]

  • job.units.create [data]



58
59
60
# File 'lib/crowdflower/job.rb', line 58

def units
  Unit.new(self)
end

#update(data) ⇒ Object



112
113
114
115
116
# File 'lib/crowdflower/job.rb', line 112

def update(data)
  response = connection.put("#{resource_uri}/#{@id}.json", {:body => { :job => data }, :headers => { "Content-Length" => "0" } } )
  self.class.verify_response(response)
  response
end

#update_tags(tags) ⇒ Object



138
139
140
# File 'lib/crowdflower/job.rb', line 138

def update_tags(tags)
  connection.put("#{resource_uri}/#{@id}/tags", {:body => { :tags => tags } } ) 
end

#upload(file, content_type) ⇒ Object



77
78
79
# File 'lib/crowdflower/job.rb', line 77

def upload(file, content_type)
  self.class.upload(file, content_type, self)
end