Class: RTurk::Operation

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) {|_self| ... } ⇒ Operation

###################### # 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

.create(opts = {}, &blk) ⇒ Object



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

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

.default_paramsObject



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

def default_params
  @default_params ||= {}
end

.require_params(*args) ⇒ Object



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

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

.required_paramsObject



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

def required_params
  @required_params || []
end

.set_operation(op) ⇒ Object



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

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

Instance Method Details

#check_paramsObject



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

#default_paramsObject



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

def default_params
  self.class.default_params
end

#parse(xml) ⇒ Object



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

#requestObject



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

#to_paramsObject



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

def to_params
  {}# Override to include extra params
end