qualtrics_api 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a3bf9143b4c37cceea6b973651341c7d22a51adf
4
- data.tar.gz: 435d996160fa0f5eb8b8c6c73e30ee9f60c5d006
3
+ metadata.gz: a61e69c480b7a2b20a2f64d820d0dee7ef906b30
4
+ data.tar.gz: 96f38df517d7b6b061f0b110cd44c24f94b83a0f
5
5
  SHA512:
6
- metadata.gz: 3aa43a3d8216203905e042156a272cccc79dac959c3a690529ffa4d8e1eab61552aab8cc28adbbcf15204c59296623a432e5eb0bc6eae784ec25e21db6f1bc34
7
- data.tar.gz: 66d4225b2e86180e52102b3bfba52392b2a3c0c53a036e02a64c47d9bc24b60f9f811d69cac14bb76b83fee342655aa801f2b0bb854a4e82658b7e32ca85d760
6
+ metadata.gz: 0460ac8586c6f6afcd345a8e7cda73a0e7d8c93fb4f34f12cc0620253aeb07ea18e42109a47ad7f3b2f73e5cdb3a02bcb68cac15042c759d7ab81e2a6f62116f
7
+ data.tar.gz: 74010e87087b798bf4e80a53a719de558e7b4d707f7ed1aace8fe5da45f341d09f2cc1c70281b85b99fc45f60179a00a26248199e21f54751a2cfca3dbe0b61e
data/lib/qualtrics_api.rb CHANGED
@@ -9,12 +9,14 @@ require "qualtrics_api/url"
9
9
  require "qualtrics_api/request_error_handler"
10
10
 
11
11
  require "qualtrics_api/configurable"
12
+ require "qualtrics_api/connectable"
12
13
  require "qualtrics_api/client"
13
14
 
14
15
  require "qualtrics_api/extensions/serializable_model"
15
16
  require "qualtrics_api/extensions/serializable_collection"
16
17
 
17
18
  require "qualtrics_api/base_model"
19
+ require "qualtrics_api/base_collection"
18
20
 
19
21
  require "qualtrics_api/survey"
20
22
  require "qualtrics_api/survey_collection"
@@ -31,6 +33,21 @@ require "qualtrics_api/services/response_export_service"
31
33
  module QualtricsAPI
32
34
  class << self
33
35
  include QualtricsAPI::Configurable
34
- include QualtricsAPI::Client
36
+ extend Forwardable
37
+
38
+ def_delegator :client, :surveys
39
+ def_delegator :client, :response_exports
40
+ def_delegator :client, :panels
41
+
42
+ def connection(parent = nil)
43
+ return parent.connection if parent && parent.connection
44
+ client.connection
45
+ end
46
+
47
+ private
48
+
49
+ def client
50
+ @client ||= QualtricsAPI::Client.new(QualtricsAPI.api_token)
51
+ end
35
52
  end
36
53
  end
@@ -0,0 +1,12 @@
1
+ module QualtricsAPI
2
+ class BaseCollection
3
+ extend Forwardable
4
+ include Enumerable
5
+ include Virtus.value_object
6
+ include QualtricsAPI::Extensions::SerializableCollection
7
+ include QualtricsAPI::Connectable
8
+
9
+ def_delegator :all, :each
10
+ def_delegator :all, :size
11
+ end
12
+ end
@@ -2,8 +2,10 @@ module QualtricsAPI
2
2
  class BaseModel
3
3
  include Virtus.value_object
4
4
  include QualtricsAPI::Extensions::SerializableModel
5
+ include QualtricsAPI::Connectable
5
6
 
6
7
  def initialize(options = {})
8
+ @connection = options[:connection]
7
9
  attributes_mappings.each do |key, qualtrics_key|
8
10
  instance_variable_set "@#{key}", options[qualtrics_key]
9
11
  end
@@ -15,6 +17,5 @@ module QualtricsAPI
15
17
  def attributes_mappings
16
18
  {}
17
19
  end
18
-
19
20
  end
20
21
  end
@@ -1,23 +1,28 @@
1
1
  module QualtricsAPI
2
- module Client
3
- include Virtus.value_object
2
+ class Client
3
+ include QualtricsAPI::Connectable
4
+
5
+ def initialize(api_token)
6
+ @connection = establish_connection(api_token || fail('Please provide api token!'))
7
+ end
4
8
 
5
9
  def surveys(options = {})
6
10
  @surveys = nil if @surveys && @surveys.scope_id != options[:scope_id]
7
- @surveys ||= QualtricsAPI::SurveyCollection.new(options)
11
+ @surveys ||= QualtricsAPI::SurveyCollection.new(options).propagate_connection(self)
8
12
  end
9
13
 
10
14
  def response_exports(options = {})
11
- @response_exports ||= QualtricsAPI::ResponseExportCollection.new(options)
15
+ @response_exports ||= QualtricsAPI::ResponseExportCollection.new(options).propagate_connection(self)
12
16
  end
13
17
 
14
18
  def panels(options = {})
15
- @panels ||= QualtricsAPI::PanelCollection.new(options)
19
+ @panels ||= QualtricsAPI::PanelCollection.new(options).propagate_connection(self)
16
20
  end
17
21
 
18
- def connection
19
- api_token ||= QualtricsAPI.api_token || fail('Please configure api token!')
20
- @conn ||= Faraday.new(url: QualtricsAPI::URL, params: { apiToken: api_token }) do |faraday|
22
+ private
23
+
24
+ def establish_connection(api_token)
25
+ Faraday.new(url: QualtricsAPI::URL, params: { apiToken: api_token }) do |faraday|
21
26
  faraday.request :url_encoded
22
27
  faraday.response :json, :content_type => /\bjson$/
23
28
 
@@ -0,0 +1,10 @@
1
+ module QualtricsAPI
2
+ module Connectable
3
+ attr_reader :connection
4
+
5
+ def propagate_connection(connectable)
6
+ @connection = connectable.connection
7
+ self
8
+ end
9
+ end
10
+ end
@@ -8,7 +8,7 @@ module QualtricsAPI
8
8
  end
9
9
 
10
10
  def members(options = {})
11
- @members ||= QualtricsAPI::PanelMemberCollection.new(options.merge(id: id))
11
+ @members ||= QualtricsAPI::PanelMemberCollection.new(options.merge(id: id)).propagate_connection(self)
12
12
  end
13
13
 
14
14
  private
@@ -1,20 +1,12 @@
1
1
  module QualtricsAPI
2
- class PanelCollection
3
- extend Forwardable
4
- include Enumerable
5
- include Virtus.value_object
6
- include QualtricsAPI::Extensions::SerializableCollection
7
-
2
+ class PanelCollection < BaseCollection
8
3
  values do
9
4
  attribute :all, Array, :default => []
10
5
  end
11
6
 
12
- def_delegator :all, :each
13
- def_delegator :all, :size
14
-
15
7
  def fetch(_options = {})
16
8
  @all = []
17
- parse_fetch_response(QualtricsAPI.connection.get('panels'))
9
+ parse_fetch_response(QualtricsAPI.connection(self).get('panels'))
18
10
  self
19
11
  end
20
12
 
@@ -25,14 +17,14 @@ module QualtricsAPI
25
17
  def find(panel_id)
26
18
  @all.detect do |panel|
27
19
  panel.id == panel_id
28
- end || QualtricsAPI::Panel.new("panelId" => panel_id)
20
+ end || QualtricsAPI::Panel.new("panelId" => panel_id).propagate_connection(self)
29
21
  end
30
22
 
31
23
  private
32
24
 
33
25
  def parse_fetch_response(response)
34
26
  @all = response.body["result"].map do |result|
35
- QualtricsAPI::Panel.new(result)
27
+ QualtricsAPI::Panel.new(result).propagate_connection(self)
36
28
  end
37
29
  end
38
30
  end
@@ -1,14 +1,12 @@
1
1
  module QualtricsAPI
2
- class PanelImport
3
- include Virtus.value_object
4
-
2
+ class PanelImport < BaseModel
5
3
  values do
6
4
  attribute :id, String
7
5
  attribute :panel_id, String
8
6
  end
9
7
 
10
8
  def update_status
11
- res = QualtricsAPI.connection.get("panels/#{panel_id}/members/panelImports/#{id}").body["result"]
9
+ res = QualtricsAPI.connection(self).get("panels/#{panel_id}/members/panelImports/#{id}").body["result"]
12
10
  @import_progress = res["percentComplete"]
13
11
  @completed = true if @import_progress == 100.0
14
12
  self
@@ -1,29 +1,22 @@
1
1
  module QualtricsAPI
2
- class PanelMemberCollection
3
- extend Forwardable
4
- include Enumerable
5
- include Virtus.value_object
6
-
2
+ class PanelMemberCollection < BaseCollection
7
3
  values do
8
4
  attribute :id, String
9
5
  attribute :all, Array, :default => []
10
6
  end
11
7
 
12
- def_delegator :all, :each
13
- def_delegator :all, :size
14
-
15
8
  def fetch(_options = {})
16
9
  @all = []
17
- parse_fetch_response(QualtricsAPI.connection.get("panels/#{id}/members"))
10
+ parse_fetch_response(QualtricsAPI.connection(self).get("panels/#{id}/members"))
18
11
  self
19
12
  end
20
13
 
21
14
  def create(panel_members)
22
- res = QualtricsAPI.connection
23
- .post("panels/#{id}/members", QualtricsAPI.connection.params.merge(panelMembers: panel_members.to_json))
15
+ res = QualtricsAPI.connection(self)
16
+ .post("panels/#{id}/members", QualtricsAPI.connection(self).params.merge(panelMembers: panel_members.to_json))
24
17
  .body["result"]
25
18
  import_id = res['importStatus'].split('/').last
26
- QualtricsAPI::PanelImport.new(id: import_id, panel_id: id)
19
+ QualtricsAPI::PanelImport.new(id: import_id, panel_id: id).propagate_connection(self)
27
20
  end
28
21
 
29
22
  def [](member_id)
@@ -33,14 +26,14 @@ module QualtricsAPI
33
26
  def find(member_id)
34
27
  @all.detect do |panel_member|
35
28
  panel_member.id == member_id
36
- end || QualtricsAPI::PanelMember.new(:id => member_id)
29
+ end || QualtricsAPI::PanelMember.new(:id => member_id).propagate_connection(self)
37
30
  end
38
31
 
39
32
  private
40
33
 
41
34
  def parse_fetch_response(response)
42
35
  @all = response.body["result"].map do |result|
43
- QualtricsAPI::PanelMember.new(result)
36
+ QualtricsAPI::PanelMember.new(result).propagate_connection(self)
44
37
  end
45
38
  end
46
39
  end
@@ -1,13 +1,11 @@
1
1
  module QualtricsAPI
2
- class ResponseExport
3
- include Virtus.value_object
4
-
2
+ class ResponseExport < BaseModel
5
3
  values do
6
4
  attribute :id, String
7
5
  end
8
6
 
9
7
  def update_status
10
- res = QualtricsAPI.connection.get('surveys/responseExports/' + id).body["result"]
8
+ res = QualtricsAPI.connection(self).get('surveys/responseExports/' + id).body["result"]
11
9
  @export_progress = res["percentComplete"]
12
10
  @file_url = res["fileUrl"]
13
11
  @completed = true if @export_progress == 100.0
@@ -1,16 +1,9 @@
1
1
  module QualtricsAPI
2
- class ResponseExportCollection
3
- extend Forwardable
4
- include Enumerable
5
- include Virtus.value_object
6
-
2
+ class ResponseExportCollection < BaseCollection
7
3
  values do
8
4
  attribute :all, Array, :default => []
9
5
  end
10
6
 
11
- def_delegator :all, :each
12
- def_delegator :all, :size
13
-
14
7
  def [](export_id)
15
8
  find(export_id)
16
9
  end
@@ -18,7 +11,7 @@ module QualtricsAPI
18
11
  def find(export_id)
19
12
  @all.detect do |response_export|
20
13
  response_export.id == export_id
21
- end || QualtricsAPI::ResponseExport.new(:id => export_id)
14
+ end || QualtricsAPI::ResponseExport.new(:id => export_id).propagate_connection(self)
22
15
  end
23
16
  end
24
17
  end
@@ -1,28 +1,28 @@
1
1
  module QualtricsAPI
2
2
  module Services
3
- class ResponseExportService
4
- include Virtus.value_object
5
-
6
- attribute :survey_id, String
7
- attribute :response_set_id, String
8
- attribute :file_type, String, :default => 'CSV'
9
- attribute :last_response_id, String
10
- attribute :start_date, String
11
- attribute :end_date, String
12
- attribute :limit, String
13
- attribute :included_question_ids, String
14
- attribute :max_rows, String
15
- attribute :use_labels, Boolean, :default => false
16
- attribute :decimal_format, String, :default => '.'
17
- attribute :seen_unanswered_recode, String
18
- attribute :use_local_time, Boolean, :default => false
19
- attribute :spss_string_length, String
20
- attribute :id, String
3
+ class ResponseExportService < QualtricsAPI::BaseModel
4
+ values do
5
+ attribute :survey_id, String
6
+ attribute :response_set_id, String
7
+ attribute :file_type, String, :default => 'CSV'
8
+ attribute :last_response_id, String
9
+ attribute :start_date, String
10
+ attribute :end_date, String
11
+ attribute :limit, String
12
+ attribute :included_question_ids, String
13
+ attribute :max_rows, String
14
+ attribute :use_labels, Boolean, :default => false
15
+ attribute :decimal_format, String, :default => '.'
16
+ attribute :seen_unanswered_recode, String
17
+ attribute :use_local_time, Boolean, :default => false
18
+ attribute :spss_string_length, String
19
+ attribute :id, String
20
+ end
21
21
 
22
22
  attr_reader :result
23
23
 
24
24
  def start
25
- response = QualtricsAPI.connection.get("surveys/#{survey_id}/responseExports", export_params)
25
+ response = QualtricsAPI.connection(self).get("surveys/#{survey_id}/responseExports", export_params)
26
26
  export_id = response.body["result"]["exportStatus"].split('/').last
27
27
  @result = ResponseExport.new(id: export_id)
28
28
  end
@@ -10,7 +10,7 @@ module QualtricsAPI
10
10
  end
11
11
 
12
12
  def export_responses(export_options = {})
13
- QualtricsAPI::Services::ResponseExportService.new(export_options.merge(survey_id: id))
13
+ QualtricsAPI::Services::ResponseExportService.new(export_options.merge(survey_id: id)).propagate_connection(self)
14
14
  end
15
15
 
16
16
  private
@@ -1,9 +1,5 @@
1
1
  module QualtricsAPI
2
- class SurveyCollection
3
- extend Forwardable
4
- include Enumerable
5
- include Virtus.value_object
6
-
2
+ class SurveyCollection < BaseCollection
7
3
  values do
8
4
  attribute :scope_id, String
9
5
  attribute :all, Array, :default => []
@@ -11,13 +7,10 @@ module QualtricsAPI
11
7
 
12
8
  attr_writer :scope_id
13
9
 
14
- def_delegator :all, :each
15
- def_delegator :all, :size
16
-
17
10
  def fetch(options = {})
18
11
  @all = []
19
12
  update_query_attributes(options)
20
- parse_fetch_response(QualtricsAPI.connection.get('surveys', query_params))
13
+ parse_fetch_response(QualtricsAPI.connection(self).get('surveys', query_params))
21
14
  self
22
15
  end
23
16
 
@@ -57,7 +50,7 @@ module QualtricsAPI
57
50
 
58
51
  def parse_fetch_response(response)
59
52
  @all = response.body["result"].map do |result|
60
- QualtricsAPI::Survey.new result
53
+ QualtricsAPI::Survey.new(result).propagate_connection(self)
61
54
  end
62
55
  end
63
56
  end
@@ -1,3 +1,3 @@
1
1
  module QualtricsAPI
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8".freeze
3
3
  end
@@ -26,4 +26,16 @@ describe QualtricsAPI::Client do
26
26
  expect(subject.surveys.object_id).to eq subject.surveys.object_id
27
27
  end
28
28
  end
29
+
30
+ describe "#initialize" do
31
+ subject { QualtricsAPI::Client }
32
+
33
+ it "fails if api_token not provided" do
34
+ expect { subject.new(nil) }.to raise_error('Please provide api token!')
35
+ end
36
+
37
+ it 'establishes connection when api_token' do
38
+ subject.new('sample_token')
39
+ end
40
+ end
29
41
  end
@@ -47,7 +47,7 @@ describe QualtricsAPI::Survey do
47
47
 
48
48
  it "inits a ResponseExportService with options" do
49
49
  expect(QualtricsAPI::Services::ResponseExportService).to receive(:new).with(start_date: options[:start_date],
50
- survey_id: subject.id)
50
+ survey_id: subject.id).and_return(QualtricsAPI::Services::ResponseExportService.new)
51
51
 
52
52
  subject.export_responses(options)
53
53
  end
@@ -4,8 +4,62 @@ describe QualtricsAPI do
4
4
  describe "#new" do
5
5
  subject { QualtricsAPI }
6
6
 
7
- it "initializes a Client with the token passed" do
8
- expect(subject).to be_kind_of(QualtricsAPI::Client)
7
+ it 'two clients have different connection' do
8
+ client_1 = QualtricsAPI::Client.new('some_id')
9
+ client_2 = QualtricsAPI::Client.new('other_id')
10
+ expect(client_1.connection).not_to eq(client_2.connection)
11
+ end
12
+
13
+ it 'reuses connection if globally configured' do
14
+ expect(QualtricsAPI.connection).to eq(QualtricsAPI.connection)
15
+ end
16
+
17
+ it 'does not reuse connection with client' do
18
+ client = QualtricsAPI::Client.new('some_id')
19
+ expect(client.connection).not_to eq(QualtricsAPI.connection)
20
+ end
21
+
22
+ context 'chains' do
23
+ it 'does propagate default connection' do
24
+ members = VCR.use_cassette('panel_member_collection_create_success') do
25
+ QualtricsAPI.panels.fetch['ML_bC2c5xBz1DxyOYB'].members
26
+ end
27
+ expect(members.connection).to eq(QualtricsAPI.connection)
28
+ end
29
+
30
+ context 'with client' do
31
+ let(:client) { QualtricsAPI::Client.new(TEST_API_TOKEN) }
32
+ let(:members) do
33
+ VCR.use_cassette('panel_member_collection_create_success') do
34
+ client.panels.fetch['ML_bC2c5xBz1DxyOYB'].members
35
+ end
36
+ end
37
+
38
+ it 'has client connection' do
39
+ expect(client.connection).not_to be_nil
40
+ end
41
+
42
+ it 'has propagated exception to members' do
43
+ expect(members.connection).to eq(client.connection)
44
+ end
45
+
46
+ context 'with different client' do
47
+ let(:client_2) { QualtricsAPI::Client.new(TEST_API_TOKEN) }
48
+ let(:members_2) do
49
+ VCR.use_cassette('panel_member_collection_create_success') do
50
+ client_2.panels.fetch['ML_bC2c5xBz1DxyOYB'].members
51
+ end
52
+ end
53
+
54
+ it 'has propagated exception to members' do
55
+ expect(members_2.connection).to eq(client_2.connection)
56
+ end
57
+
58
+ it 'does not conflict with different client' do
59
+ expect(client_2.connection).not_to eq(client.connection)
60
+ end
61
+ end
62
+ end
9
63
  end
10
64
  end
11
65
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qualtrics_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yurui Zhang
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2015-08-04 00:00:00.000000000 Z
13
+ date: 2015-08-19 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: faraday
@@ -170,9 +170,11 @@ files:
170
170
  - fixtures/vcr_cassettes/survey_collection_fetch_sucess.yml
171
171
  - fixtures/vcr_cassettes/survey_collection_fetch_with_scopeId_success.yml
172
172
  - lib/qualtrics_api.rb
173
+ - lib/qualtrics_api/base_collection.rb
173
174
  - lib/qualtrics_api/base_model.rb
174
175
  - lib/qualtrics_api/client.rb
175
176
  - lib/qualtrics_api/configurable.rb
177
+ - lib/qualtrics_api/connectable.rb
176
178
  - lib/qualtrics_api/extensions/serializable_collection.rb
177
179
  - lib/qualtrics_api/extensions/serializable_model.rb
178
180
  - lib/qualtrics_api/panel.rb