Module: Crowdkit

Defined in:
lib/crowdkit.rb,
lib/crowdkit/api.rb,
lib/crowdkit/error.rb,
lib/crowdkit/client.rb,
lib/crowdkit/config.rb,
lib/crowdkit/version.rb,
lib/crowdkit/api/factory.rb,
lib/crowdkit/client/jobs.rb,
lib/crowdkit/client/poll.rb,
lib/crowdkit/client/units.rb,
lib/crowdkit/api/arguments.rb,
lib/crowdkit/client/account.rb,
lib/crowdkit/client/workers.rb,
lib/crowdkit/client/statuses.rb,
lib/crowdkit/client/worksets.rb,
lib/crowdkit/client/judgments.rb,
lib/crowdkit/api/request_methods.rb,
lib/crowdkit/api/response_wrapper.rb,
lib/crowdkit/middleware/raise_error.rb

Defined Under Namespace

Modules: Middleware Classes: API, BadGateway, BadRequest, Client, ClientError, Config, Conflict, Error, Forbidden, InternalServerError, MissingContentType, NotAcceptable, NotFound, NotImplemented, RequiredParams, ServerError, ServiceError, ServiceUnavailable, StatusError, TooManyRequests, Unauthorized, UnprocessableEntity, UnsupportedMediaType, UserError, ValidationError

Constant Summary collapse

VERSION =
File.read(File.expand_path(File.dirname(__FILE__) + "/../../VERSION")).strip

Class Method Summary collapse

Class Method Details

.configure(overrides = {}, &block) ⇒ Object



31
32
33
# File 'lib/crowdkit.rb', line 31

def self.configure(overrides={}, &block)
  @configuration = Config.new(overrides, &block)
end

.last_responseObject



23
24
25
# File 'lib/crowdkit.rb', line 23

def self.last_response
  @last_response
end

.last_response=(last_response) ⇒ Object



27
28
29
# File 'lib/crowdkit.rb', line 27

def self.last_response=(last_response)
  @last_response = last_response
end

.new(overrides = {}, &block) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/crowdkit.rb', line 14

def self.new(overrides={}, &block)
  #Use the global config if no overrides present
  if @configuration && overrides.empty? && !block_given?
    Client.new(@configuration)
  else
    Client.new(overrides, &block)
  end
end

.reset!Object



35
36
37
38
# File 'lib/crowdkit.rb', line 35

def self.reset!
  @last_response = nil
  @configuration = nil
end

.wait_on_status(resource, max_time_to_wait_in_seconds = 30) ⇒ Object

This takes a response from a resource that processes something in the background (i.e. a 202 response) and will ping the status API until it timesout an error is returned, or the background process is completed.

Raises:

  • (ArgumentError)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/crowdkit.rb', line 43

def self.wait_on_status(resource, max_time_to_wait_in_seconds = 30)
  raise ArgumentError, "This resource did not respond with a 202 status code and has no status to wait on." unless resource.last_response.status == 202
  progressbar = ProgressBar.create(total: 100)
  status = resource.current_status
  wait = 1

  max_time_to_wait_in_seconds.times do |i|
    break if status.state == "completed"
    raise StatusError.new(status) if ["failed", "killed"].include?(status.state)

    #increase wait time to 5 seconds after 5 seconds
    wait = 5 if i == 5
    #increase wait time to 10 seconds after 30 seconds
    wait = 10 if i == 30

    if i % wait == 0
      percents = status.percent_complete.to_i
      progressbar.progress = percents if percents > 0
      sleep(wait)
      status = resource.current_status
    end
  end

  raise StatusError.new(status, true) unless status.state == "completed"
  progressbar.finish
  status
end