qualtrics_api 0.0.12 → 0.0.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,76 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: delete
5
+ uri: https://co1.qualtrics.com/API/v3/eventsubscriptions/SUB_5pPHcNfCaiADoFf
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ X-API-TOKEN:
11
+ - 6Wpo0Vsx1cN1kcHivCaGTz5IhOvchLrg1o4L0KOZ
12
+ User-Agent:
13
+ - Faraday v0.9.2
14
+ response:
15
+ status:
16
+ code: 200
17
+ message:
18
+ headers:
19
+ request-time:
20
+ - '325'
21
+ content-type:
22
+ - application/json
23
+ content-length:
24
+ - '85'
25
+ x-edgeconnect-midmile-rtt:
26
+ - '13'
27
+ x-edgeconnect-origin-mex-latency:
28
+ - '399'
29
+ date:
30
+ - Mon, 04 Dec 2017 22:33:13 GMT
31
+ connection:
32
+ - close
33
+ strict-transport-security:
34
+ - max-age=31536000; includeSubDomains; preload
35
+ body:
36
+ encoding: UTF-8
37
+ string: '{"meta":{"httpStatus":"200 - OK","requestId":"fa58c44f-f1a4-41ba-9c7b-3db9547407d7"}}'
38
+ http_version:
39
+ recorded_at: Mon, 04 Dec 2017 22:33:13 GMT
40
+ - request:
41
+ method: get
42
+ uri: https://co1.qualtrics.com/API/v3/eventsubscriptions/SUB_5pPHcNfCaiADoFf
43
+ body:
44
+ encoding: US-ASCII
45
+ string: ''
46
+ headers:
47
+ X-API-TOKEN:
48
+ - 6Wpo0Vsx1cN1kcHivCaGTz5IhOvchLrg1o4L0KOZ
49
+ User-Agent:
50
+ - Faraday v0.9.2
51
+ response:
52
+ status:
53
+ code: 404
54
+ message:
55
+ headers:
56
+ content-type:
57
+ - application/json
58
+ content-length:
59
+ - '136'
60
+ x-edgeconnect-midmile-rtt:
61
+ - '18'
62
+ x-edgeconnect-origin-mex-latency:
63
+ - '317'
64
+ date:
65
+ - Mon, 04 Dec 2017 22:33:13 GMT
66
+ connection:
67
+ - close
68
+ strict-transport-security:
69
+ - max-age=31536000; includeSubDomains; preload
70
+ body:
71
+ encoding: UTF-8
72
+ string: '{"meta":{"httpStatus":"404 - Not Found","error":{"errorMessage":"SUB_5pPHcNfCaiADoFf
73
+ not found","errorCode":"ESSG_2"},"requestId":null}}'
74
+ http_version:
75
+ recorded_at: Mon, 04 Dec 2017 22:33:13 GMT
76
+ recorded_with: VCR 2.9.3
@@ -0,0 +1,41 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://co1.qualtrics.com/API/v3/eventsubscriptions/SUB_6XMPw9UoySyGNyl
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ X-API-TOKEN:
11
+ - 6Wpo0Vsx1cN1kcHivCaGTz5IhOvchLrg1o4L0KOZ
12
+ User-Agent:
13
+ - Faraday v0.9.2
14
+ response:
15
+ status:
16
+ code: 200
17
+ message:
18
+ headers:
19
+ request-time:
20
+ - '454'
21
+ content-type:
22
+ - application/json
23
+ content-length:
24
+ - '274'
25
+ x-edgeconnect-midmile-rtt:
26
+ - '22'
27
+ x-edgeconnect-origin-mex-latency:
28
+ - '504'
29
+ date:
30
+ - Mon, 04 Dec 2017 21:22:04 GMT
31
+ connection:
32
+ - close
33
+ strict-transport-security:
34
+ - max-age=31536000; includeSubDomains; preload
35
+ body:
36
+ encoding: UTF-8
37
+ string: '{"result":{"id":"SUB_6XMPw9UoySyGNyl","scope":"cambridgetripod","topics":"controlpanel.activateSurvey","publicationUrl":"https://requestb.in/1mnrk0m1","encrypted":false,"successfulCalls":0},"meta":{"httpStatus":"200
38
+ - OK","requestId":"9ce297f0-0e9b-49fa-9710-ef53cde43b90"}}'
39
+ http_version:
40
+ recorded_at: Mon, 04 Dec 2017 21:22:04 GMT
41
+ recorded_with: VCR 2.9.3
@@ -0,0 +1,39 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://co1.qualtrics.com/API/v3/eventsubscriptions/pewpewpewpew
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ X-API-TOKEN:
11
+ - 6Wpo0Vsx1cN1kcHivCaGTz5IhOvchLrg1o4L0KOZ
12
+ User-Agent:
13
+ - Faraday v0.9.2
14
+ response:
15
+ status:
16
+ code: 404
17
+ message:
18
+ headers:
19
+ content-type:
20
+ - application/json
21
+ content-length:
22
+ - '129'
23
+ x-edgeconnect-midmile-rtt:
24
+ - '23'
25
+ x-edgeconnect-origin-mex-latency:
26
+ - '397'
27
+ date:
28
+ - Mon, 04 Dec 2017 21:24:24 GMT
29
+ connection:
30
+ - close
31
+ strict-transport-security:
32
+ - max-age=31536000; includeSubDomains; preload
33
+ body:
34
+ encoding: UTF-8
35
+ string: '{"meta":{"httpStatus":"404 - Not Found","error":{"errorMessage":"pewpewpewpew
36
+ not found","errorCode":"ESSG_2"},"requestId":null}}'
37
+ http_version:
38
+ recorded_at: Mon, 04 Dec 2017 21:24:24 GMT
39
+ recorded_with: VCR 2.9.3
@@ -4,8 +4,6 @@ require "faraday"
4
4
  require "faraday_middleware"
5
5
 
6
6
  require "qualtrics_api/version"
7
- require "qualtrics_api/url"
8
-
9
7
  require "qualtrics_api/request_error_handler"
10
8
 
11
9
  require "qualtrics_api/configurable"
@@ -27,6 +25,8 @@ require "qualtrics_api/panel_collection"
27
25
  require "qualtrics_api/panel_member"
28
26
  require "qualtrics_api/panel_member_collection"
29
27
  require "qualtrics_api/panel_import"
28
+ require "qualtrics_api/event_subscription"
29
+ require "qualtrics_api/event_subscription_collection"
30
30
 
31
31
  require "qualtrics_api/services/response_export_service"
32
32
 
@@ -38,16 +38,21 @@ module QualtricsAPI
38
38
  def_delegator :client, :surveys
39
39
  def_delegator :client, :response_exports
40
40
  def_delegator :client, :panels
41
+ def_delegator :client, :event_subscriptions
41
42
 
42
43
  def connection(parent = nil)
43
44
  return parent.connection if parent && parent.connection
44
45
  client.connection
45
46
  end
46
47
 
48
+ def url(data_center_id = self.data_center_id)
49
+ "https://#{data_center_id}.qualtrics.com:443/API/v3/"
50
+ end
51
+
47
52
  private
48
53
 
49
54
  def client
50
- @client ||= QualtricsAPI::Client.new(QualtricsAPI.api_token)
55
+ @client ||= QualtricsAPI::Client.new(QualtricsAPI.api_token, QualtricsAPI.data_center_id)
51
56
  end
52
57
  end
53
58
  end
@@ -14,6 +14,20 @@ module QualtricsAPI
14
14
  end
15
15
  end
16
16
 
17
+ def map
18
+ res = []
19
+ each_page do |page|
20
+ page.each do |element|
21
+ res.push(yield element)
22
+ end
23
+ end
24
+ res
25
+ end
26
+
27
+ def all
28
+ map {|element| element}
29
+ end
30
+
17
31
  def each_page
18
32
  endpoint = list_endpoint
19
33
  loop do
@@ -2,8 +2,8 @@ module QualtricsAPI
2
2
  class Client
3
3
  include QualtricsAPI::Connectable
4
4
 
5
- def initialize(api_token)
6
- @connection = establish_connection(api_token || fail('Please provide api token!'))
5
+ def initialize(api_token, data_center_id)
6
+ @connection = establish_connection(api_token || fail('Please provide api token!'), data_center_id)
7
7
  end
8
8
 
9
9
  def surveys(options = {})
@@ -18,10 +18,14 @@ module QualtricsAPI
18
18
  QualtricsAPI::PanelCollection.new(options).propagate_connection(self)
19
19
  end
20
20
 
21
+ def event_subscriptions(options = {})
22
+ QualtricsAPI::EventSubscriptionCollection.new(options).propagate_connection(self)
23
+ end
24
+
21
25
  private
22
26
 
23
- def establish_connection(api_token)
24
- Faraday.new(url: QualtricsAPI::URL, headers: { 'X-API-TOKEN' => api_token }) do |faraday|
27
+ def establish_connection(api_token, data_center_id)
28
+ Faraday.new(url: QualtricsAPI.url(data_center_id), headers: { 'X-API-TOKEN' => api_token }) do |faraday|
25
29
  faraday.request :multipart
26
30
  faraday.request :json
27
31
  faraday.response :json, :content_type => /\bjson$/
@@ -1,10 +1,15 @@
1
1
  module QualtricsAPI
2
2
  module Configurable
3
3
  attr_accessor :api_token
4
+ attr_accessor :data_center_id
4
5
 
5
6
  def configure
6
7
  yield self
7
8
  self
8
9
  end
10
+
11
+ def data_center_id
12
+ @data_center_id || 'co1'
13
+ end
9
14
  end
10
15
  end
@@ -0,0 +1,35 @@
1
+ module QualtricsAPI
2
+ class EventSubscription < BaseModel
3
+ values do
4
+ attribute :id, String
5
+ attribute :scope, String
6
+ attribute :topics, String
7
+ attribute :publication_url, String
8
+ attribute :encrypted, Boolean, default: false
9
+ attribute :successful_calls, Integer, default: 0
10
+ end
11
+
12
+ def delete
13
+ QualtricsAPI.connection(self)
14
+ .delete(endpoint)
15
+ .body["result"]
16
+ end
17
+
18
+ private
19
+
20
+ def endpoint
21
+ "eventsubscriptions/#{id}"
22
+ end
23
+
24
+ def attributes_mappings
25
+ {
26
+ id: "id",
27
+ scope: "scope",
28
+ topics: "topics",
29
+ publication_url: "publicationUrl",
30
+ encrypted: "encrypted",
31
+ successful_calls: "successfulCalls"
32
+ }
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,48 @@
1
+ module QualtricsAPI
2
+ class EventSubscriptionCollection < BaseCollection
3
+ alias_method :[], :find
4
+
5
+ def create(publication_url, topics, opts = { encrypt: false })
6
+ payload = {
7
+ publicationUrl: publication_url,
8
+ topics: topics
9
+ }.merge(opts)
10
+ res = QualtricsAPI.connection(self)
11
+ .post(list_endpoint, payload)
12
+ .body["result"]
13
+ find(res["id"])
14
+ end
15
+
16
+ def create_completed_response_subscription(url, survey_id, opts = { encrypt: false })
17
+ create(url, "surveyengine.completedResponse.#{survey_id}", opts)
18
+ end
19
+
20
+ def create_partial_response_subscription(url, survey_id, opts = { encrypt: false })
21
+ create(url, "surveyengine.partialResponse.#{survey_id}", opts)
22
+ end
23
+
24
+ def delete(id)
25
+ QualtricsAPI.connection(self)
26
+ .delete(endpoint(id))
27
+ end
28
+
29
+ def delete_all
30
+ QualtricsAPI.connection(self)
31
+ .delete(list_endpoint)
32
+ end
33
+
34
+ private
35
+
36
+ def build_result(element)
37
+ QualtricsAPI::EventSubscription.new(element)
38
+ end
39
+
40
+ def list_endpoint
41
+ "eventsubscriptions"
42
+ end
43
+
44
+ def endpoint(id)
45
+ "eventsubscriptions/#{id}"
46
+ end
47
+ end
48
+ end
@@ -1,27 +1,44 @@
1
+ require 'logger'
2
+
1
3
  module QualtricsAPI
2
4
  class RequestErrorHandler < Faraday::Response::Middleware
3
5
  def on_complete(env)
4
- case env[:status]
6
+ raise_http_errors(env[:status], env[:body])
7
+ show_notices(env[:body])
8
+ end
9
+
10
+ private
11
+
12
+ def raise_http_errors(code, body)
13
+ case code
5
14
  when 404
6
15
  raise NotFoundError, "Not Found"
7
16
  when 400
8
- raise BadRequestError, error_message(JSON.parse(env[:body]))
17
+ raise BadRequestError, error_message(JSON.parse(body))
9
18
  when 401
10
- raise UnauthorizedError, error_message(JSON.parse(env[:body]))
19
+ raise UnauthorizedError, error_message(JSON.parse(body))
11
20
  when 500
12
- raise InternalServerError, error_message(JSON.parse(env[:body]))
21
+ raise InternalServerError, error_message(JSON.parse(body))
13
22
  end
14
23
  end
15
24
 
16
- private
25
+ def show_notices(body)
26
+ response = JSON.parse(body)
27
+ notice = response["meta"]["notice"]
28
+ if notice && notice.size > 0
29
+ STDERR.puts notice
30
+ end
31
+ end
17
32
 
18
33
  def error_message(response)
19
34
  meta = response["meta"]
35
+ err = meta["error"] || {}
20
36
  ["[",
21
- meta["status"], " - ",
22
- meta["qualtricsErrorCode"] || meta["internalErrorCode"],
37
+ meta["status"] || meta["httpStatus"],
38
+ " - ",
39
+ err["qualtricsErrorCode"] || err["internalErrorCode"] || err["errorCode"],
23
40
  "] ",
24
- meta["errorMessage"]
41
+ err["errorMessage"]
25
42
  ].join
26
43
  end
27
44
  end
@@ -1,3 +1,3 @@
1
1
  module QualtricsAPI
2
- VERSION = "0.0.12".freeze
2
+ VERSION = "0.0.13".freeze
3
3
  end
@@ -20,14 +20,14 @@ Gem::Specification.new do |spec|
20
20
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
21
  spec.require_paths = ["lib"]
22
22
 
23
- spec.required_ruby_version = '>= 2.1.0'
23
+ spec.required_ruby_version = '>= 2.3.0'
24
24
 
25
- spec.add_dependency "faraday", "~> 0.9.1"
26
- spec.add_dependency "faraday_middleware", "~> 0.9.1"
25
+ spec.add_dependency "faraday", "~> 0.13.1"
26
+ spec.add_dependency "faraday_middleware", "~> 0.12.2"
27
27
  spec.add_dependency "virtus", "~> 1.0"
28
28
 
29
- spec.add_development_dependency "bundler", "~> 1.11"
30
- spec.add_development_dependency "rake", "~> 11.1"
31
- spec.add_development_dependency "rspec", "~> 3.4"
32
- spec.add_development_dependency "vcr", "~> 2.9"
29
+ spec.add_development_dependency "bundler", "~> 1.16"
30
+ spec.add_development_dependency "rake", "~> 12.3"
31
+ spec.add_development_dependency "rspec", "~> 3.7"
32
+ spec.add_development_dependency "vcr", "~> 3.0"
33
33
  end
@@ -19,11 +19,18 @@ describe QualtricsAPI::Client do
19
19
  subject { QualtricsAPI::Client }
20
20
 
21
21
  it "fails if api_token not provided" do
22
- expect { subject.new(nil) }.to raise_error('Please provide api token!')
22
+ expect { subject.new(nil, nil) }.to raise_error('Please provide api token!')
23
23
  end
24
24
 
25
- it 'establishes connection when api_token' do
26
- subject.new('sample_token')
25
+ it 'establishes connection when api_token is provided' do
26
+ client = subject.new('sample_token', 'co1')
27
+ expect(client.connection).not_to be_nil
28
+ expect(client.connection.headers["X-API-TOKEN"]).to eq('sample_token')
29
+ end
30
+
31
+ it 'establishes connection to the specified data center' do
32
+ client = subject.new('sample_token', 'somedcid')
33
+ expect(client.connection.url_prefix.to_s).to eq('https://somedcid.qualtrics.com/API/v3/')
27
34
  end
28
35
  end
29
36
  end