Class: CrowdFlower::Base

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

Overview

Base class for Crowdflower api entities. Now instead of one global configuration, each instance could maintain its own connection. If no connection is available for an instance it will look for default class-level or global connection settings, see Base#connection method. You can set class-specific connection with Base.connect! call for example:

Job.connect!(api_key, development, version).

It is possible to use several api keys simultaneously either by providing each entity instance with a specific Connection object or by creating entity subclasses that have their own default connections for example:

CrowdFlower.connect!(default_api_key, development, version)
(JobSubclass = Class.new(Job)).connect!(custom_api_key, development, version)
job1 = Job.create('created with default api key')      
job2 = JobSubclass.create('created with custom api key')

Direct Known Subclasses

Job, Judgment, Order, Unit, Worker

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(new_connection = nil, last_res = nil) ⇒ Base

Returns a new instance of Base.



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

def initialize(new_connection = nil, last_res = nil)
  @connection = new_connection
  @last_response = last_res
end

Class Attribute Details

.default_connectionObject

Returns the value of attribute default_connection.



131
132
133
# File 'lib/crowdflower/base.rb', line 131

def default_connection
  @default_connection
end

Instance Attribute Details

#last_responseObject

Returns the value of attribute last_response.



115
116
117
# File 'lib/crowdflower/base.rb', line 115

def last_response
  @last_response
end

Class Method Details

.connectObject



165
166
167
168
169
# File 'lib/crowdflower/base.rb', line 165

def self.connect
  unless connection
    raise UsageError, "Please establish a connection using 'CrowdFlower.connect!'"
  end
end

.connect!(key, domain_base = "https://api.crowdflower.com", version = 1) ⇒ Object



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

def self.connect!(key, domain_base = "https://api.crowdflower.com", version = 1)
  connect_domain!(key, domain_base, version)
end

.connect_config!(opts) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
# File 'lib/crowdflower/base.rb', line 142

def self.connect_config!(opts)
  extract_option = lambda do |arr|
    arr.map { |k| opts[k] || opts[k.to_s] }.compact.first
  end
  key         = extract_option.call([:key, :api_key])
  domain_base = extract_option.call([:domain_base, :domain])
  version     = extract_option.call([:version])     || 1
  ssl_port    = extract_option.call([:ssl_port])    || 443
  public_port = extract_option.call([:public_port]) || 80
  self.default_connection = Connection.new(key, domain_base, version, ssl_port, public_port)
end

.connect_domain!(key, domain_base, version = 1) ⇒ Object



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

def self.connect_domain!(key, domain_base, version = 1)
  self.default_connection = Connection.new(key, domain_base, version)
end

.connectionObject



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

def self.connection
  default_connection or (self != Base and superclass.connection)
end

.delete(*args) ⇒ Object



157
# File 'lib/crowdflower/base.rb', line 157

def self.delete(*args); connection.delete(*args); end

.get(*args) ⇒ Object



154
# File 'lib/crowdflower/base.rb', line 154

def self.get(*args); connection.get(*args); end

.post(*args) ⇒ Object



155
# File 'lib/crowdflower/base.rb', line 155

def self.post(*args); connection.post(*args); end

.put(*args) ⇒ Object



156
# File 'lib/crowdflower/base.rb', line 156

def self.put(*args); connection.put(*args); end

.verify_response(response) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/crowdflower/base.rb', line 171

def self.verify_response(response)
  if response.respond_to?(:[])
    if error = (response["errors"] || response["error"])
      raise CrowdFlower::APIError.new(error)
    elsif warning = response["warning"]
      raise CrowdFlower::APIWarning.new(warning)
    end
  elsif response.response.kind_of? Net::HTTPUnauthorized
    raise CrowdFlower::APIError.new('message' => response.to_s)
  elsif (500...600).include?(response.code)
    raise CrowdFlower::APIError.new('message' => response.to_s)
  end
end

Instance Method Details

#connectObject



159
160
161
162
163
# File 'lib/crowdflower/base.rb', line 159

def connect
  unless connection
    raise UsageError, "Please establish a connection using 'CrowdFlower.connect!'"
  end
end

#connectionObject



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

def connection
  @connection or self.class.connection
end