Class: CrowdFlower::Base
- Inherits:
-
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')
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.
131
132
133
134
|
# File 'lib/crowdflower/base.rb', line 131
def initialize(new_connection = nil, last_res = nil)
@connection = new_connection
@last_response = last_res
end
|
Class Attribute Details
.default_connection ⇒ Object
Returns the value of attribute default_connection.
145
146
147
|
# File 'lib/crowdflower/base.rb', line 145
def default_connection
@default_connection
end
|
Instance Attribute Details
#last_response ⇒ Object
Returns the value of attribute last_response.
129
130
131
|
# File 'lib/crowdflower/base.rb', line 129
def last_response
@last_response
end
|
Class Method Details
.connect ⇒ Object
179
180
181
182
183
|
# File 'lib/crowdflower/base.rb', line 179
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
148
149
150
|
# File 'lib/crowdflower/base.rb', line 148
def self.connect!(key, domain_base = "https://api.crowdflower.com", version = 1)
connect_domain!(key, domain_base, version)
end
|
.connect_config!(opts) ⇒ Object
156
157
158
159
160
161
162
163
164
165
166
|
# File 'lib/crowdflower/base.rb', line 156
def self.connect_config!(opts)
= lambda do |arr|
arr.map { |k| opts[k] || opts[k.to_s] }.compact.first
end
key = .call([:key, :api_key])
domain_base = .call([:domain_base, :domain])
version = .call([:version]) || 1
ssl_port = .call([:ssl_port]) || 443
public_port = .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
152
153
154
|
# File 'lib/crowdflower/base.rb', line 152
def self.connect_domain!(key, domain_base, version = 1)
self.default_connection = Connection.new(key, domain_base, version)
end
|
.connection ⇒ Object
140
141
142
|
# File 'lib/crowdflower/base.rb', line 140
def self.connection
default_connection or (self != Base and superclass.connection)
end
|
.delete(*args) ⇒ Object
171
|
# File 'lib/crowdflower/base.rb', line 171
def self.delete(*args); connection.delete(*args); end
|
.get(*args) ⇒ Object
168
|
# File 'lib/crowdflower/base.rb', line 168
def self.get(*args); connection.get(*args); end
|
.post(*args) ⇒ Object
169
|
# File 'lib/crowdflower/base.rb', line 169
def self.post(*args); connection.post(*args); end
|
.put(*args) ⇒ Object
170
|
# File 'lib/crowdflower/base.rb', line 170
def self.put(*args); connection.put(*args); end
|
.verify_response(response) ⇒ Object
185
186
187
188
189
190
191
192
193
194
195
196
197
|
# File 'lib/crowdflower/base.rb', line 185
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
#connect ⇒ Object
173
174
175
176
177
|
# File 'lib/crowdflower/base.rb', line 173
def connect
unless connection
raise UsageError, "Please establish a connection using 'CrowdFlower.connect!'"
end
end
|
#connection ⇒ Object
136
137
138
|
# File 'lib/crowdflower/base.rb', line 136
def connection
@connection or self.class.connection
end
|