dolores-ruby-crowdflower 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Dolores Labs
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,21 @@
1
+ = ruby-crowdflower
2
+
3
+ A toolkit for interacting with CrowdFlower via the REST API.
4
+
5
+ This is alpha software in its most nascent stages, and is not
6
+ yet ready for use with the current version of CrowdFlower.
7
+
8
+ == Note on Patches/Pull Requests
9
+
10
+ * Fork the project.
11
+ * Make your feature addition or bug fix.
12
+ * Add tests for it. This is important so I don't break it in a
13
+ future version unintentionally.
14
+ * Commit, do not mess with rakefile, version, or history.
15
+ (if you want to have your own version, that is fine but
16
+ bump version in a commit by itself I can ignore when I pull)
17
+ * Send me a pull request. Bonus points for topic branches.
18
+
19
+ == Copyright
20
+
21
+ Copyright (c) 2009 Dolores Labs. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,60 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "ruby-crowdflower"
8
+ gem.summary = %Q{a toolkit for the CrowdFlower API}
9
+ gem.description = <<-EOF
10
+ A toolkit for interacting with CrowdFlower via the REST API.
11
+
12
+ This is alpha software in its most nascent stages, and is not
13
+ yet ready for use with the current version of CrowdFlower.
14
+
15
+ EOF
16
+ gem.email = "brian@doloreslabs.com"
17
+ gem.homepage = "http://github.com/dolores/ruby-crowdflower"
18
+ gem.authors = ["Brian P O'Rourke", "Chris Van Pelt"]
19
+ gem.add_dependency 'httparty', '>= 0.4.3'
20
+ end
21
+
22
+ rescue LoadError
23
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
24
+ end
25
+
26
+ require 'spec/rake/spectask'
27
+ Spec::Rake::SpecTask.new(:spec) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.spec_files = FileList['spec/**/*_spec.rb']
30
+ end
31
+
32
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
33
+ spec.libs << 'lib' << 'spec'
34
+ spec.pattern = 'spec/**/*_spec.rb'
35
+ spec.rcov = true
36
+ end
37
+
38
+ task :default => :spec
39
+
40
+ task :refresh_builder => [:build] do
41
+ cp "pkg/ruby-crowdflower-#{File.read("VERSION").strip}.gem", "../builder/gems/cache/"
42
+ rm_rf "../builder/gems/gems/ruby-crowdflower-#{File.read("VERSION").strip}/"
43
+ `cd ../builder && bin/thor merb:gem:redeploy`
44
+ end
45
+
46
+ require 'rake/rdoctask'
47
+ Rake::RDocTask.new do |rdoc|
48
+ if File.exist?('VERSION.yml')
49
+ config = YAML.load(File.read('VERSION.yml'))
50
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
51
+ else
52
+ version = ""
53
+ end
54
+
55
+ rdoc.rdoc_dir = 'rdoc'
56
+ rdoc.title = "ruby-crowdflower #{version}"
57
+ rdoc.rdoc_files.include('README*')
58
+ rdoc.rdoc_files.include('lib/**/*.rb')
59
+ end
60
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,7 @@
1
+ require 'httparty'
2
+ require 'ruby-crowdflower/patches/httparty'
3
+ require 'ruby-crowdflower/base'
4
+ require 'ruby-crowdflower/job'
5
+ require 'ruby-crowdflower/unit'
6
+ require 'ruby-crowdflower/judgment'
7
+ require 'ruby-crowdflower/order'
@@ -0,0 +1,27 @@
1
+ module CrowdFlower
2
+ @@key = nil
3
+ @@domain = nil
4
+
5
+ module Defaults
6
+ def self.included(base)
7
+ base.send :include, HTTParty
8
+ base.send :headers, "accept" => "application/json"
9
+ base.send :format, :json
10
+ end
11
+ end
12
+
13
+ def self.connect!(key, development = false)
14
+ @@domain = development ? "http://api.localhost:4000/v1" : "https://api.crowdflower.com/v1"
15
+ @@key = key
16
+ end
17
+
18
+ def self.with_domain(path)
19
+ raise "Please establish a connection using 'CrowdFlower.connect!'" unless @@domain
20
+ @@domain + path
21
+ end
22
+
23
+ def self.key
24
+ raise "Please establish a connection using 'CrowdFlower.connect!'" unless @@key
25
+ {:key => @@key}
26
+ end
27
+ end
@@ -0,0 +1,81 @@
1
+ module CrowdFlower
2
+ class Job
3
+ include Defaults
4
+ attr_reader :id
5
+
6
+ def initialize(job_id)
7
+ @id = job_id
8
+ Job.base_uri CrowdFlower.with_domain("/jobs")
9
+ Job.default_params CrowdFlower.key
10
+ end
11
+
12
+ def self.all
13
+ Job.get("/")
14
+ end
15
+
16
+ def get(id = nil)
17
+ Job.get("/#{@id || id}")
18
+ end
19
+
20
+ #Copies a job and optionally gold or all units.
21
+ #Parameters
22
+ # opts: Hash
23
+ # gold: when set to true copies gold units
24
+ # all_units: when set to true copies all units
25
+ def copy(opts = {})
26
+ Job.get("/#{@id}/copy", {:query => opts})
27
+ end
28
+
29
+ def status
30
+ Job.get("/#{@id}/ping")
31
+ end
32
+
33
+ def upload(file, content_type)
34
+ Job.upload(file, content_type, @id)
35
+ end
36
+
37
+ def legend
38
+ Job.get("/#{@id}/legend")
39
+ end
40
+
41
+ def self.upload(file, content_type, job_id = nil)
42
+ job_id = "/#{job_id}" unless job_id.nil?
43
+ Job.post("#{job_id}/upload",
44
+ :body => File.read(file),
45
+ :headers => custom_content_type(content_type))
46
+ end
47
+
48
+ def download_csv(full = true, filename = nil)
49
+ filename ||= "#{full ? "f" : "a"}#{@id}.csv"
50
+ res = Job.get("/#{@id}.csv", {:format => :csv, :query => {:full => full}})
51
+ puts "Status... #{res.code.inspect}"
52
+ if res.code == 202
53
+ puts "CSV Generating... Trying again in 10 seconds."
54
+ Kernel.sleep 10
55
+ download_csv(full, filename)
56
+ else
57
+ puts "CSV written to: #{File.expand_path(filename)}"
58
+ File.open(filename, "w") {|f| f.puts res.body }
59
+ end
60
+ end
61
+
62
+ def pause
63
+ Job.get("/#{@id}/pause")
64
+ end
65
+
66
+ def resume
67
+ Job.get("/#{@id}/resume")
68
+ end
69
+
70
+ def cancel
71
+ Job.get("/#{@id}/cancel")
72
+ end
73
+
74
+ private
75
+
76
+ def self.custom_content_type(content_type)
77
+ #To preserve the accept header we are forced to merge the defaults back in...
78
+ Job.default_options[:headers].merge({"content-type" => content_type})
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,21 @@
1
+ module CrowdFlower
2
+ class Judgment
3
+ include Defaults
4
+ attr_reader :job
5
+
6
+ def initialize(job)
7
+ @job = job
8
+ Judgment.base_uri CrowdFlower.with_domain("/jobs/#{@job.id}/judgments")
9
+ Judgment.default_params CrowdFlower.key
10
+ end
11
+
12
+ #Pull every judgment
13
+ def all(page = 1, limit = 1000)#full = true
14
+ Judgment.get("", {:query => {:limit => limit, :page => page}})
15
+ end
16
+
17
+ def get(id)
18
+ Judgment.get("/#{id}")
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,16 @@
1
+ module CrowdFlower
2
+ class Order
3
+ include Defaults
4
+ attr_reader :job
5
+
6
+ def initialize(job)
7
+ @job = job
8
+ Order.base_uri CrowdFlower.with_domain("/jobs/#{@job.id}/orders")
9
+ Order.default_params CrowdFlower.key
10
+ end
11
+
12
+ def debit(percentage = 100, channels = ["amt"])
13
+ Order.post("", {:body => {:percentage => percentage, :channels => channels}})
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,24 @@
1
+ module HTTParty
2
+ class Request
3
+ def handle_response(response)
4
+ case response
5
+ when Net::HTTPRedirection
6
+ options[:limit] -= 1
7
+
8
+ self.http_method = Net::HTTP::Get
9
+ self.path = response['location']
10
+
11
+ #Remove post data
12
+ options[:body] = ""
13
+ options[:headers].delete('content-type')
14
+ @message = parse_response(response.body.to_s) rescue nil
15
+ perform
16
+ else
17
+ parsed_response = parse_response(response.body.to_s)
18
+ Response.new(parsed_response, response.body.to_s, response.code, @message || response.message, response.to_hash)
19
+ end
20
+ rescue
21
+ puts response.body.to_s
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,32 @@
1
+ module CrowdFlower
2
+ class Unit
3
+ include Defaults
4
+ attr_reader :job
5
+
6
+ def initialize(job)
7
+ @job = job
8
+ Unit.base_uri CrowdFlower.with_domain("/jobs/#{@job.id}/units")
9
+ Unit.default_params CrowdFlower.key
10
+ end
11
+
12
+ def all(page = 1, limit = 1000)
13
+ Unit.get("", {:query => {:limit => limit, :page => page}})
14
+ end
15
+
16
+ def get(id)
17
+ Unit.get("/#{id}")
18
+ end
19
+
20
+ def create(data, gold = nil)
21
+ Unit.post("", {:query => {:unit => {:data => data.to_json, :golden => gold}}})
22
+ end
23
+
24
+ def copy(unit_id, job_id, data = {})
25
+ Unit.get("/#{unit_id}/copy", {:query => {:unit => {:job_id => job_id, :data => data}}})
26
+ end
27
+
28
+ def split(on, with = " ")
29
+ Unit.get("/split", {:query => {:on => on, :with => with}})
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,64 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{ruby-crowdflower}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Brian P O'Rourke", "Chris Van Pelt"]
12
+ s.date = %q{2009-09-14}
13
+ s.description = %q{A toolkit for interacting with CrowdFlower via the REST API.
14
+
15
+ This is alpha software in its most nascent stages, and is not
16
+ yet ready for use with the current version of CrowdFlower.
17
+
18
+ }
19
+ s.email = %q{brian@doloreslabs.com}
20
+ s.extra_rdoc_files = [
21
+ "LICENSE",
22
+ "README.rdoc"
23
+ ]
24
+ s.files = [
25
+ ".document",
26
+ ".gitignore",
27
+ "LICENSE",
28
+ "README.rdoc",
29
+ "Rakefile",
30
+ "VERSION",
31
+ "lib/ruby-crowdflower.rb",
32
+ "lib/ruby-crowdflower/base.rb",
33
+ "lib/ruby-crowdflower/job.rb",
34
+ "lib/ruby-crowdflower/judgment.rb",
35
+ "lib/ruby-crowdflower/order.rb",
36
+ "lib/ruby-crowdflower/patches/httparty.rb",
37
+ "lib/ruby-crowdflower/unit.rb",
38
+ "ruby-crowdflower.gemspec",
39
+ "spec/ruby-crowdflower_spec.rb",
40
+ "spec/spec_helper.rb"
41
+ ]
42
+ s.homepage = %q{http://github.com/dolores/ruby-crowdflower}
43
+ s.rdoc_options = ["--charset=UTF-8"]
44
+ s.require_paths = ["lib"]
45
+ s.rubygems_version = %q{1.3.5}
46
+ s.summary = %q{a toolkit for the CrowdFlower API}
47
+ s.test_files = [
48
+ "spec/ruby-crowdflower_spec.rb",
49
+ "spec/spec_helper.rb"
50
+ ]
51
+
52
+ if s.respond_to? :specification_version then
53
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
54
+ s.specification_version = 3
55
+
56
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
57
+ s.add_runtime_dependency(%q<httparty>, [">= 0.4.3"])
58
+ else
59
+ s.add_dependency(%q<httparty>, [">= 0.4.3"])
60
+ end
61
+ else
62
+ s.add_dependency(%q<httparty>, [">= 0.4.3"])
63
+ end
64
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe CrowdFlower do
4
+ it "has examples" do
5
+ pending "they need to be ported to the gem package"
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'rubygems'
4
+ require 'ruby-crowdflower'
5
+ require 'spec'
6
+ require 'spec/autorun'
7
+
8
+ Spec::Runner.configure do |config|
9
+
10
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dolores-ruby-crowdflower
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Brian P O'Rourke
8
+ - Chris Van Pelt
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-09-14 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: httparty
18
+ type: :runtime
19
+ version_requirement:
20
+ version_requirements: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 0.4.3
25
+ version:
26
+ description: A toolkit for interacting with CrowdFlower via the REST API. This is alpha software in its most nascent stages, and is not yet ready for use with the current version of CrowdFlower.
27
+ email: brian@doloreslabs.com
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - LICENSE
34
+ - README.rdoc
35
+ files:
36
+ - .document
37
+ - .gitignore
38
+ - LICENSE
39
+ - README.rdoc
40
+ - Rakefile
41
+ - VERSION
42
+ - lib/ruby-crowdflower.rb
43
+ - lib/ruby-crowdflower/base.rb
44
+ - lib/ruby-crowdflower/job.rb
45
+ - lib/ruby-crowdflower/judgment.rb
46
+ - lib/ruby-crowdflower/order.rb
47
+ - lib/ruby-crowdflower/patches/httparty.rb
48
+ - lib/ruby-crowdflower/unit.rb
49
+ - ruby-crowdflower.gemspec
50
+ - spec/ruby-crowdflower_spec.rb
51
+ - spec/spec_helper.rb
52
+ has_rdoc: false
53
+ homepage: http://github.com/dolores/ruby-crowdflower
54
+ licenses:
55
+ post_install_message:
56
+ rdoc_options:
57
+ - --charset=UTF-8
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: "0"
71
+ version:
72
+ requirements: []
73
+
74
+ rubyforge_project:
75
+ rubygems_version: 1.3.5
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: a toolkit for the CrowdFlower API
79
+ test_files:
80
+ - spec/ruby-crowdflower_spec.rb
81
+ - spec/spec_helper.rb