Class: RTurk::Operation

Inherits:
Object
  • Object
show all
Defined in:
lib/rturk/operation.rb

Direct Known Subclasses

ApproveAssignment, AssignQualification, BlockWorker, CreateQualificationType, DisableHIT, DisposeHIT, DisposeQualificationType, ExtendHIT, ForceExpireHIT, GetAccountBalance, GetAssignmentsForHIT, GetBonusPayments, GetHIT, GetQualificationType, GetQualificationsForQualificationType, GetReviewableHITs, GrantBonus, NotifyWorkers, RegisterHITType, RejectAssignment, RevokeQualification, SearchHITs, SendTestEventNotification, SetHITTypeNotification, UnblockWorker, UpdateQualificationType

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Operation) initialize(opts = {}) {|_self| ... }

###################### # Instance Methods ### ######################

Yields:

  • (_self)

Yield Parameters:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rturk/operation.rb', line 34

def initialize(opts = {})
  opts.each_pair do |k,v|
    if self.respond_to?("#{k.to_sym}=")
      self.send "#{k}=".to_sym, v
    elsif v.is_a?(Array)
      v.each do |a|
        (self.send k.to_s).send a[0].to_sym, a[1]
      end
    elsif self.respond_to?(k.to_sym)
      self.send k.to_sym, v
    end
  end
  yield(self) if block_given?
  self
end

Class Method Details

+ (Object) create(opts = {}, &blk)



23
24
25
26
# File 'lib/rturk/operation.rb', line 23

def create(opts = {}, &blk)
  hit = self.new(opts, &blk)
  response = hit.request
end

+ (Object) default_params



6
7
8
# File 'lib/rturk/operation.rb', line 6

def default_params
  @default_params ||= {}
end

+ (Object) require_params(*args)



14
15
16
17
# File 'lib/rturk/operation.rb', line 14

def require_params(*args)
  @required_params ||= []
  @required_params.push(*args)
end

+ (Object) required_params



10
11
12
# File 'lib/rturk/operation.rb', line 10

def required_params
  @required_params || []
end

+ (Object) set_operation(op)



19
20
21
# File 'lib/rturk/operation.rb', line 19

def set_operation(op)
  default_params.merge!('Operation' => op)
end

Instance Method Details

- (Object) check_params



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rturk/operation.rb', line 75

def check_params
  missing_parameters = []
  self.class.required_params.each do |param|
    if self.respond_to?(param)
      missing_parameters << param.to_s if self.send(param).nil?
    else
      raise MissingParameters, "The parameter '#{param.to_s}' was required and not available"
    end
    raise MissingParameters, "Parameters '#{missing_parameters.join(', ')}' cannot be blank" unless missing_parameters.empty?
  end
end

- (Object) default_params



50
51
52
# File 'lib/rturk/operation.rb', line 50

def default_params
  self.class.default_params
end

- (Object) parse(xml)



54
55
56
57
# File 'lib/rturk/operation.rb', line 54

def parse(xml)
  # Override this in your operation if you like
  RTurk::Response.new(xml)
end

- (Object) request



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rturk/operation.rb', line 63

def request
  if self.respond_to?(:validate)
    validate
  end
  check_params
  params = {'Operation' => self.class.to_s.gsub(/^\w+::/,'')}
  params = params.merge(self.default_params)
  params = to_params.merge(params)
  response = RTurk.Request(params)
  parse(response)
end

- (Object) to_params



59
60
61
# File 'lib/rturk/operation.rb', line 59

def to_params
  {}# Override to include extra params
end