survey-gizmo-ruby 4.0.0 → 4.1.0

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: ea584803e0be7b02dc62f295924bc21bac08f0ba
4
- data.tar.gz: 183b970af5cdee838e5a64e331e232faf1075541
3
+ metadata.gz: 00dc389fa6c3415227ed3ea344c8ec1b7152eaa8
4
+ data.tar.gz: 342bda557eb8df193f19958de0337a23504c5a49
5
5
  SHA512:
6
- metadata.gz: 74f7f2967b1dedb1f95d88e47ff03d5c038d7b9ed7e57170c4463d02af57b983c64b50b5f97c46b61c87843e2add15ce97aacf99dbe21422daae3ed0cca15a4b
7
- data.tar.gz: 2e000e5f1e3c657959ffd82455355cdf5b9dddf319a850700cf2b3e707377bbad5182eb01cb938a790c1cff8602ed6b27091fffab2ccda89f99aaa401a136cae
6
+ metadata.gz: 35695df30398092c26e8fdb48eead2e669753df6cddd4a615eb70688ede385a80e6731adfd29d33bc83ae93b25a15d875c5cf3d2efa7519cb3124e02b018f6a5
7
+ data.tar.gz: 434990416b53a6cdd22c00f102f888125ec5a501c800c2c8df41fe1a8a126d510228595f8d9b4fd7a6482fb6644235d029564ca476c9166ccb14a7ccea3940c5
data/README.md CHANGED
@@ -40,12 +40,17 @@ SurveyGizmo.configure do |config|
40
40
  config.user = 'still_tippin@woodgraingrip.com'
41
41
  config.password = 'it_takes_grindin_to_be_a_king'
42
42
 
43
- # api_version defaults to v4, but you can probably set to v3 safely if you suspect a bug in v4
43
+ # Optional - Defaults to v4, but you can probably set to v3 safely if you suspect a bug in v4
44
44
  config.api_version = 'v4'
45
+
46
+ # Optional - Defaults to 50, maximum 500. Setting too high may cause SurveyGizmo to start throwing timeouts.
47
+ config.results_per_page = 100
45
48
  end
46
49
 
47
- # Retrieve all your surveys
50
+ # Retrieve the first page of your surveys
48
51
  surveys = SurveyGizmo::API::Survey.all
52
+ # Retrieve ALL your surveys (handle pagination for you)
53
+ surveys = SurveyGizmo::API::Survey.all(all_pages: true)
49
54
 
50
55
  # Retrieve the survey with id: 12345
51
56
  survey = SurveyGizmo::API::Survey.first(id: 12345)
@@ -54,6 +59,7 @@ survey.pages # => [page1, page2,...]
54
59
  survey.number_of_completed_responses # => 355
55
60
  survey.server_has_new_results_since?(Time.now.utc - 2.days) # => true
56
61
  survey.team_names # => ['Development', 'Test']
62
+ survey.belongs_to?('Development') # => true
57
63
 
58
64
  # Retrieving Questions for a given survey. Note that page_id is a required parameter.
59
65
  questions = SurveyGizmo::API::Question.all(survey_id: survey.id, page_id: 1)
@@ -68,14 +74,28 @@ question.save
68
74
  # Destroy a question
69
75
  question.destroy
70
76
 
71
- # Retrieving SurveyResponses for a given survey.
72
- # Note that because both options are hashes, you need to enclose them both in
73
- # braces to page successfully!
74
- responses = SurveyGizmo::API::Response.all({ survey_id: survey.id }, { page: 1 })
75
-
76
- # Retrieving page 2 of non test data SurveyResponses
77
- filters = { page: 2, filters: [{ field: 'istestdata', operator: '<>', value: 1 }] }
78
- responses = SurveyGizmo::API::Response.all({ survey_id: survey_id }, filters)
77
+ # Retrieve 2nd page of SurveyResponses for a given survey.
78
+ responses = SurveyGizmo::API::Response.all(survey_id: 12345, page: 2)
79
+ # Retrieve all responses for a given survey.
80
+ responses = SurveyGizmo::API::Response.all(all_pages: true, survey_id: 12345)
81
+ # Retrieving page 3 of completed, non test data SurveyResponses submitted within the past 3 days
82
+ # for contact id 999. This example shows you how to use some of the gem's built in filters and
83
+ # filter generators as well as how to construct your own raw filter.
84
+ # See: http://apihelp.surveygizmo.com/help/article/link/filters for more info on filters
85
+ responses = SurveyGizmo::API::Response.all(
86
+ survey_id: 12345,
87
+ page: 3,
88
+ filters: [
89
+ SurveyGizmo::API::Response::NO_TEST_DATA,
90
+ SurveyGizmo::API::Response::ONLY_COMPLETED,
91
+ SurveyGizmo::API::Response.submitted_since_filter(Time.now - 72.hours),
92
+ {
93
+ field: 'contact_id',
94
+ operator: '=',
95
+ value: 999
96
+ }
97
+ ]
98
+ )
79
99
  ```
80
100
 
81
101
  ## On API Timeouts
@@ -3,15 +3,15 @@ require 'active_support/core_ext/module'
3
3
  require 'active_support/core_ext/hash'
4
4
  require 'active_support/core_ext/object/blank'
5
5
  require 'active_support/concern'
6
+ require 'active_support/time_with_zone'
6
7
  require 'awesome_print'
7
- require 'virtus'
8
- require 'httparty'
9
8
  require 'digest/md5'
9
+ require 'httparty'
10
+ require 'virtus'
10
11
 
11
12
  require 'survey_gizmo/version'
12
13
  require 'survey_gizmo/survey_gizmo'
13
14
  require 'survey_gizmo/configuration'
14
-
15
15
  require 'survey_gizmo/multilingual_title'
16
16
  require 'survey_gizmo/resource'
17
17
  require 'survey_gizmo/rest_response'
@@ -2,17 +2,15 @@ module SurveyGizmo; module API
2
2
  # @see SurveyGizmo::Resource::ClassMethods
3
3
  class Option
4
4
  include SurveyGizmo::Resource
5
+ include SurveyGizmo::MultilingualTitle
5
6
 
6
7
  attribute :id, Integer
7
8
  attribute :survey_id, Integer
8
9
  attribute :page_id, Integer
9
10
  attribute :question_id, Integer
10
- attribute :title, String
11
11
  attribute :value, String
12
12
  attribute :properties, Hash
13
13
 
14
- include SurveyGizmo::MultilingualTitle
15
-
16
14
  route '/survey/:survey_id/surveypage/:page_id/surveyquestion/:question_id/surveyoption', via: :create
17
15
  route '/survey/:survey_id/surveypage/:page_id/surveyquestion/:question_id/surveyoption/:id', via: [:get, :update, :delete]
18
16
 
@@ -2,16 +2,14 @@ module SurveyGizmo; module API
2
2
  # @see SurveyGizmo::Resource::ClassMethods
3
3
  class Page
4
4
  include SurveyGizmo::Resource
5
+ include SurveyGizmo::MultilingualTitle
5
6
 
6
7
  attribute :id, Integer
7
- attribute :title, Hash
8
8
  attribute :description, String
9
9
  attribute :properties, Hash
10
10
  attribute :after, Integer
11
11
  attribute :survey_id, Integer
12
12
 
13
- include SurveyGizmo::MultilingualTitle
14
-
15
13
  # routing
16
14
  route '/survey/:survey_id/surveypage', via: :create
17
15
  route '/survey/:survey_id/surveypage/:id', via: [:get, :update, :delete]
@@ -21,7 +19,7 @@ module SurveyGizmo; module API
21
19
  end
22
20
 
23
21
  def questions
24
- @questions ||= SurveyGizmo::API::Question.all(survey_id: survey_id, page_id: id)
22
+ @questions ||= SurveyGizmo::API::Question.all(survey_id: survey_id, page_id: id, all_pages: true)
25
23
  end
26
24
 
27
25
  def to_param_options
@@ -2,9 +2,9 @@ module SurveyGizmo; module API
2
2
  # @see SurveyGizmo::Resource::ClassMethods
3
3
  class Question
4
4
  include SurveyGizmo::Resource
5
+ include SurveyGizmo::MultilingualTitle
5
6
 
6
7
  attribute :id, Integer
7
- attribute :title, Hash
8
8
  attribute :type, String
9
9
  attribute :description, String
10
10
  attribute :shortname, String
@@ -17,26 +17,24 @@ module SurveyGizmo; module API
17
17
 
18
18
  alias_attribute :_subtype, :type
19
19
 
20
- include SurveyGizmo::MultilingualTitle
21
-
22
20
  route '/survey/:survey_id/surveyquestion/:id', via: :get
23
21
  route '/survey/:survey_id/surveypage/:page_id/surveyquestion', via: :create
24
22
  route '/survey/:survey_id/surveypage/:page_id/surveyquestion/:id', via: [:update, :delete]
25
23
 
26
24
  def survey
27
- @survey ||= SurveyGizmo::API::Survey.first(id: survey_id)
25
+ @survey ||= Survey.first(id: survey_id)
28
26
  end
29
27
 
30
28
  def options
31
- @options ||= SurveyGizmo::API::Option.all(survey_id: survey_id, page_id: page_id, question_id: id)
29
+ @options ||= Option.all(survey_id: survey_id, page_id: page_id, question_id: id, all_pages: true)
32
30
  end
33
31
 
34
32
  def parent_question
35
- @parent_question ||= parent_question_id ? SurveyGizmo::API::Question.first(survey_id: survey_id, id: parent_question_id) : nil
33
+ @parent_question ||= parent_question_id ? Question.first(survey_id: survey_id, id: parent_question_id) : nil
36
34
  end
37
35
 
38
36
  def sub_questions
39
- @sub_questions ||= sub_question_skus.map { |subquestion_id| SurveyGizmo::API::Question.first(survey_id: survey_id, id: subquestion_id) }
37
+ @sub_questions ||= sub_question_skus.map { |subquestion_id| Question.first(survey_id: survey_id, id: subquestion_id) }
40
38
  .each { |subquestion| subquestion.parent_question_id = id }
41
39
  end
42
40
 
@@ -3,6 +3,18 @@ module SurveyGizmo; module API
3
3
  class Response
4
4
  include SurveyGizmo::Resource
5
5
 
6
+ # Filters
7
+ NO_TEST_DATA = { field: 'istestdata', operator: '<>', value: 1 }
8
+ ONLY_COMPLETED = { field: 'status', operator: '=', value: 'Complete' }
9
+
10
+ def self.submitted_since_filter(time)
11
+ {
12
+ field: 'datesubmitted',
13
+ operator: '>=',
14
+ value: time.in_time_zone('Eastern Time (US & Canada)').strftime('%Y-%m-%d %H:%M:%S')
15
+ }
16
+ end
17
+
6
18
  attribute :id, Integer
7
19
  attribute :survey_id, Integer
8
20
  attribute :contact_id, Integer
@@ -21,7 +33,7 @@ module SurveyGizmo; module API
21
33
  route '/survey/:survey_id/surveyresponse/:id', via: [:get, :update, :delete]
22
34
 
23
35
  def survey
24
- @survey ||= SurveyGizmo::API::Survey.first(id: survey_id)
36
+ @survey ||= Survey.first(id: survey_id)
25
37
  end
26
38
 
27
39
  def to_param_options
@@ -25,18 +25,18 @@ module SurveyGizmo; module API
25
25
  route '/survey', via: :create
26
26
 
27
27
  def to_param_options
28
- { id: self.id }
28
+ { id: id }
29
29
  end
30
30
 
31
31
  def pages
32
- @pages ||= SurveyGizmo::API::Page.all(survey_id: id)
32
+ @pages ||= Page.all(survey_id: id, all_pages: true)
33
33
  end
34
34
 
35
35
  # Sub question handling is in resource.rb. It should probably be here instead but if it gets moved here
36
36
  # and people try to request all the questions for a specific page directly from a ::API::Question request,
37
37
  # sub questions will not be included! So I left it there for least astonishment.
38
38
  def questions
39
- @questions ||= pages.map { |p| SurveyGizmo::API::Question.all(survey_id: id, page_id: p.id) }.flatten
39
+ @questions ||= pages.map { |p| Question.all(survey_id: id, page_id: p.id, all_pages: true) }.flatten
40
40
  end
41
41
 
42
42
  # Statistics array of arrays looks like:
@@ -50,19 +50,13 @@ module SurveyGizmo; module API
50
50
  end
51
51
 
52
52
  def server_has_new_results_since?(time)
53
- filters = [{
54
- field: 'datesubmitted',
55
- operator: '>=',
56
- value: time.in_time_zone('Eastern Time (US & Canada)').strftime('%Y-%m-%d %H:%M:%S')
57
- }]
58
- responses = SurveyGizmo::API::Response.all({ survey_id: self.id }, { page: 1, filters: filters })
59
- responses.size > 0
53
+ Response.all(survey_id: id, filters: [Response.submitted_since_filter(time)]).size > 0
60
54
  end
61
55
 
62
- # As of 2015-08-07, when you request data on multiple surveys from /survey, the team
63
- # variable comes back as "0". If you request one survey at a time from /survey/{id}, it works correctly.
56
+ # As of 2015-12-18, when you request data on multiple surveys from /survey, the team variable comes
57
+ # back as "0". If you request one survey at a time from /survey/{id}, it is populated correctly.
64
58
  def teams
65
- @individual_survey ||= SurveyGizmo::API::Survey.first(id: self.id)
59
+ @individual_survey ||= Survey.first(id: id)
66
60
  @individual_survey.team
67
61
  end
68
62
 
@@ -23,8 +23,8 @@ module SurveyGizmo; module API
23
23
  attribute :surveycampaign, Integer
24
24
  attribute :copy, Boolean
25
25
 
26
- route '/survey/:survey_id/surveycampaign/:id', :via => [:get, :update, :delete]
27
- route '/survey/:survey_id/surveycampaign', :via => :create
26
+ route '/survey/:survey_id/surveycampaign/:id', via: [:get, :update, :delete]
27
+ route '/survey/:survey_id/surveycampaign', via: :create
28
28
 
29
29
  def to_param_options
30
30
  { id: self.id, survey_id: self.survey_id }
@@ -14,13 +14,16 @@ module SurveyGizmo
14
14
  end
15
15
 
16
16
  class Configuration
17
+ DEFAULT_RESULTS_PER_PAGE = 50
17
18
  DEFAULT_API_VERSION = 'v4'
18
19
 
19
20
  attr_accessor :api_version
20
21
  attr_accessor :user
21
22
  attr_accessor :password
23
+ attr_accessor :results_per_page
22
24
 
23
25
  def initialize
26
+ @results_per_page = DEFAULT_RESULTS_PER_PAGE
24
27
  @api_version = DEFAULT_API_VERSION
25
28
  end
26
29
  end
@@ -1,11 +1,12 @@
1
- # Inclusion of this module must come AFTER the virtus call:
2
- # attribute :title
1
+ # SurveyGizmo has a bad habit of returning titles in different formats when one is
2
+ # requesting all surveys vs. an individual survey.
3
3
 
4
4
  module SurveyGizmo
5
5
  module MultilingualTitle
6
6
  extend ActiveSupport::Concern
7
7
 
8
8
  included do
9
+ attribute :title, Hash
9
10
  alias_method_chain :title=, :multilingual
10
11
  end
11
12
 
@@ -18,16 +18,41 @@ module SurveyGizmo
18
18
 
19
19
  # These are methods that every API resource can use to access resources in SurveyGizmo
20
20
  module ClassMethods
21
- # Get an array of resources
22
- def all(conditions = {}, filters = nil)
23
- response = RestResponse.new(SurveyGizmo.get(handle_route(:create, conditions) + convert_filters_into_query_string(filters)))
24
- _collection = response.data.map { |datum| datum.is_a?(Hash) ? self.new(datum) : datum }
21
+ # Get an array of resources.
22
+ # @param [Hash] options - simple URL params at the top level, and SurveyGizmo "filters" at the :filters key
23
+ #
24
+ # example: { page: 2, filters: [{ field: "istestdata", operator: "<>", value: 1 }] }
25
+ #
26
+ # The top level keys (e.g. page, resultsperpage) get encoded in the url, while the
27
+ # contents of the array of hashes passed at the :filters key get turned into the format
28
+ # SurveyGizmo expects for its internal filtering, for example:
29
+ #
30
+ # filter[field][0]=istestdata&filter[operator][0]=<>&filter[value][0]=1
31
+ #
32
+ # Set all_pages: true if you want the gem to page through all the available responses
33
+ def all(conditions = {}, _deprecated_filters = {})
34
+ conditions = merge_params(conditions, _deprecated_filters)
35
+ fail ':all_pages and :page are mutually exclusive' if conditions[:page] && conditions[:all_pages]
36
+
37
+ all_pages = conditions.delete(:all_pages)
38
+ properties = conditions.dup
39
+ conditions[:resultsperpage] = SurveyGizmo.configuration.results_per_page unless conditions[:resultsperpage]
40
+
41
+ request_route = handle_route!(:create, conditions)
42
+ response = RestResponse.new(SurveyGizmo.get(request_route + filters_to_query_string(conditions)))
43
+ collection = response.data.map { |datum| datum.is_a?(Hash) ? new(datum) : datum }
44
+
45
+ while all_pages && response.current_page < response.total_pages
46
+ paged_filter = filters_to_query_string(conditions.merge(page: response.current_page + 1))
47
+ response = RestResponse.new(SurveyGizmo.get(request_route + paged_filter))
48
+ collection += response.data.map { |datum| datum.is_a?(Hash) ? new(datum) : datum }
49
+ end
25
50
 
26
51
  # Add in the properties from the conditions hash because many of the important ones (like survey_id) are
27
52
  # not often part of the SurveyGizmo returned data
28
- conditions.keys.each do |k|
29
- if conditions[k] && instance_methods.include?(k)
30
- _collection.each { |c| c[k] ||= conditions[k] }
53
+ properties.each do |k,v|
54
+ if v && instance_methods.include?(k)
55
+ collection.each { |c| c[k] ||= v }
31
56
  end
32
57
  end
33
58
 
@@ -35,18 +60,21 @@ module SurveyGizmo
35
60
  # SurveyGizmo claims they will fix this bug and eventually all questions will be
36
61
  # returned in one request.
37
62
  if self == SurveyGizmo::API::Question
38
- _collection += _collection.map { |question| question.sub_questions }.flatten
63
+ collection += collection.map { |question| question.sub_questions }.flatten
39
64
  end
40
65
 
41
- _collection
66
+ collection
42
67
  end
43
68
 
44
- # Retrieve a single resource.
45
- def first(conditions = {}, filters = nil)
46
- response = RestResponse.new(SurveyGizmo.get(handle_route(:get, conditions) + convert_filters_into_query_string(filters)))
69
+ # Retrieve a single resource. See usage comment on .all
70
+ def first(conditions, _deprecated_filters = {})
71
+ conditions = merge_params(conditions, _deprecated_filters)
72
+ properties = conditions.dup
73
+
74
+ response = RestResponse.new(SurveyGizmo.get(handle_route!(:get, conditions) + filters_to_query_string(conditions)))
47
75
  # Add in the properties from the conditions hash because many of the important ones (like survey_id) are
48
76
  # not often part of the SurveyGizmo's returned data
49
- new(conditions.merge(response.data))
77
+ new(properties.merge(response.data))
50
78
  end
51
79
 
52
80
  # Create a new resource. Returns the newly created Resource instance.
@@ -58,7 +86,7 @@ module SurveyGizmo
58
86
 
59
87
  # Delete resources
60
88
  def destroy(conditions)
61
- RestResponse.new(SurveyGizmo.delete(handle_route(:delete, conditions)))
89
+ RestResponse.new(SurveyGizmo.delete(handle_route!(:delete, conditions)))
62
90
  end
63
91
 
64
92
  # Define the path where a resource is located
@@ -68,56 +96,52 @@ module SurveyGizmo
68
96
  methods.is_a?(Array) ? methods.each { |m| @paths[m] = path } : (@paths[methods] = path)
69
97
  end
70
98
 
71
- # This method replaces the :page_id, :survey_id, etc strings defined in each model's URI routes with the
99
+ # Replaces the :page_id, :survey_id, etc strings defined in each model's URI routes with the
72
100
  # values being passed in interpolation hash with the same keys.
73
- def handle_route(key, interpolation_hash)
101
+ #
102
+ # This method has the SIDE EFFECT of deleting REST path related keys from interpolation_hash!
103
+ def handle_route!(key, interpolation_hash)
74
104
  path = @paths[key]
75
- fail "No routes defined for `#{key}` in #{self.name}" unless path
105
+ fail "No routes defined for `#{key}` in #{name}" unless path
76
106
  fail "User/password hash not setup!" if SurveyGizmo.default_params.empty?
77
107
 
78
108
  path.gsub(/:(\w+)/) do |m|
79
109
  raise(SurveyGizmo::URLError, "Missing RESTful parameters in request: `#{m}`") unless interpolation_hash[$1.to_sym]
80
- interpolation_hash[$1.to_sym]
110
+ interpolation_hash.delete($1.to_sym)
81
111
  end
82
112
  end
83
113
 
84
- # Convert a [Hash] of filters into a query string
85
- # @param [Hash] filters - simple pagination or other options at the top level, and surveygizmo "filters" at the :filters key
86
- # @return [String]
87
- #
88
- # example input: { page: 2, filters: [{:field=>"istestdata", :operator=>"<>", :value=>1}] }
89
- #
90
- # The top level keys (e.g. page, resultsperpage) get simply encoded in the url, while the
91
- # contents of the array of hashes passed in the filters hash get turned into the format
92
- # SurveyGizmo expects for its internal filtering, for example:
93
- #
94
- # filter[field][0]=istestdata&filter[operator][0]=<>&filter[value][0]=1
95
- def convert_filters_into_query_string(filters = nil)
114
+ private
115
+
116
+ # Convert a [Hash] of params and internal surveygizmo style filters into a query string
117
+ def filters_to_query_string(filters = {})
96
118
  return '' unless filters && filters.size > 0
97
119
 
98
- output_filters = filters[:filters] || []
99
- filter_hash = {}
100
- output_filters.each_with_index do |filter,i|
101
- filter_hash.merge!(
102
- "filter[field][#{i}]".to_sym => "#{filter[:field]}",
103
- "filter[operator][#{i}]".to_sym => "#{filter[:operator]}",
104
- "filter[value][#{i}]".to_sym => "#{filter[:value]}",
105
- )
120
+ params = {}
121
+ (filters.delete(:filters) || []).each_with_index do |filter, i|
122
+ fail "Bad filter params: #{filter}" unless filter.is_a?(Hash) && [:field, :operator, :value].all? { |k| filter[k] }
123
+
124
+ params["filter[field][#{i}]".to_sym] = "#{filter[:field]}"
125
+ params["filter[operator][#{i}]".to_sym] = "#{filter[:operator]}"
126
+ params["filter[value][#{i}]".to_sym] = "#{filter[:value]}"
106
127
  end
107
- simple_filters = filters.reject { |k,v| k == :filters }
108
- filter_hash.merge!(simple_filters)
109
128
 
110
129
  uri = Addressable::URI.new
111
- uri.query_values = filter_hash
130
+ uri.query_values = params.merge(filters)
112
131
  "?#{uri.query}"
113
132
  end
133
+
134
+ def merge_params(conditions, _deprecated_filters)
135
+ $stderr.puts('Use of the 2nd hash parameter is deprecated.') unless _deprecated_filters.empty?
136
+ conditions.merge(_deprecated_filters || {})
137
+ end
114
138
  end
115
139
 
116
140
  # Save the resource to SurveyGizmo
117
141
  def save
118
142
  if id
119
143
  # Then it's an update, because we already know the surveygizmo assigned id
120
- RestResponse.new(SurveyGizmo.post(handle_route(:update), query: self.attributes_without_blanks))
144
+ RestResponse.new(SurveyGizmo.post(handle_route(:update), query: attributes_without_blanks))
121
145
  else
122
146
  create_record_in_surveygizmo
123
147
  end
@@ -143,46 +167,32 @@ module SurveyGizmo
143
167
 
144
168
  # Returns itself if successfully saved, but with attributes added by SurveyGizmo
145
169
  def create_record_in_surveygizmo(attributes = {})
146
- rest_response = RestResponse.new(SurveyGizmo.put(handle_route(:create), query: self.attributes_without_blanks))
170
+ rest_response = RestResponse.new(SurveyGizmo.put(handle_route(:create), query: attributes_without_blanks))
147
171
  self.attributes = rest_response.data
148
172
  self
149
173
  end
150
174
 
151
175
  def inspect
152
- if ENV['GIZMO_DEBUG']
153
- ap "CLASS: #{self.class}"
154
- end
155
-
156
176
  attribute_strings = self.class.attribute_set.map do |attrib|
157
- if ENV['GIZMO_DEBUG']
158
- ap attrib
159
- ap attrib.name
160
- ap self.send(attrib.name)
161
- ap self.send(attrib.name).class
162
- end
163
-
164
- if self.send(attrib.name).class == Hash
165
- value = self.send(attrib.name).inspect
166
- else
167
- value = self.send(attrib.name).to_s
168
- end
177
+ value = self.send(attrib.name)
178
+ value = value.is_a?(Hash) ? value.inspect : value.to_s
169
179
 
170
180
  " \"#{attrib.name}\" => \"#{value}\"\n" unless value.strip.blank?
171
181
  end.compact
172
182
 
173
- "#<#{self.class.name}:#{self.object_id}>\n#{attribute_strings.join()}"
183
+ "#<#{self.class.name}:#{self.object_id}>\n#{attribute_strings.join}"
174
184
  end
175
185
 
176
186
  protected
177
187
 
178
188
  def attributes_without_blanks
179
- self.attributes.reject { |k,v| v.blank? }
189
+ attributes.reject { |k,v| v.blank? }
180
190
  end
181
191
 
182
192
  private
183
193
 
184
194
  def handle_route(key)
185
- self.class.handle_route(key, to_param_options)
195
+ self.class.handle_route!(key, to_param_options)
186
196
  end
187
197
  end
188
198
  end
@@ -1,22 +1,23 @@
1
1
  # This class normalizes the response returned by Survey Gizmo, including validation.
2
+
2
3
  class RestResponse
3
4
  attr_accessor :raw_response
4
5
  attr_accessor :parsed_response
5
6
 
6
- def initialize(rest_response)
7
- @raw_response = rest_response
8
- @parsed_response = rest_response.parsed_response
7
+ def initialize(http_response)
8
+ @raw_response = http_response
9
+ @parsed_response = http_response.parsed_response
9
10
 
10
11
  if ENV['GIZMO_DEBUG']
11
- ap 'SG Response: '
12
+ ap 'Parsed SurveyGizmo Response:'
12
13
  ap @parsed_response
13
14
  end
14
15
 
15
- fail "Bad response: #{rest_response.inspect}" unless @parsed_response['result_ok'] && @parsed_response['result_ok'].to_s.downcase == 'true'
16
+ fail "Bad response: #{http_response.inspect}" unless @parsed_response['result_ok'] && @parsed_response['result_ok'].to_s.downcase == 'true'
16
17
  return unless data
17
18
 
18
19
  # Handle really crappy [] notation in SG API, so far just in SurveyResponse
19
- (data.is_a?(Array) ? data : [data]).each do |datum|
20
+ (data.is_a?(Array) ? data : [data]).compact.each do |datum|
20
21
  unless datum['datesubmitted'].blank?
21
22
  # SurveyGizmo returns date information in EST but does not provide time zone information.
22
23
  # See https://surveygizmov4.helpgizmo.com/help/article/link/date-and-time-submitted
@@ -47,12 +48,20 @@ class RestResponse
47
48
 
48
49
  # The parsed JSON data of the response
49
50
  def data
50
- @_data ||= @parsed_response['data']
51
+ @parsed_response['data']
51
52
  end
52
53
 
53
54
  # The error message if there is one
54
55
  def message
55
- @_message ||= @parsed_response['message']
56
+ @parsed_response['message']
57
+ end
58
+
59
+ def current_page
60
+ @parsed_response['page']
61
+ end
62
+
63
+ def total_pages
64
+ @parsed_response['total_pages']
56
65
  end
57
66
 
58
67
  private
@@ -1,3 +1,4 @@
1
1
  module SurveyGizmo
2
- VERSION = '4.0.0'
2
+ # TODO: When bumping to 5.0, remove the deprecated parameters for Resource.all and Resource.first
3
+ VERSION = '4.1.0'
3
4
  end
@@ -36,16 +36,16 @@ describe 'Survey Gizmo Resource' do
36
36
  it_should_behave_like 'an API object'
37
37
  it_should_behave_like 'an object with errors'
38
38
 
39
- context '#convert_filters_into_query_string' do
39
+ context '#filters_to_query_string' do
40
40
  let(:page) { 2 }
41
41
  let(:filters) { { page: page, filters: [{ field: 'istestdata', operator: '<>', value: 1 }] } }
42
42
 
43
43
  it 'should generate the correct page request' do
44
- expect(SurveyGizmoSpec::ResourceTest.convert_filters_into_query_string(page: page)).to eq("?page=#{page}")
44
+ expect(SurveyGizmoSpec::ResourceTest.send(:filters_to_query_string, { page: page })).to eq("?page=#{page}")
45
45
  end
46
46
 
47
47
  it 'should generate the correct filter fragment' do
48
- expect(SurveyGizmoSpec::ResourceTest.convert_filters_into_query_string(filters)).to eq("?filter%5Bfield%5D%5B0%5D=istestdata&filter%5Boperator%5D%5B0%5D=%3C%3E&filter%5Bvalue%5D%5B0%5D=1&page=#{page}")
48
+ expect(SurveyGizmoSpec::ResourceTest.send(:filters_to_query_string, filters)).to eq("?filter%5Bfield%5D%5B0%5D=istestdata&filter%5Boperator%5D%5B0%5D=%3C%3E&filter%5Bvalue%5D%5B0%5D=1&page=#{page}")
49
49
  end
50
50
  end
51
51
  end
@@ -72,10 +72,10 @@ describe 'Survey Gizmo Resource' do
72
72
 
73
73
  describe SurveyGizmo::API::Question do
74
74
  let(:base_params) { {survey_id: 1234, page_id: 1} }
75
- let(:create_attributes) { base_params.merge(title: 'Spec Question', type: 'radio', properties: {'required' => true, 'option_sort' => false}) }
75
+ let(:create_attributes) { base_params.merge(title: 'Spec Question', type: 'radio', properties: { 'required' => true, 'option_sort' => false }) }
76
76
  let(:update_attributes) { base_params.merge(title: 'Updated') }
77
77
  let(:first_params) { base_params.merge(id: 1) }
78
- let(:get_attributes) { create_attributes.merge(id: 1) }
78
+ let(:get_attributes) { create_attributes.merge(id: 1).reject { |k, v| k == :properties } }
79
79
  let(:uri_paths) {
80
80
  { :get => '/survey/1234/surveyquestion/1',
81
81
  :create => '/survey/1234/surveypage/1/surveyquestion',
@@ -96,7 +96,7 @@ describe 'Survey Gizmo Resource' do
96
96
  end
97
97
 
98
98
  it 'should have no subquestions' do
99
- expect(described_class.new().sub_questions).to eq([])
99
+ expect(described_class.new.sub_questions).to eq([])
100
100
  end
101
101
 
102
102
  it 'should find the survey' do
@@ -107,7 +107,8 @@ describe 'Survey Gizmo Resource' do
107
107
 
108
108
  context 'with subquestions' do
109
109
  let(:parent_id) { 33 }
110
- let(:question_with_subquestions) { described_class.new(id: parent_id, survey_id: 1234, sub_question_skus: [1, 2])}
110
+ let(:question_with_subquestions) { described_class.new(id: parent_id, survey_id: 1234, sub_question_skus: [1, 2]) }
111
+
111
112
  it 'should have 2 subquestions and they should have the right parent question' do
112
113
  stub_request(:get, /#{@base}/).to_return(json_response(true, get_attributes))
113
114
  expect(question_with_subquestions.sub_questions.size).to eq(2)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: survey-gizmo-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.0
4
+ version: 4.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kabari Hendrick
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2015-10-19 00:00:00.000000000 Z
14
+ date: 2015-12-18 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activesupport