survey-gizmo-ruby 3.0.3 → 4.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +22 -13
- data/lib/survey-gizmo-ruby.rb +1 -0
- data/lib/survey_gizmo/api/option.rb +2 -9
- data/lib/survey_gizmo/api/page.rb +4 -9
- data/lib/survey_gizmo/api/question.rb +3 -9
- data/lib/survey_gizmo/api/response.rb +0 -1
- data/lib/survey_gizmo/api/survey.rb +1 -1
- data/lib/survey_gizmo/multilingual_title.rb +16 -0
- data/lib/survey_gizmo/resource.rb +68 -149
- data/lib/survey_gizmo/rest_response.rb +10 -18
- data/lib/survey_gizmo/version.rb +1 -1
- data/spec/resource_spec.rb +1 -1
- data/spec/support/spec_shared_api_object.rb +2 -2
- data/spec/support/spec_shared_object_with_errors.rb +4 -25
- data/survey-gizmo-ruby.gemspec +0 -2
- metadata +4 -31
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ea584803e0be7b02dc62f295924bc21bac08f0ba
|
4
|
+
data.tar.gz: 183b970af5cdee838e5a64e331e232faf1075541
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 74f7f2967b1dedb1f95d88e47ff03d5c038d7b9ed7e57170c4463d02af57b983c64b50b5f97c46b61c87843e2add15ce97aacf99dbe21422daae3ed0cca15a4b
|
7
|
+
data.tar.gz: 2e000e5f1e3c657959ffd82455355cdf5b9dddf319a850700cf2b3e707377bbad5182eb01cb938a790c1cff8602ed6b27091fffab2ccda89f99aaa401a136cae
|
data/README.md
CHANGED
@@ -6,7 +6,12 @@ Currently supports SurveyGizmo API **v4** (default) and **v3**.
|
|
6
6
|
|
7
7
|
## Versions
|
8
8
|
|
9
|
-
### Major Changes in
|
9
|
+
### Major Changes in 4.x
|
10
|
+
|
11
|
+
* BREAKING CHANGE: There is no more error tracking. If the API gives an error or bad response, an exception will be raised.
|
12
|
+
* BREAKING CHANGE: There is no more ```copy``` method
|
13
|
+
|
14
|
+
### Major Changes in 3.x
|
10
15
|
|
11
16
|
* BREAKING CHANGE: Configuration is completely different
|
12
17
|
* Important Change: Defaults to using the v4 SurveyGizmo API endpoint to take advantage of various API bug fixes (notably team ownership is broken in v3)
|
@@ -32,16 +37,19 @@ require 'survey-gizmo-ruby'
|
|
32
37
|
|
33
38
|
# Configure your credentials
|
34
39
|
SurveyGizmo.configure do |config|
|
35
|
-
config.user = 'still_tippin@
|
36
|
-
config.password = '
|
40
|
+
config.user = 'still_tippin@woodgraingrip.com'
|
41
|
+
config.password = 'it_takes_grindin_to_be_a_king'
|
37
42
|
|
38
43
|
# api_version defaults to v4, but you can probably set to v3 safely if you suspect a bug in v4
|
39
44
|
config.api_version = 'v4'
|
40
45
|
end
|
41
46
|
|
47
|
+
# Retrieve all your surveys
|
48
|
+
surveys = SurveyGizmo::API::Survey.all
|
49
|
+
|
42
50
|
# Retrieve the survey with id: 12345
|
43
51
|
survey = SurveyGizmo::API::Survey.first(id: 12345)
|
44
|
-
survey.title # => My Title
|
52
|
+
survey.title # => "My Title"
|
45
53
|
survey.pages # => [page1, page2,...]
|
46
54
|
survey.number_of_completed_responses # => 355
|
47
55
|
survey.server_has_new_results_since?(Time.now.utc - 2.days) # => true
|
@@ -52,22 +60,21 @@ questions = SurveyGizmo::API::Question.all(survey_id: survey.id, page_id: 1)
|
|
52
60
|
# Or just retrieve all questions for all pages of this survey
|
53
61
|
questions = survey.questions
|
54
62
|
|
55
|
-
# Create a question for your survey
|
63
|
+
# Create a question for your survey. The returned object will be given an :id parameter by SG.
|
56
64
|
question = SurveyGizmo::API::Question.create(survey_id: survey.id, title: 'Do you like ruby?', type: 'checkbox')
|
65
|
+
# Update a question
|
57
66
|
question.title = "Do you LOVE Ruby?"
|
58
|
-
question.save
|
59
|
-
|
60
|
-
|
61
|
-
question.save # => false
|
62
|
-
question.errors # => ['There was an error']
|
67
|
+
question.save
|
68
|
+
# Destroy a question
|
69
|
+
question.destroy
|
63
70
|
|
64
71
|
# Retrieving SurveyResponses for a given survey.
|
65
|
-
# Note that because
|
72
|
+
# Note that because both options are hashes, you need to enclose them both in
|
66
73
|
# braces to page successfully!
|
67
74
|
responses = SurveyGizmo::API::Response.all({ survey_id: survey.id }, { page: 1 })
|
68
75
|
|
69
76
|
# Retrieving page 2 of non test data SurveyResponses
|
70
|
-
filters = {page: 2, filters: [{ field: 'istestdata', operator: '<>', value: 1 }] }
|
77
|
+
filters = { page: 2, filters: [{ field: 'istestdata', operator: '<>', value: 1 }] }
|
71
78
|
responses = SurveyGizmo::API::Response.all({ survey_id: survey_id }, filters)
|
72
79
|
```
|
73
80
|
|
@@ -127,9 +134,11 @@ The [Virtus](https://github.com/solnic/virtus) gem is included to handle the att
|
|
127
134
|
|
128
135
|
## Desirable/Missing Features
|
129
136
|
|
137
|
+
* Better foreign language support
|
138
|
+
* Use Faraday instead of Httparty (partied too hard)
|
139
|
+
* Better specs with VCR/Webmock would be nice.
|
130
140
|
* There are several API objects that are available and not included in this gem. AccountTeams, for instance, has some skeleton code but is untested.
|
131
141
|
* OAuth authentication ability.
|
132
|
-
* Better specs with VCR/Webmock would be nice.
|
133
142
|
|
134
143
|
# Copyright
|
135
144
|
|
data/lib/survey-gizmo-ruby.rb
CHANGED
@@ -11,18 +11,11 @@ module SurveyGizmo; module API
|
|
11
11
|
attribute :value, String
|
12
12
|
attribute :properties, Hash
|
13
13
|
|
14
|
-
|
14
|
+
include SurveyGizmo::MultilingualTitle
|
15
|
+
|
15
16
|
route '/survey/:survey_id/surveypage/:page_id/surveyquestion/:question_id/surveyoption', via: :create
|
16
17
|
route '/survey/:survey_id/surveypage/:page_id/surveyquestion/:question_id/surveyoption/:id', via: [:get, :update, :delete]
|
17
18
|
|
18
|
-
# survey gizmo sends a hash back for :title
|
19
|
-
# @private
|
20
|
-
def title_with_multilingual=(val)
|
21
|
-
self.title_without_multilingual = val.is_a?(Hash) ? val['English'] : val
|
22
|
-
end
|
23
|
-
|
24
|
-
alias_method_chain :title=, :multilingual
|
25
|
-
|
26
19
|
def to_param_options
|
27
20
|
{ id: self.id, survey_id: self.survey_id, page_id: self.page_id, question_id: self.question_id }
|
28
21
|
end
|
@@ -10,9 +10,11 @@ module SurveyGizmo; module API
|
|
10
10
|
attribute :after, Integer
|
11
11
|
attribute :survey_id, Integer
|
12
12
|
|
13
|
+
include SurveyGizmo::MultilingualTitle
|
14
|
+
|
13
15
|
# routing
|
14
|
-
route '/survey/:survey_id/surveypage', :
|
15
|
-
route '/survey/:survey_id/surveypage/:id', :
|
16
|
+
route '/survey/:survey_id/surveypage', via: :create
|
17
|
+
route '/survey/:survey_id/surveypage/:id', via: [:get, :update, :delete]
|
16
18
|
|
17
19
|
def survey
|
18
20
|
@survey ||= SurveyGizmo::API::Survey.first(id: survey_id)
|
@@ -22,13 +24,6 @@ module SurveyGizmo; module API
|
|
22
24
|
@questions ||= SurveyGizmo::API::Question.all(survey_id: survey_id, page_id: id)
|
23
25
|
end
|
24
26
|
|
25
|
-
# survey gizmo sends a hash back for :title
|
26
|
-
def title_with_multilingual=(val)
|
27
|
-
self.title_without_multilingual = val.is_a?(Hash) ? val : { 'English' => val }
|
28
|
-
end
|
29
|
-
|
30
|
-
alias_method_chain :title=, :multilingual
|
31
|
-
|
32
27
|
def to_param_options
|
33
28
|
{ id: self.id, survey_id: self.survey_id }
|
34
29
|
end
|
@@ -4,7 +4,7 @@ module SurveyGizmo; module API
|
|
4
4
|
include SurveyGizmo::Resource
|
5
5
|
|
6
6
|
attribute :id, Integer
|
7
|
-
attribute :title,
|
7
|
+
attribute :title, Hash
|
8
8
|
attribute :type, String
|
9
9
|
attribute :description, String
|
10
10
|
attribute :shortname, String
|
@@ -17,6 +17,8 @@ module SurveyGizmo; module API
|
|
17
17
|
|
18
18
|
alias_attribute :_subtype, :type
|
19
19
|
|
20
|
+
include SurveyGizmo::MultilingualTitle
|
21
|
+
|
20
22
|
route '/survey/:survey_id/surveyquestion/:id', via: :get
|
21
23
|
route '/survey/:survey_id/surveypage/:page_id/surveyquestion', via: :create
|
22
24
|
route '/survey/:survey_id/surveypage/:page_id/surveyquestion/:id', via: [:update, :delete]
|
@@ -38,14 +40,6 @@ module SurveyGizmo; module API
|
|
38
40
|
.each { |subquestion| subquestion.parent_question_id = id }
|
39
41
|
end
|
40
42
|
|
41
|
-
# survey gizmo sends a hash back for :title
|
42
|
-
# @private
|
43
|
-
def title_with_multilingual=(val)
|
44
|
-
self.title_without_multilingual = val.is_a?(Hash) ? val['English'] : val
|
45
|
-
end
|
46
|
-
|
47
|
-
alias_method_chain :title=, :multilingual
|
48
|
-
|
49
43
|
# @see SurveyGizmo::Resource#to_param_options
|
50
44
|
def to_param_options
|
51
45
|
{ id: self.id, survey_id: self.survey_id, page_id: self.page_id }
|
@@ -17,7 +17,6 @@ module SurveyGizmo; module API
|
|
17
17
|
attribute :url, Hash # READ-ONLY
|
18
18
|
attribute :answers, Hash # READ-ONLY
|
19
19
|
|
20
|
-
# routing
|
21
20
|
route '/survey/:survey_id/surveyresponse', via: :create
|
22
21
|
route '/survey/:survey_id/surveyresponse/:id', via: [:get, :update, :delete]
|
23
22
|
|
@@ -53,7 +53,7 @@ module SurveyGizmo; module API
|
|
53
53
|
filters = [{
|
54
54
|
field: 'datesubmitted',
|
55
55
|
operator: '>=',
|
56
|
-
value: time.in_time_zone(
|
56
|
+
value: time.in_time_zone('Eastern Time (US & Canada)').strftime('%Y-%m-%d %H:%M:%S')
|
57
57
|
}]
|
58
58
|
responses = SurveyGizmo::API::Response.all({ survey_id: self.id }, { page: 1, filters: filters })
|
59
59
|
responses.size > 0
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# Inclusion of this module must come AFTER the virtus call:
|
2
|
+
# attribute :title
|
3
|
+
|
4
|
+
module SurveyGizmo
|
5
|
+
module MultilingualTitle
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
alias_method_chain :title=, :multilingual
|
10
|
+
end
|
11
|
+
|
12
|
+
def title_with_multilingual=(val)
|
13
|
+
self.title_without_multilingual = val.is_a?(Hash) ? val['English'] : val
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -16,116 +16,52 @@ module SurveyGizmo
|
|
16
16
|
@descendants ||= Set.new
|
17
17
|
end
|
18
18
|
|
19
|
-
# These are methods that every API resource
|
20
|
-
# in Survey Gizmo
|
19
|
+
# These are methods that every API resource can use to access resources in SurveyGizmo
|
21
20
|
module ClassMethods
|
22
|
-
|
23
|
-
# Convert a [Hash] of filters into a query string
|
24
|
-
# @param [Hash] filters - simple pagination or other options at the top level, and surveygizmo "filters" at the :filters key
|
25
|
-
# @return [String]
|
26
|
-
#
|
27
|
-
# example input: { page: 2, filters: [{:field=>"istestdata", :operator=>"<>", :value=>1}] }
|
28
|
-
#
|
29
|
-
# The top level keys (e.g. page, resultsperpage) get simply encoded in the url, while the
|
30
|
-
# contents of the array of hashes passed at filters[:filters] gets turned into the format
|
31
|
-
# SurveyGizmo expects for its internal filtering, for example:
|
32
|
-
#
|
33
|
-
# filter[field][0]=istestdata&filter[operator][0]=<>&filter[value][0]=1
|
34
|
-
def convert_filters_into_query_string(filters = nil)
|
35
|
-
return '' unless filters && filters.size > 0
|
36
|
-
|
37
|
-
output_filters = filters[:filters] || []
|
38
|
-
filter_hash = {}
|
39
|
-
output_filters.each_with_index do |filter,i|
|
40
|
-
filter_hash.merge!(
|
41
|
-
"filter[field][#{i}]".to_sym => "#{filter[:field]}",
|
42
|
-
"filter[operator][#{i}]".to_sym => "#{filter[:operator]}",
|
43
|
-
"filter[value][#{i}]".to_sym => "#{filter[:value]}",
|
44
|
-
)
|
45
|
-
end
|
46
|
-
simple_filters = filters.reject { |k,v| k == :filters }
|
47
|
-
filter_hash.merge!(simple_filters)
|
48
|
-
|
49
|
-
uri = Addressable::URI.new
|
50
|
-
uri.query_values = filter_hash
|
51
|
-
"?#{uri.query}"
|
52
|
-
end
|
53
|
-
|
54
|
-
# Get a list of resources
|
55
|
-
# @param [Hash] conditions
|
56
|
-
# @param [Hash] filters
|
57
|
-
# @return [Array] of objects of this class
|
21
|
+
# Get an array of resources
|
58
22
|
def all(conditions = {}, filters = nil)
|
59
23
|
response = RestResponse.new(SurveyGizmo.get(handle_route(:create, conditions) + convert_filters_into_query_string(filters)))
|
60
|
-
|
61
|
-
_collection = response.data.map { |datum| datum.is_a?(Hash) ? self.new(datum) : datum }
|
62
|
-
|
63
|
-
# Add in the properties from the conditions hash because many of the important ones (like survey_id) are
|
64
|
-
# not often part of the SurveyGizmo returned data
|
65
|
-
conditions.keys.each do |k|
|
66
|
-
if conditions[k] && instance_methods.include?(k)
|
67
|
-
_collection.each { |c| c[k] ||= conditions[k] }
|
68
|
-
end
|
69
|
-
end
|
24
|
+
_collection = response.data.map { |datum| datum.is_a?(Hash) ? self.new(datum) : datum }
|
70
25
|
|
71
|
-
|
72
|
-
|
73
|
-
|
26
|
+
# Add in the properties from the conditions hash because many of the important ones (like survey_id) are
|
27
|
+
# 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] }
|
74
31
|
end
|
32
|
+
end
|
75
33
|
|
76
|
-
|
77
|
-
|
78
|
-
|
34
|
+
# Sub questions are not pulled by default so we have to retrieve them manually
|
35
|
+
# SurveyGizmo claims they will fix this bug and eventually all questions will be
|
36
|
+
# returned in one request.
|
37
|
+
if self == SurveyGizmo::API::Question
|
38
|
+
_collection += _collection.map { |question| question.sub_questions }.flatten
|
79
39
|
end
|
40
|
+
|
41
|
+
_collection
|
80
42
|
end
|
81
43
|
|
82
|
-
#
|
83
|
-
# @param [Hash] conditions
|
84
|
-
# @param [Hash] filters
|
85
|
-
# @return [Object, nil]
|
44
|
+
# Retrieve a single resource.
|
86
45
|
def first(conditions = {}, filters = nil)
|
87
46
|
response = RestResponse.new(SurveyGizmo.get(handle_route(:get, conditions) + convert_filters_into_query_string(filters)))
|
88
47
|
# Add in the properties from the conditions hash because many of the important ones (like survey_id) are
|
89
48
|
# not often part of the SurveyGizmo's returned data
|
90
|
-
|
49
|
+
new(conditions.merge(response.data))
|
91
50
|
end
|
92
51
|
|
93
|
-
# Create a new resource
|
94
|
-
# @param [Hash] attributes
|
95
|
-
# @return [Resource]
|
96
|
-
# The newly created Resource instance
|
52
|
+
# Create a new resource. Returns the newly created Resource instance.
|
97
53
|
def create(attributes = {})
|
98
54
|
resource = new(attributes)
|
99
55
|
resource.create_record_in_surveygizmo
|
100
56
|
resource
|
101
57
|
end
|
102
58
|
|
103
|
-
#
|
104
|
-
# @param [Integer] id
|
105
|
-
# @param [Hash] attributes
|
106
|
-
# @return [Resource]
|
107
|
-
# The newly created resource instance
|
108
|
-
def copy(attributes = {})
|
109
|
-
attributes[:copy] = true
|
110
|
-
resource = new(attributes)
|
111
|
-
resource.__send__(:_copy)
|
112
|
-
resource
|
113
|
-
end
|
114
|
-
|
115
|
-
# Deleted the Resource from Survey Gizmo
|
116
|
-
# @param [Hash] conditions
|
117
|
-
# @return [Boolean]
|
59
|
+
# Delete resources
|
118
60
|
def destroy(conditions)
|
119
|
-
RestResponse.new(SurveyGizmo.delete(handle_route(:delete, conditions)))
|
61
|
+
RestResponse.new(SurveyGizmo.delete(handle_route(:delete, conditions)))
|
120
62
|
end
|
121
63
|
|
122
64
|
# Define the path where a resource is located
|
123
|
-
# @param [String] path
|
124
|
-
# the path in Survey Gizmo for the resource
|
125
|
-
# @param [Hash] options
|
126
|
-
# @option options [Array] :via
|
127
|
-
# which is `:get`, `:create`, `:update`, `:delete`, or `:any`
|
128
|
-
# @scope class
|
129
65
|
def route(path, options)
|
130
66
|
methods = options[:via]
|
131
67
|
methods = [:get, :create, :update, :delete] if methods == :any
|
@@ -134,67 +70,84 @@ module SurveyGizmo
|
|
134
70
|
|
135
71
|
# This method replaces the :page_id, :survey_id, etc strings defined in each model's URI routes with the
|
136
72
|
# values being passed in interpolation hash with the same keys.
|
137
|
-
# @api private
|
138
73
|
def handle_route(key, interpolation_hash)
|
139
74
|
path = @paths[key]
|
140
|
-
|
141
|
-
|
75
|
+
fail "No routes defined for `#{key}` in #{self.name}" unless path
|
76
|
+
fail "User/password hash not setup!" if SurveyGizmo.default_params.empty?
|
142
77
|
|
143
78
|
path.gsub(/:(\w+)/) do |m|
|
144
79
|
raise(SurveyGizmo::URLError, "Missing RESTful parameters in request: `#{m}`") unless interpolation_hash[$1.to_sym]
|
145
80
|
interpolation_hash[$1.to_sym]
|
146
81
|
end
|
147
82
|
end
|
83
|
+
|
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)
|
96
|
+
return '' unless filters && filters.size > 0
|
97
|
+
|
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
|
+
)
|
106
|
+
end
|
107
|
+
simple_filters = filters.reject { |k,v| k == :filters }
|
108
|
+
filter_hash.merge!(simple_filters)
|
109
|
+
|
110
|
+
uri = Addressable::URI.new
|
111
|
+
uri.query_values = filter_hash
|
112
|
+
"?#{uri.query}"
|
113
|
+
end
|
148
114
|
end
|
149
115
|
|
150
|
-
# Save the
|
116
|
+
# Save the resource to SurveyGizmo
|
151
117
|
def save
|
152
118
|
if id
|
153
119
|
# Then it's an update, because we already know the surveygizmo assigned id
|
154
|
-
|
155
|
-
@latest_response.ok?
|
120
|
+
RestResponse.new(SurveyGizmo.post(handle_route(:update), query: self.attributes_without_blanks))
|
156
121
|
else
|
157
122
|
create_record_in_surveygizmo
|
158
123
|
end
|
159
124
|
end
|
160
125
|
|
161
|
-
#
|
162
|
-
# @return [self, false]
|
163
|
-
# Returns the object, if saved. Otherwise returns false.
|
126
|
+
# Repopulate the attributes based on what is on SurveyGizmo's servers
|
164
127
|
def reload
|
165
|
-
|
166
|
-
|
167
|
-
self.attributes = @latest_response['data']
|
168
|
-
self
|
169
|
-
else
|
170
|
-
false
|
171
|
-
end
|
128
|
+
self.attributes = RestResponse.new(SurveyGizmo.get(handle_route(:get))).data
|
129
|
+
self
|
172
130
|
end
|
173
131
|
|
174
|
-
#
|
175
|
-
# @return [Boolean]
|
132
|
+
# Delete the Resource from Survey Gizmo
|
176
133
|
def destroy
|
177
|
-
|
178
|
-
|
179
|
-
@latest_response.ok?
|
180
|
-
else
|
181
|
-
false
|
182
|
-
end
|
134
|
+
fail "No id; can't delete #{self.inspect}!" unless id
|
135
|
+
RestResponse.new(SurveyGizmo.delete(handle_route(:delete)))
|
183
136
|
end
|
184
137
|
|
185
138
|
# Sets the hash that will be used to interpolate values in routes. It needs to be defined per model.
|
186
139
|
# @return [Hash] a hash of the values needed in routing
|
187
140
|
def to_param_options
|
188
|
-
|
141
|
+
fail "Define #to_param_options in #{self.class.name}"
|
189
142
|
end
|
190
143
|
|
191
|
-
#
|
192
|
-
|
193
|
-
|
194
|
-
|
144
|
+
# Returns itself if successfully saved, but with attributes added by SurveyGizmo
|
145
|
+
def create_record_in_surveygizmo(attributes = {})
|
146
|
+
rest_response = RestResponse.new(SurveyGizmo.put(handle_route(:create), query: self.attributes_without_blanks))
|
147
|
+
self.attributes = rest_response.data
|
148
|
+
self
|
195
149
|
end
|
196
150
|
|
197
|
-
# @visibility private
|
198
151
|
def inspect
|
199
152
|
if ENV['GIZMO_DEBUG']
|
200
153
|
ap "CLASS: #{self.class}"
|
@@ -220,18 +173,6 @@ module SurveyGizmo
|
|
220
173
|
"#<#{self.class.name}:#{self.object_id}>\n#{attribute_strings.join()}"
|
221
174
|
end
|
222
175
|
|
223
|
-
# Returns itself if successfully saved, but with attributes added by SurveyGizmo
|
224
|
-
def create_record_in_surveygizmo(attributes = {})
|
225
|
-
http = RestResponse.new(SurveyGizmo.put(handle_route(:create), query: self.attributes_without_blanks))
|
226
|
-
handle_response(http)
|
227
|
-
if http.ok?
|
228
|
-
self.attributes = http.data
|
229
|
-
self
|
230
|
-
else
|
231
|
-
false
|
232
|
-
end
|
233
|
-
end
|
234
|
-
|
235
176
|
protected
|
236
177
|
|
237
178
|
def attributes_without_blanks
|
@@ -243,27 +184,5 @@ module SurveyGizmo
|
|
243
184
|
def handle_route(key)
|
244
185
|
self.class.handle_route(key, to_param_options)
|
245
186
|
end
|
246
|
-
|
247
|
-
def handle_response(rest_response, &block)
|
248
|
-
@latest_response = rest_response
|
249
|
-
if @latest_response.ok?
|
250
|
-
self.errors.clear
|
251
|
-
true
|
252
|
-
else
|
253
|
-
errors << @latest_response.message
|
254
|
-
false
|
255
|
-
end
|
256
|
-
end
|
257
|
-
|
258
|
-
def _copy(attributes = {})
|
259
|
-
http = RestResponse.new(SurveyGizmo.post(handle_route(:update), query: self.attributes_without_blanks))
|
260
|
-
handle_response(http) do
|
261
|
-
if http.ok?
|
262
|
-
self.attributes = http.data
|
263
|
-
else
|
264
|
-
false
|
265
|
-
end
|
266
|
-
end
|
267
|
-
end
|
268
187
|
end
|
269
188
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# This class normalizes the response returned by Survey Gizmo
|
1
|
+
# This class normalizes the response returned by Survey Gizmo, including validation.
|
2
2
|
class RestResponse
|
3
3
|
attr_accessor :raw_response
|
4
4
|
attr_accessor :parsed_response
|
@@ -6,16 +6,20 @@ class RestResponse
|
|
6
6
|
def initialize(rest_response)
|
7
7
|
@raw_response = rest_response
|
8
8
|
@parsed_response = rest_response.parsed_response
|
9
|
+
|
10
|
+
if ENV['GIZMO_DEBUG']
|
11
|
+
ap 'SG Response: '
|
12
|
+
ap @parsed_response
|
13
|
+
end
|
14
|
+
|
15
|
+
fail "Bad response: #{rest_response.inspect}" unless @parsed_response['result_ok'] && @parsed_response['result_ok'].to_s.downcase == 'true'
|
9
16
|
return unless data
|
10
17
|
|
11
18
|
# Handle really crappy [] notation in SG API, so far just in SurveyResponse
|
12
19
|
(data.is_a?(Array) ? data : [data]).each do |datum|
|
13
|
-
|
14
|
-
# SurveyGizmo returns date information in EST, but does not
|
15
|
-
# provide time zone information in their API responses.
|
16
|
-
#
|
17
|
-
# See https://surveygizmov4.helpgizmo.com/help/article/link/date-and-time-submitted
|
18
20
|
unless datum['datesubmitted'].blank?
|
21
|
+
# SurveyGizmo returns date information in EST but does not provide time zone information.
|
22
|
+
# See https://surveygizmov4.helpgizmo.com/help/article/link/date-and-time-submitted
|
19
23
|
datum['datesubmitted'] = datum['datesubmitted'] + ' EST'
|
20
24
|
end
|
21
25
|
|
@@ -41,18 +45,6 @@ class RestResponse
|
|
41
45
|
end
|
42
46
|
end
|
43
47
|
|
44
|
-
def ok?
|
45
|
-
if ENV['GIZMO_DEBUG']
|
46
|
-
ap 'SG Response: '
|
47
|
-
ap @parsed_response
|
48
|
-
end
|
49
|
-
|
50
|
-
if @parsed_response['result_ok'] && @parsed_response['result_ok'].to_s.downcase == 'false' && @parsed_response['message'] && @parsed_response['code'] && @parsed_response['message'] =~ /service/i
|
51
|
-
raise Exception, "#{@parsed_response['message']}: #{@parsed_response['code']}"
|
52
|
-
end
|
53
|
-
@parsed_response['result_ok'] && @parsed_response['result_ok'].to_s.downcase == 'true'
|
54
|
-
end
|
55
|
-
|
56
48
|
# The parsed JSON data of the response
|
57
49
|
def data
|
58
50
|
@_data ||= @parsed_response['data']
|
data/lib/survey_gizmo/version.rb
CHANGED
data/spec/resource_spec.rb
CHANGED
@@ -135,7 +135,7 @@ describe 'Survey Gizmo Resource' do
|
|
135
135
|
end
|
136
136
|
|
137
137
|
describe SurveyGizmo::API::Page do
|
138
|
-
let(:create_attributes) { {:survey_id => 1234, :title =>
|
138
|
+
let(:create_attributes) { {:survey_id => 1234, :title => 'Spec Page' } }
|
139
139
|
let(:get_attributes) { create_attributes.merge(:id => 1) }
|
140
140
|
let(:update_attributes) { {:survey_id => 1234, :title => 'Updated'} }
|
141
141
|
let(:first_params) { {:id => 1, :survey_id => 1234 } }
|
@@ -39,7 +39,7 @@ shared_examples_for 'an API object' do
|
|
39
39
|
|
40
40
|
it "should return false if the request fails" do
|
41
41
|
stub_request(:get, /#{@base}/).to_return(json_response(false, "something is wrong"))
|
42
|
-
described_class.first(first_params).
|
42
|
+
expect { described_class.first(first_params) }.to raise_error
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
@@ -56,7 +56,7 @@ shared_examples_for 'an API object' do
|
|
56
56
|
|
57
57
|
it "cannot be destroyed if new" do
|
58
58
|
@obj.id = nil
|
59
|
-
@obj.destroy.
|
59
|
+
expect { @obj.destroy }.to raise_error
|
60
60
|
end
|
61
61
|
end
|
62
62
|
|
@@ -3,31 +3,10 @@ shared_examples_for 'an object with errors' do
|
|
3
3
|
stub_request(:any, /#{@base}/).to_return(json_response(false, 'There was an error!'))
|
4
4
|
end
|
5
5
|
|
6
|
-
context
|
7
|
-
it
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
context "instance methods" do
|
12
|
-
before(:each) do
|
13
|
-
@obj = described_class.new(create_attributes)
|
14
|
-
end
|
15
|
-
|
16
|
-
it "should have an errors array" do
|
17
|
-
@obj.errors.should == []
|
18
|
-
end
|
19
|
-
|
20
|
-
it "should add errors on failed requests" do
|
21
|
-
@obj.save.should == false
|
22
|
-
@obj.errors.should include('There was an error!')
|
23
|
-
end
|
24
|
-
|
25
|
-
it "should empty the errors array if object gets saved" do
|
26
|
-
stub_request(:any, /#{@base}/).to_return(json_response(false, 'There was an error!'), json_response(true, get_attributes))
|
27
|
-
@obj.save.should == false
|
28
|
-
@obj.errors.should_not be_empty
|
29
|
-
@obj.save.id.nil?.should == false
|
30
|
-
@obj.errors.should be_empty
|
6
|
+
context 'class methods' do
|
7
|
+
it 'should raise errors' do
|
8
|
+
expect { described_class.first(get_attributes) }.to raise_error
|
9
|
+
expect { described_class.all(get_attributes) }.to raise_error
|
31
10
|
end
|
32
11
|
end
|
33
12
|
end
|
data/survey-gizmo-ruby.gemspec
CHANGED
@@ -22,8 +22,6 @@ Gem::Specification.new do |gem|
|
|
22
22
|
gem.add_dependency 'i18n'
|
23
23
|
gem.add_dependency 'virtus', '>= 1.0.0'
|
24
24
|
|
25
|
-
gem.add_development_dependency 'bluecloth'
|
26
|
-
gem.add_development_dependency 'net-http-spy'
|
27
25
|
gem.add_development_dependency 'rspec', '~> 2.11.0'
|
28
26
|
gem.add_development_dependency 'rake'
|
29
27
|
gem.add_development_dependency 'webmock'
|
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
|
+
version: 4.0.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-
|
14
|
+
date: 2015-10-19 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: activesupport
|
@@ -97,34 +97,6 @@ dependencies:
|
|
97
97
|
- - ">="
|
98
98
|
- !ruby/object:Gem::Version
|
99
99
|
version: 1.0.0
|
100
|
-
- !ruby/object:Gem::Dependency
|
101
|
-
name: bluecloth
|
102
|
-
requirement: !ruby/object:Gem::Requirement
|
103
|
-
requirements:
|
104
|
-
- - ">="
|
105
|
-
- !ruby/object:Gem::Version
|
106
|
-
version: '0'
|
107
|
-
type: :development
|
108
|
-
prerelease: false
|
109
|
-
version_requirements: !ruby/object:Gem::Requirement
|
110
|
-
requirements:
|
111
|
-
- - ">="
|
112
|
-
- !ruby/object:Gem::Version
|
113
|
-
version: '0'
|
114
|
-
- !ruby/object:Gem::Dependency
|
115
|
-
name: net-http-spy
|
116
|
-
requirement: !ruby/object:Gem::Requirement
|
117
|
-
requirements:
|
118
|
-
- - ">="
|
119
|
-
- !ruby/object:Gem::Version
|
120
|
-
version: '0'
|
121
|
-
type: :development
|
122
|
-
prerelease: false
|
123
|
-
version_requirements: !ruby/object:Gem::Requirement
|
124
|
-
requirements:
|
125
|
-
- - ">="
|
126
|
-
- !ruby/object:Gem::Version
|
127
|
-
version: '0'
|
128
100
|
- !ruby/object:Gem::Dependency
|
129
101
|
name: rspec
|
130
102
|
requirement: !ruby/object:Gem::Requirement
|
@@ -206,6 +178,7 @@ files:
|
|
206
178
|
- lib/survey_gizmo/api/survey.rb
|
207
179
|
- lib/survey_gizmo/api/survey_campaign.rb
|
208
180
|
- lib/survey_gizmo/configuration.rb
|
181
|
+
- lib/survey_gizmo/multilingual_title.rb
|
209
182
|
- lib/survey_gizmo/resource.rb
|
210
183
|
- lib/survey_gizmo/rest_response.rb
|
211
184
|
- lib/survey_gizmo/survey_gizmo.rb
|
@@ -242,7 +215,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
242
215
|
version: '0'
|
243
216
|
requirements: []
|
244
217
|
rubyforge_project:
|
245
|
-
rubygems_version: 2.4.5
|
218
|
+
rubygems_version: 2.4.5.1
|
246
219
|
signing_key:
|
247
220
|
specification_version: 4
|
248
221
|
summary: Gem to use the SurveyGizmo.com REST API, v3+
|