Class: Qualtrics::Operation

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

Constant Summary collapse

REQUEST_METHOD_WHITELIST =
[:get, :post]
@@listeners =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_method, action, options, body_override = nil) ⇒ Operation

Returns a new instance of Operation.



7
8
9
10
11
12
13
14
# File 'lib/qualtrics/operation.rb', line 7

def initialize(http_method, action, options, body_override = nil)
  @http_method = http_method
  @action = action
  @options = options
  @entity_name = action.gsub(/(create|delete|update)/, '')
  @body_override = body_override
  @command = $1
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



3
4
5
# File 'lib/qualtrics/operation.rb', line 3

def action
  @action
end

#commandObject (readonly)

Returns the value of attribute command.



3
4
5
# File 'lib/qualtrics/operation.rb', line 3

def command
  @command
end

#entity_nameObject (readonly)

Returns the value of attribute entity_name.



3
4
5
# File 'lib/qualtrics/operation.rb', line 3

def entity_name
  @entity_name
end

#http_methodObject (readonly)

Returns the value of attribute http_method.



3
4
5
# File 'lib/qualtrics/operation.rb', line 3

def http_method
  @http_method
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/qualtrics/operation.rb', line 3

def options
  @options
end

Class Method Details

.add_listener(listener) ⇒ Object



53
54
55
# File 'lib/qualtrics/operation.rb', line 53

def add_listener(listener)
  @@listeners << listener
end

.delete_listener(listener) ⇒ Object



57
58
59
# File 'lib/qualtrics/operation.rb', line 57

def delete_listener(listener)
  @@listeners.delete(listener)
end

.flush_listeners!Object



61
62
63
# File 'lib/qualtrics/operation.rb', line 61

def flush_listeners!
  @@listeners = []
end

Instance Method Details

#disable_listeners(&block) ⇒ Object



46
47
48
49
50
# File 'lib/qualtrics/operation.rb', line 46

def disable_listeners(&block)
  @listeners_disabled = true
  block.call(self)
  @listeners_disabled = nil
end

#issue_requestObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/qualtrics/operation.rb', line 16

def issue_request
  raise Qualtrics::UnexpectedRequestMethod if !REQUEST_METHOD_WHITELIST.include?(http_method)

  query = default_params.dup.merge(options)
  query['Request'] = action
  body = nil
  query_params = {}
  raw_resp = nil

  if @body_override
    body = @body_override
    query_params = query
    raw_resp = connection.send(http_method, path, body) do |req|
      req.params = query_params
    end
  else
    body = query
    raw_resp = connection.send(http_method, path, body)
  end


  Qualtrics::Response.new(raw_resp).tap do |response|
    if !@listeners_disabled
      @@listeners.each do |listener|
        listener.received_response(self, response)
      end
    end
  end
end