survey-gizmo-ruby 2.0.1 → 3.0.3

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: cc7ec77f5e304d1dec270f479b3caadc795e75e1
4
- data.tar.gz: 1c65b8c0bf95015e3c65be784b78bdd916793100
3
+ metadata.gz: ff4381315b3f5ce301adca67c272f4966b4c5e8e
4
+ data.tar.gz: 21531bfec2ce56c5946b14ffd7a7f011d177ceeb
5
5
  SHA512:
6
- metadata.gz: 77bf4d152eeb999f2a6a62f4a233b4d8b42e8c47038e54864ccd42947024383e790785f41fa618ec06f8f50d7add669e0f0c96666092e6a3c45155a3073151c7
7
- data.tar.gz: e87d4acc9fe981fd5653ceb2f7197e144c7d5d3c5aedbe8f2d71afc7d0c21b1d5036fa66d5bb062c04cc331cfd48a3054549a74ee5bedb3df7c726ef30a29521
6
+ metadata.gz: 453a7400ff86b0f8ac68d525b3e5dad504c46bd20fb002e9dfb4bb8f5964f9c907c781a4afb7f3c4a4b0ab423a5dd3716f2b8d2c4d71620d4bcf25786e2eb2ee
7
+ data.tar.gz: 7b4d3ccc7cdde1a2fdeba1883d91669ae87c98f982ad78149830820c52b24201bcd287442c3e487db7caaef5c268ac1b19b5ab88562213d88a08ee59f98a8765
data/README.md CHANGED
@@ -1,27 +1,23 @@
1
- #### WARNING:
1
+ # Survey Gizmo (ruby)
2
2
 
3
- This is version `2.0.0` of the gem, which introduces breaking changes. People who need backwards compatibility can use `1.0.4`.
3
+ Integrate with the [Survey Gizmo API](http://apisurveygizmo.helpgizmo.com/help) using an ActiveModel style interface.
4
4
 
5
- What's fixed/better:
6
- * Filtering of requests is implemented
7
- * Requests for the questions in a survey now retrieve ALL of the questions, even the sneaky sub_questions that weren't directly returned to a basic API request.
5
+ Currently supports SurveyGizmo API **v4** (default) and **v3**.
8
6
 
9
- What broke:
10
- * SurveyGizmo objects (Survey, SurveyQuestion, etc) no longer track their own state (:new, :zombie, :saved, etc etc) - state tracking was useless anyways because exceptions always were raised on errors.
11
- * There is no more SurveyGizmo::Collection class... now there are just Arrays.
12
- * There is no more lazy loading/instantiation.
7
+ ## Versions
13
8
 
14
- What's different:
15
- * Variables are not autoloaded. That is to say when you load a Survey, the API request that loads the associated questions is not executed until you ask for them with the #questions method
9
+ ### Major Changes in 3.0
16
10
 
17
- Also a past warning from another author:
18
- > SurveyGizmo doesn't test their REST API when they roll out changes. They don't publish a list of active defects, and when you call/email for support it is unlikely you will geto a person that knows anything about programming or the REST API. You can't talk to level 2 support, although they might offer you a discount on their paid consulting rates if the problem persists for more than a few weeks.
11
+ * BREAKING CHANGE: Configuration is completely different
12
+ * 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)
19
13
 
20
- chorn@chorn.com 2013-03-15
14
+ ### Old versions
21
15
 
22
- # Survey Gizmo (ruby)
16
+ [Version 2.0.1 for the v3 API is here](https://github.com/RipTheJacker/survey-gizmo-ruby/releases/tag/v2.0.1)
17
+
18
+ [Version 1.0.5 for the v2 API is here](https://github.com/RipTheJacker/survey-gizmo-ruby/releases/tag/v1.0.5)
23
19
 
24
- Integrate with the [Survey Gizmo API](http://apisurveygizmo.helpgizmo.com/help) using an ActiveModel style interface. We currently support rest API **v3**. If you want to use version 1 of the API, please use gem version ~0.7.0
20
+ [Version 0.7.0 for the v1 API is here](https://github.com/RipTheJacker/survey-gizmo-ruby/releases/tag/v0.7.0)
25
21
 
26
22
  ## Installation
27
23
 
@@ -34,13 +30,27 @@ gem 'survey-gizmo-ruby'
34
30
  ```ruby
35
31
  require 'survey-gizmo-ruby'
36
32
 
37
- # somewhere in your app define your survey gizmo login credentials.
38
- SurveyGizmo.setup(user: 'you@somewhere.com', password: 'mypassword')
33
+ # Configure your credentials
34
+ SurveyGizmo.configure do |config|
35
+ config.user = 'still_tippin@test.com'
36
+ config.password = 'ittakesgrindintobeaking'
37
+
38
+ # api_version defaults to v4, but you can probably set to v3 safely if you suspect a bug in v4
39
+ config.api_version = 'v4'
40
+ end
39
41
 
40
42
  # Retrieve the survey with id: 12345
41
43
  survey = SurveyGizmo::API::Survey.first(id: 12345)
42
44
  survey.title # => My Title
43
45
  survey.pages # => [page1, page2,...]
46
+ survey.number_of_completed_responses # => 355
47
+ survey.server_has_new_results_since?(Time.now.utc - 2.days) # => true
48
+ survey.team_names # => ['Development', 'Test']
49
+
50
+ # Retrieving Questions for a given survey. Note that page_id is a required parameter.
51
+ questions = SurveyGizmo::API::Question.all(survey_id: survey.id, page_id: 1)
52
+ # Or just retrieve all questions for all pages of this survey
53
+ questions = survey.questions
44
54
 
45
55
  # Create a question for your survey
46
56
  question = SurveyGizmo::API::Question.create(survey_id: survey.id, title: 'Do you like ruby?', type: 'checkbox')
@@ -51,23 +61,23 @@ question.save # => question # (but now with the id assigned by SurveyGizmo as th
51
61
  question.save # => false
52
62
  question.errors # => ['There was an error']
53
63
 
54
- # Retrieving Questions for a given survey. Note that page_id is a required parameter.
55
- questions = SurveyGizmo::API::Question.all(survey_id: survey.id, page_id: 1)
56
- # Or just retrieve all questions for all pages of this survey
57
- questions = survey.questions
58
-
59
64
  # Retrieving SurveyResponses for a given survey.
60
- # Note that because of both options being hashes, you need to enclose them both in braces to page successfully!
61
- responses = SurveyGizmo::API::Response.all({survey_id: survey.id}, {page: 1})
65
+ # Note that because of both options being hashes, you need to enclose them both in
66
+ # braces to page successfully!
67
+ responses = SurveyGizmo::API::Response.all({ survey_id: survey.id }, { page: 1 })
62
68
 
63
69
  # Retrieving page 2 of non test data SurveyResponses
64
- filters = {page: 2, filters: [{field: 'istestdata', operator: '<>', value: 1}] }
65
- responses = SurveyGizmo::API::Response.all({survey_id: survey_id}, filters)
70
+ filters = {page: 2, filters: [{ field: 'istestdata', operator: '<>', value: 1 }] }
71
+ responses = SurveyGizmo::API::Response.all({ survey_id: survey_id }, filters)
66
72
  ```
67
73
 
74
+ ## On API Timeouts
75
+
76
+ API timeouts are a regular occurrence with the SurveyGizmo API. At Lumos Labs we use our own [Pester gem](https://github.com/lumoslabs/pester) to manage retry strategies. It might work for you.
77
+
68
78
  ## Debugging
69
79
 
70
- The GIZMO_DEBUG environment variable will trigger full printouts of SurveyGizmo's HTTP responses and variable introspection for almost everything
80
+ The GIZMO_DEBUG environment variable will trigger full printouts of SurveyGizmo's HTTP responses and variable introspection for almost everything.
71
81
 
72
82
  ```bash
73
83
  cd /my/app
@@ -77,14 +87,14 @@ bundle exec rails whatever
77
87
 
78
88
  ## Adding API Objects
79
89
 
80
- Currently, the following API objects are included in the gem: `Survey`, `Question`, `Option`, `Page`, `Response`, `EmailMessage`, `SurveyCampaign`, `Contact`. If you want to use something that isn't included you can easily write a class that handles it. Here's an example of the how to do so:
90
+ Currently, the following API objects are included in the gem: `Survey`, `Question`, `Option`, `Page`, `Response`, `EmailMessage`, `SurveyCampaign`, `Contact`, `AccountTeams`. If you want to use something that isn't included you can easily write a class that handles it. Here's an example of the how to do so:
81
91
 
82
92
  ```ruby
83
93
  class SomeObject
84
94
  # the base where most of the methods for handling the API are stored
85
95
  include SurveyGizmo::Resource
86
96
 
87
- # the attribtues the object should respond to
97
+ # the attributes the object should respond to
88
98
  attribute :id, Integer
89
99
  attribute :title, String
90
100
  attribute :status, String
@@ -92,12 +102,13 @@ class SomeObject
92
102
  attribute :created_on, DateTime
93
103
 
94
104
  # defing the paths used to retrieve/set info
95
- route '/something/:id', :via => [:get, :update, :delete]
96
- route '/something', :via => :create
105
+ route '/something/:id', via: [:get, :update, :delete]
106
+ route '/something', via: :create
97
107
 
98
- # this must be defined with the params that would be included in any route
108
+ # this must be defined with the params that would be included in any route related
109
+ # to an instance of SomeObject
99
110
  def to_param_options
100
- {:id => self.id}
111
+ { id: self.id }
101
112
  end
102
113
  end
103
114
  ```
@@ -110,16 +121,15 @@ The [Virtus](https://github.com/solnic/virtus) gem is included to handle the att
110
121
  * Take a gander at the github issues beforehand
111
122
  * Fork the project
112
123
  * Start a feature/bugfix branch and hack away
113
- * Make sure to add tests for it!!!!
124
+ * Make sure to add specs for it!!!!
114
125
  * Submit a pull request
115
126
  * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
116
127
 
117
- ## Missing Features
118
-
119
- * It would be nice to implement enumerable on the Question and (especially) Response objects so people don't have to implement their own paging
120
- * There are several API objects that are available and not included in this gem.
121
- * It is also missing OAuth authentication ability.
128
+ ## Desirable/Missing Features
122
129
 
130
+ * 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
+ * OAuth authentication ability.
132
+ * Better specs with VCR/Webmock would be nice.
123
133
 
124
134
  # Copyright
125
135
 
@@ -1 +1,26 @@
1
- require 'survey_gizmo/survey_gizmo'
1
+ require 'active_support/core_ext/string'
2
+ require 'active_support/core_ext/module'
3
+ require 'active_support/core_ext/hash'
4
+ require 'active_support/core_ext/object/blank'
5
+ require 'active_support/concern'
6
+ require 'awesome_print'
7
+ require 'virtus'
8
+ require 'httparty'
9
+ require 'digest/md5'
10
+
11
+ require 'survey_gizmo/version'
12
+ require 'survey_gizmo/survey_gizmo'
13
+ require 'survey_gizmo/configuration'
14
+
15
+ require 'survey_gizmo/resource'
16
+ require 'survey_gizmo/rest_response'
17
+
18
+ require 'survey_gizmo/api/account_teams'
19
+ require 'survey_gizmo/api/contact'
20
+ require 'survey_gizmo/api/email_message'
21
+ require 'survey_gizmo/api/option'
22
+ require 'survey_gizmo/api/page'
23
+ require 'survey_gizmo/api/question'
24
+ require 'survey_gizmo/api/response'
25
+ require 'survey_gizmo/api/survey'
26
+ require 'survey_gizmo/api/survey_campaign'
@@ -0,0 +1,24 @@
1
+ # This REST endpoint is only available to accounts with admin privileges
2
+ # This code is untested.
3
+
4
+ module SurveyGizmo
5
+ module API
6
+ class AccountTeams
7
+ include SurveyGizmo::Resource
8
+
9
+ attribute :id, Integer
10
+ attribute :teamid, Integer
11
+ attribute :teamname, String
12
+ attribute :color, String
13
+ attribute :default_role, String
14
+ attribute :status, String
15
+
16
+ route '/accountteams/:id', via: [:get, :update, :delete]
17
+ route '/accountteams', via: :create
18
+
19
+ def to_param_options
20
+ { id: self.id }
21
+ end
22
+ end
23
+ end
24
+ end
@@ -3,8 +3,6 @@ module SurveyGizmo; module API
3
3
  class Contact
4
4
  include SurveyGizmo::Resource
5
5
 
6
- # @macro [attach] virtus_attribute
7
- # @return [$2]
8
6
  attribute :id, Integer
9
7
  attribute :survey_id, Integer
10
8
  attribute :campaign_id, Integer
@@ -41,9 +39,8 @@ module SurveyGizmo; module API
41
39
  route '/survey/:survey_id/surveycampaign/:campaign_id/contact/:id', via: [:get, :update, :delete]
42
40
  route '/survey/:survey_id/surveycampaign/:campaign_id/contact', via: :create
43
41
 
44
- # @see SurveyGizmo::Resource#to_param_options
45
42
  def to_param_options
46
- {id: self.id, survey_id: self.survey_id, campaign_id: self.campaign_id}
43
+ { id: self.id, survey_id: self.survey_id, campaign_id: self.campaign_id }
47
44
  end
48
45
  end
49
46
  end; end
@@ -3,8 +3,6 @@ module SurveyGizmo; module API
3
3
  class EmailMessage
4
4
  include SurveyGizmo::Resource
5
5
 
6
- # @macro [attach] virtus_attribute
7
- # @return [$2]
8
6
  attribute :id, Integer
9
7
  attribute :survey_id, Integer
10
8
  attribute :campaign_id, Integer
@@ -25,9 +23,8 @@ module SurveyGizmo; module API
25
23
  route '/survey/:survey_id/surveycampaign/:campaign_id/emailmessage/:id', :via => [:get, :update, :delete]
26
24
  route '/survey/:survey_id/surveycampaign/:campaign_id/emailmessage', :via => :create
27
25
 
28
- # @see SurveyGizmo::Resource#to_param_options
29
26
  def to_param_options
30
- {:id => self.id, :survey_id => self.survey_id, :campaign_id => self.campaign_id}
27
+ { id: self.id, survey_id: self.survey_id, campaign_id: self.campaign_id }
31
28
  end
32
29
  end
33
30
  end; end
@@ -3,8 +3,6 @@ module SurveyGizmo; module API
3
3
  class Option
4
4
  include SurveyGizmo::Resource
5
5
 
6
- # @macro [attach] virtus_attribute
7
- # @return [$2]
8
6
  attribute :id, Integer
9
7
  attribute :survey_id, Integer
10
8
  attribute :page_id, Integer
@@ -25,9 +23,8 @@ module SurveyGizmo; module API
25
23
 
26
24
  alias_method_chain :title=, :multilingual
27
25
 
28
- # @see SurveyGizmo::Resource#to_param_options
29
26
  def to_param_options
30
- {id: self.id, survey_id: self.survey_id, page_id: self.page_id, question_id: self.question_id}
27
+ { id: self.id, survey_id: self.survey_id, page_id: self.page_id, question_id: self.question_id }
31
28
  end
32
29
  end
33
30
  end; end
@@ -3,8 +3,6 @@ module SurveyGizmo; module API
3
3
  class Page
4
4
  include SurveyGizmo::Resource
5
5
 
6
- # @macro [attach] virtus_attribute
7
- # @return [$2]
8
6
  attribute :id, Integer
9
7
  attribute :title, Hash
10
8
  attribute :description, String
@@ -25,16 +23,14 @@ module SurveyGizmo; module API
25
23
  end
26
24
 
27
25
  # survey gizmo sends a hash back for :title
28
- # @private
29
26
  def title_with_multilingual=(val)
30
27
  self.title_without_multilingual = val.is_a?(Hash) ? val : { 'English' => val }
31
28
  end
32
29
 
33
30
  alias_method_chain :title=, :multilingual
34
31
 
35
- # @see SurveyGizmo::Resource#to_param_options
36
32
  def to_param_options
37
- {id: self.id, survey_id: self.survey_id}
33
+ { id: self.id, survey_id: self.survey_id }
38
34
  end
39
35
  end
40
36
  end; end
@@ -3,8 +3,6 @@ module SurveyGizmo; module API
3
3
  class Question
4
4
  include SurveyGizmo::Resource
5
5
 
6
- # @macro [attach] virtus_attribute
7
- # @return [$2]
8
6
  attribute :id, Integer
9
7
  attribute :title, String
10
8
  attribute :type, String
@@ -13,7 +11,7 @@ module SurveyGizmo; module API
13
11
  attribute :properties, Hash
14
12
  attribute :after, Integer
15
13
  attribute :survey_id, Integer
16
- attribute :page_id, Integer, :default => 1
14
+ attribute :page_id, Integer, default: 1
17
15
  attribute :sub_question_skus, Array
18
16
  attribute :parent_question_id, Integer
19
17
 
@@ -36,9 +34,10 @@ module SurveyGizmo; module API
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)}
40
- .each {|subquestion| subquestion.parent_question_id = id}
37
+ @sub_questions ||= sub_question_skus.map { |subquestion_id| SurveyGizmo::API::Question.first(survey_id: survey_id, id: subquestion_id) }
38
+ .each { |subquestion| subquestion.parent_question_id = id }
41
39
  end
40
+
42
41
  # survey gizmo sends a hash back for :title
43
42
  # @private
44
43
  def title_with_multilingual=(val)
@@ -49,7 +48,7 @@ module SurveyGizmo; module API
49
48
 
50
49
  # @see SurveyGizmo::Resource#to_param_options
51
50
  def to_param_options
52
- {id: self.id, survey_id: self.survey_id, page_id: self.page_id}
51
+ { id: self.id, survey_id: self.survey_id, page_id: self.page_id }
53
52
  end
54
53
  end
55
54
  end; end
@@ -3,8 +3,6 @@ module SurveyGizmo; module API
3
3
  class Response
4
4
  include SurveyGizmo::Resource
5
5
 
6
- # @macro [attach] virtus_attribute
7
- # @return [$2] the attribute +$1+ as a $2
8
6
  attribute :id, Integer
9
7
  attribute :survey_id, Integer
10
8
  attribute :contact_id, Integer
@@ -27,9 +25,8 @@ module SurveyGizmo; module API
27
25
  @survey ||= SurveyGizmo::API::Survey.first(id: survey_id)
28
26
  end
29
27
 
30
- # @see SurveyGizmo::Resource#to_param_options
31
28
  def to_param_options
32
- {id: self.id, survey_id: self.survey_id}
29
+ { id: self.id, survey_id: self.survey_id }
33
30
  end
34
31
  end
35
32
  end; end
@@ -3,10 +3,8 @@ module SurveyGizmo; module API
3
3
  class Survey
4
4
  include SurveyGizmo::Resource
5
5
 
6
- # @macro [attach] virtus_attribute
7
- # @return [$2]
8
6
  attribute :id, Integer
9
- attribute :team, Integer
7
+ attribute :team, Array
10
8
  attribute :type, String
11
9
  attribute :_subtype, String
12
10
  attribute :status, String
@@ -26,27 +24,54 @@ module SurveyGizmo; module API
26
24
  route '/survey/:id', via: [:get, :update, :delete]
27
25
  route '/survey', via: :create
28
26
 
27
+ def to_param_options
28
+ { id: self.id }
29
+ end
30
+
29
31
  def pages
30
32
  @pages ||= SurveyGizmo::API::Page.all(survey_id: id)
31
33
  end
32
34
 
35
+ # Sub question handling is in resource.rb. It should probably be here instead but if it gets moved here
36
+ # and people try to request all the questions for a specific page directly from a ::API::Question request,
37
+ # sub questions will not be included! So I left it there for least astonishment.
33
38
  def questions
34
- @questions ||= pages.map {|p| SurveyGizmo::API::Question.all(survey_id: id, page_id: p.id)}.flatten
39
+ @questions ||= pages.map { |p| SurveyGizmo::API::Question.all(survey_id: id, page_id: p.id) }.flatten
35
40
  end
36
41
 
37
42
  # Statistics array of arrays looks like:
38
43
  # [["Partial", 2], ["Disqualified", 28], ["Complete", 15]]
39
44
  def number_of_completed_responses
40
- if statistics && !statistics.empty? && (completed_data = statistics.find {|a| a[0] == 'Complete'})
45
+ if statistics && !statistics.empty? && (completed_data = statistics.find { |a| a[0] == 'Complete' })
41
46
  completed_data[1]
42
47
  else
43
48
  0
44
49
  end
45
50
  end
46
51
 
47
- # @see SurveyGizmo::Resource#to_param_options
48
- def to_param_options
49
- {id: self.id}
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
60
+ end
61
+
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.
64
+ def teams
65
+ @individual_survey ||= SurveyGizmo::API::Survey.first(id: self.id)
66
+ @individual_survey.team
67
+ end
68
+
69
+ def team_names
70
+ teams.map { |t| t['name'] }
71
+ end
72
+
73
+ def belongs_to?(team)
74
+ team_names.any? { |t| t == team }
50
75
  end
51
76
  end
52
- end; end
77
+ end; end
@@ -3,8 +3,6 @@ module SurveyGizmo; module API
3
3
  class SurveyCampaign
4
4
  include SurveyGizmo::Resource
5
5
 
6
- # @macro [attach] virtus_attribute
7
- # @return [$2]
8
6
  attribute :id, Integer
9
7
  attribute :name, String
10
8
  attribute :type, String
@@ -28,9 +26,8 @@ module SurveyGizmo; module API
28
26
  route '/survey/:survey_id/surveycampaign/:id', :via => [:get, :update, :delete]
29
27
  route '/survey/:survey_id/surveycampaign', :via => :create
30
28
 
31
- # @see SurveyGizmo::Resource#to_param_options
32
29
  def to_param_options
33
- {:id => self.id, :survey_id => self.survey_id}
30
+ { id: self.id, survey_id: self.survey_id }
34
31
  end
35
32
  end
36
- end; end
33
+ end; end
@@ -0,0 +1,27 @@
1
+ module SurveyGizmo
2
+ class << self
3
+ attr_accessor :configuration
4
+ end
5
+
6
+ def self.configure
7
+ self.configuration ||= Configuration.new
8
+ yield(configuration) if block_given?
9
+ SurveyGizmo.setup
10
+ end
11
+
12
+ def self.reset!
13
+ self.configuration = Configuration.new
14
+ end
15
+
16
+ class Configuration
17
+ DEFAULT_API_VERSION = 'v4'
18
+
19
+ attr_accessor :api_version
20
+ attr_accessor :user
21
+ attr_accessor :password
22
+
23
+ def initialize
24
+ @api_version = DEFAULT_API_VERSION
25
+ end
26
+ end
27
+ end
@@ -24,31 +24,31 @@ module SurveyGizmo
24
24
  # @param [Hash] filters - simple pagination or other options at the top level, and surveygizmo "filters" at the :filters key
25
25
  # @return [String]
26
26
  #
27
- # example input: {page: 2, filters: [{:field=>"istestdata", :operator=>"<>", :value=>1}]}
28
- # The top level keys (e.g. page, resultsperpage) get simply encoded in the url, while the contents of the array of hashes
29
- # passed at filters[:filters] gets turned into the format surveygizmo expects for its internal filtering, for example:
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:
30
32
  #
31
33
  # filter[field][0]=istestdata&filter[operator][0]=<>&filter[value][0]=1
32
34
  def convert_filters_into_query_string(filters = nil)
33
- if filters && filters.size > 0
34
- output_filters = filters[:filters] || []
35
- filter_hash = {}
36
- output_filters.each_with_index do |filter,i|
37
- filter_hash.merge!({
38
- "filter[field][#{i}]".to_sym => "#{filter[:field]}",
39
- "filter[operator][#{i}]".to_sym => "#{filter[:operator]}",
40
- "filter[value][#{i}]".to_sym => "#{filter[:value]}",
41
- })
42
- end
43
- simple_filters = filters.reject {|k,v| k == :filters}
44
- filter_hash.merge!(simple_filters)
45
-
46
- uri = Addressable::URI.new
47
- uri.query_values = filter_hash
48
- "?#{uri.query}"
49
- else
50
- ''
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
+ )
51
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
52
  end
53
53
 
54
54
  # Get a list of resources
@@ -58,7 +58,7 @@ module SurveyGizmo
58
58
  def all(conditions = {}, filters = nil)
59
59
  response = RestResponse.new(SurveyGizmo.get(handle_route(:create, conditions) + convert_filters_into_query_string(filters)))
60
60
  if response.ok?
61
- _collection = response.data.map {|datum| datum.is_a?(Hash) ? self.new(datum) : datum}
61
+ _collection = response.data.map { |datum| datum.is_a?(Hash) ? self.new(datum) : datum }
62
62
 
63
63
  # Add in the properties from the conditions hash because many of the important ones (like survey_id) are
64
64
  # not often part of the SurveyGizmo returned data
@@ -70,7 +70,7 @@ module SurveyGizmo
70
70
 
71
71
  # Sub questions are not pulled by default so we have to retrieve them
72
72
  if self == SurveyGizmo::API::Question
73
- _collection += _collection.map {|question| question.sub_questions}.flatten
73
+ _collection += _collection.map { |question| question.sub_questions }.flatten
74
74
  end
75
75
 
76
76
  _collection
@@ -130,7 +130,6 @@ module SurveyGizmo
130
130
  methods = options[:via]
131
131
  methods = [:get, :create, :update, :delete] if methods == :any
132
132
  methods.is_a?(Array) ? methods.each { |m| @paths[m] = path } : (@paths[methods] = path)
133
- nil
134
133
  end
135
134
 
136
135
  # This method replaces the :page_id, :survey_id, etc strings defined in each model's URI routes with the
@@ -150,7 +149,8 @@ module SurveyGizmo
150
149
 
151
150
  # Save the instance to Survey Gizmo
152
151
  def save
153
- if id #Then it's an update
152
+ if id
153
+ # Then it's an update, because we already know the surveygizmo assigned id
154
154
  handle_response(SurveyGizmo.post(handle_route(:update), query: self.attributes_without_blanks))
155
155
  @latest_response.ok?
156
156
  else
@@ -232,7 +232,6 @@ module SurveyGizmo
232
232
  end
233
233
  end
234
234
 
235
-
236
235
  protected
237
236
 
238
237
  def attributes_without_blanks
@@ -240,6 +239,7 @@ module SurveyGizmo
240
239
  end
241
240
 
242
241
  private
242
+
243
243
  def handle_route(key)
244
244
  self.class.handle_route(key, to_param_options)
245
245
  end
@@ -15,8 +15,8 @@ class RestResponse
15
15
  # provide time zone information in their API responses.
16
16
  #
17
17
  # See https://surveygizmov4.helpgizmo.com/help/article/link/date-and-time-submitted
18
- unless datum["datesubmitted"].blank?
19
- datum["datesubmitted"] = datum["datesubmitted"] + " EST"
18
+ unless datum['datesubmitted'].blank?
19
+ datum['datesubmitted'] = datum['datesubmitted'] + ' EST'
20
20
  end
21
21
 
22
22
  datum.keys.grep(/^\[/).each do |key|
@@ -55,7 +55,7 @@ class RestResponse
55
55
 
56
56
  # The parsed JSON data of the response
57
57
  def data
58
- @_data ||= @parsed_response['data'] #|| {'id' => @parsed_response['id']}
58
+ @_data ||= @parsed_response['data']
59
59
  end
60
60
 
61
61
  # The error message if there is one
@@ -63,7 +63,6 @@ class RestResponse
63
63
  @_message ||= @parsed_response['message']
64
64
  end
65
65
 
66
-
67
66
  private
68
67
 
69
68
  def cleanup_attribute_name(attr)
@@ -1,53 +1,15 @@
1
- require 'active_support/core_ext/string'
2
- require 'active_support/core_ext/module'
3
- require 'active_support/core_ext/hash'
4
- require 'active_support/core_ext/object/blank'
5
- require 'active_support/concern'
6
- require 'awesome_print'
7
- require 'virtus'
8
- require 'httparty'
9
- require 'digest/md5'
10
-
11
- require 'survey_gizmo/resource'
12
- require 'survey_gizmo/rest_response'
13
-
14
- require 'survey_gizmo/api/survey'
15
- require 'survey_gizmo/api/survey_campaign'
16
- require 'survey_gizmo/api/question'
17
- require 'survey_gizmo/api/option'
18
- require 'survey_gizmo/api/page'
19
- require 'survey_gizmo/api/contact'
20
- require 'survey_gizmo/api/response'
21
- require 'survey_gizmo/api/email_message'
22
-
23
1
  module SurveyGizmo
24
2
  include HTTParty
25
- debug_output $stderr if ENV['GIZMO_DEBUG']
3
+
4
+ debug_output $stderr if ENV['GIZMO_DEBUG'] =~ /^(true|t|yes|y|1)$/i
26
5
  default_timeout 600 # 10 minutes, SurveyGizmo has serious problems.
27
6
 
28
7
  format :json
29
8
 
30
9
  URLError = Class.new(RuntimeError)
31
10
 
32
- # The base uri for this version of the API is $1
33
- base_uri 'https://restapi.surveygizmo.com/v3'
34
-
35
- @@options = {}
36
- mattr_accessor :options
37
-
38
- # Setup the account credentials to access the API
39
- # @param [Hash] opts
40
- # @option opts [#to_s] :user
41
- # The username for your account. Usually your email address
42
- # @option opts [#to_s] :password
43
- # The account password
44
- def self.setup(opts = {})
45
- self.options = opts
46
- default_params({ 'user:md5' => "#{opts[:user]}:#{Digest::MD5.hexdigest(opts[:password])}" })
47
- end
48
-
49
- def self.reset
50
- @@options = {}
51
- default_params({})
11
+ def self.setup
12
+ base_uri "https://restapi.surveygizmo.com/#{SurveyGizmo.configuration.api_version}"
13
+ default_params({ 'user:md5' => "#{SurveyGizmo.configuration.user}:#{Digest::MD5.hexdigest(SurveyGizmo.configuration.password)}" })
52
14
  end
53
15
  end
@@ -0,0 +1,3 @@
1
+ module SurveyGizmo
2
+ VERSION = '3.0.3'
3
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+ require 'survey_gizmo/configuration'
3
+
4
+ describe SurveyGizmo::Configuration do
5
+
6
+ before(:each) do
7
+ SurveyGizmo.configure do |config|
8
+ config.user = 'test@test.com'
9
+ config.password = 'password'
10
+ end
11
+ end
12
+
13
+ after(:each) do
14
+ SurveyGizmo.reset!
15
+ end
16
+
17
+ it 'should allow basic authentication configuration' do
18
+ expect(SurveyGizmo.default_params).to eq({ 'user:md5' => 'test@test.com:5f4dcc3b5aa765d61d8327deb882cf99' })
19
+ end
20
+
21
+ it 'should allow changing user and pass' do
22
+ SurveyGizmo.configure do |config|
23
+ config.user = 'slimthug'
24
+ config.password = 'fourfourz'
25
+ config.api_version = 'v3'
26
+ end
27
+
28
+ expect(SurveyGizmo.default_params).to eq({ 'user:md5'=>'slimthug:836fd7e2961a094c01cb7ba78bac6a06' })
29
+ expect(SurveyGizmo.base_uri).to eq('https://restapi.surveygizmo.com/v3')
30
+ end
31
+ end
@@ -1,18 +1,14 @@
1
1
  require 'spec_helper'
2
- describe 'Survey Gizmo Resource' do
3
2
 
3
+ describe 'Survey Gizmo Resource' do
4
4
  let(:create_attributes_to_compare) { }
5
5
  let(:get_attributes_to_compare) { }
6
6
 
7
7
  describe SurveyGizmo::Resource do
8
- before(:each) do
9
- SurveyGizmo.setup(user: 'test@test.com', password: 'password')
10
- end
11
-
12
8
  let(:described_class) { SurveyGizmoSpec::ResourceTest }
13
- let(:create_attributes) { {title: 'Spec', test_id: 5} }
14
- let(:update_attributes) { {title: 'Updated'} }
15
- let(:first_params) { {id: 1, test_id: 5} }
9
+ let(:create_attributes) { { title: 'Spec', test_id: 5 } }
10
+ let(:update_attributes) { { title: 'Updated' } }
11
+ let(:first_params) { { id: 1, test_id: 5 } }
16
12
  let(:get_attributes) { create_attributes.merge(id: 1) }
17
13
  let(:uri_paths){
18
14
  {
@@ -42,7 +38,7 @@ describe 'Survey Gizmo Resource' do
42
38
 
43
39
  context '#convert_filters_into_query_string' do
44
40
  let(:page) { 2 }
45
- let(:filters) { {page: page, filters: [{field: 'istestdata', operator: '<>', value: 1}] }}
41
+ let(:filters) { { page: page, filters: [{ field: 'istestdata', operator: '<>', value: 1 }] } }
46
42
 
47
43
  it 'should generate the correct page request' do
48
44
  expect(SurveyGizmoSpec::ResourceTest.convert_filters_into_query_string(page: page)).to eq("?page=#{page}")
@@ -170,4 +166,19 @@ describe 'Survey Gizmo Resource' do
170
166
  it_should_behave_like 'an object with errors'
171
167
  end
172
168
 
169
+ describe SurveyGizmo::API::AccountTeams do
170
+ pending('Need an account with admin privileges to test this')
171
+ let(:create_attributes) { { teamid: 1234, teamname: 'team' } }
172
+ let(:get_attributes) { create_attributes.merge(id: 1234) }
173
+ let(:update_attributes) { create_attributes }
174
+ let(:first_params) { { teamname: 'team' } }
175
+ let(:uri_paths) do
176
+ h = { :create => '/account_teams/1234' }
177
+ h.default = '/account_teams/1234'
178
+ h
179
+ end
180
+
181
+ #it_should_behave_like 'an API object'
182
+ #it_should_behave_like 'an object with errors'
183
+ end
173
184
  end
data/spec/spec_helper.rb CHANGED
@@ -5,12 +5,16 @@ require 'webmock/rspec'
5
5
 
6
6
  # Requires supporting files with custom matchers and macros, etc,
7
7
  # in ./support/ and its subdirectories.
8
- Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f }
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
9
9
 
10
10
  RSpec.configure do |config|
11
11
  config.include SurveyGizmoSpec::Methods
12
12
 
13
13
  config.before(:each) do
14
- @base = 'https://restapi.surveygizmo.com/v3'
14
+ @base = 'https://restapi.surveygizmo.com/v4'
15
+ SurveyGizmo.configure do |config|
16
+ config.user = 'test@test.com'
17
+ config.password = 'password'
18
+ end
15
19
  end
16
20
  end
@@ -4,17 +4,13 @@ module SurveyGizmoSpec
4
4
  stub_request(method, /#{@base}/).to_return(json_response(result, {}))
5
5
  end
6
6
 
7
- def request_params(opts = {})
8
- {'user:pass' => 'test@test.com:password'}.merge(opts)
9
- end
10
-
11
7
  def json_response(result, data)
12
- body = {result_ok: result}
8
+ body = { result_ok: result }
13
9
  result ? body.merge!(data: data) : body.merge!(message: data)
14
10
  {
15
- headers: {'Content-Type' => 'application/json'},
11
+ headers: { 'Content-Type' => 'application/json' },
16
12
  body: body.to_json
17
13
  }
18
14
  end
19
15
  end
20
- end
16
+ end
@@ -1,8 +1,4 @@
1
1
  shared_examples_for 'an API object' do
2
- before(:all) do
3
- SurveyGizmo.setup(user: 'test@test.com', password: 'password')
4
- end
5
-
6
2
  it "should be descendant of SurveyGizmo::Resource" do
7
3
  SurveyGizmo::Resource.descendants.should include(described_class)
8
4
  end
@@ -24,7 +20,7 @@ shared_examples_for 'an API object' do
24
20
  stub_request(:put, /#{@base}/).to_return(json_response(true, create_attributes))
25
21
  obj = described_class.create(create_attributes)
26
22
 
27
- obj.attributes.reject{|k,v| v.blank? }.should == (create_attributes_to_compare || create_attributes)
23
+ obj.attributes.reject { |k,v| v.blank? }.should == (create_attributes_to_compare || create_attributes)
28
24
  end
29
25
  end
30
26
 
@@ -1,6 +1,5 @@
1
1
  shared_examples_for 'an object with errors' do
2
2
  before(:each) do
3
- SurveyGizmo.setup(user: 'test@test.com', password: 'password')
4
3
  stub_request(:any, /#{@base}/).to_return(json_response(false, 'There was an error!'))
5
4
  end
6
5
 
@@ -31,4 +30,4 @@ shared_examples_for 'an object with errors' do
31
30
  @obj.errors.should be_empty
32
31
  end
33
32
  end
34
- end
33
+ end
@@ -22,4 +22,4 @@ module SurveyGizmoSpec
22
22
  attribute :id, Integer
23
23
  attribute :title, String
24
24
  end
25
- end
25
+ end
@@ -2,16 +2,6 @@ require 'spec_helper'
2
2
 
3
3
  describe SurveyGizmo do
4
4
  it 'should have a base uri' do
5
- SurveyGizmo.base_uri.should == 'https://restapi.surveygizmo.com/v3'
6
- end
7
-
8
- it 'should raise an error if auth isn\'t configured' do
9
- #SurveyGizmo.reset
10
- #expect(SurveyGizmo::API::Survey.all).to raise_exception
11
- end
12
-
13
- it 'should allow basic authentication configuration' do
14
- SurveyGizmo.setup(user: 'test@test.com', password: 'password')
15
- SurveyGizmo.default_options[:default_params].should == {'user:md5' => 'test@test.com:5f4dcc3b5aa765d61d8327deb882cf99'}
5
+ SurveyGizmo.base_uri.should == 'https://restapi.surveygizmo.com/v4'
16
6
  end
17
7
  end
@@ -2,9 +2,11 @@
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
+ require 'survey_gizmo/version'
6
+
5
7
  Gem::Specification.new do |gem|
6
8
  gem.name = 'survey-gizmo-ruby'
7
- gem.version = '2.0.1'
9
+ gem.version = SurveyGizmo::VERSION
8
10
  gem.authors = ['Kabari Hendrick', 'Chris Horn', 'Adrien Jarthon', 'Lumos Labs, Inc.']
9
11
  gem.email = ['adrien.jarthon@dimelo.com']
10
12
  gem.description = 'Gem to use the SurveyGizmo.com REST API, v3+'
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: 2.0.1
4
+ version: 3.0.3
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-04-21 00:00:00.000000000 Z
14
+ date: 2015-09-23 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activesupport
@@ -196,6 +196,7 @@ files:
196
196
  - README.md
197
197
  - Rakefile
198
198
  - lib/survey-gizmo-ruby.rb
199
+ - lib/survey_gizmo/api/account_teams.rb
199
200
  - lib/survey_gizmo/api/contact.rb
200
201
  - lib/survey_gizmo/api/email_message.rb
201
202
  - lib/survey_gizmo/api/option.rb
@@ -204,9 +205,12 @@ files:
204
205
  - lib/survey_gizmo/api/response.rb
205
206
  - lib/survey_gizmo/api/survey.rb
206
207
  - lib/survey_gizmo/api/survey_campaign.rb
208
+ - lib/survey_gizmo/configuration.rb
207
209
  - lib/survey_gizmo/resource.rb
208
210
  - lib/survey_gizmo/rest_response.rb
209
211
  - lib/survey_gizmo/survey_gizmo.rb
212
+ - lib/survey_gizmo/version.rb
213
+ - spec/configuration_spec.rb
210
214
  - spec/resource_spec.rb
211
215
  - spec/spec_helper.rb
212
216
  - spec/support/methods.rb
@@ -243,6 +247,7 @@ signing_key:
243
247
  specification_version: 4
244
248
  summary: Gem to use the SurveyGizmo.com REST API, v3+
245
249
  test_files:
250
+ - spec/configuration_spec.rb
246
251
  - spec/resource_spec.rb
247
252
  - spec/spec_helper.rb
248
253
  - spec/support/methods.rb