Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for API version 5 #114

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/survey-gizmo-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@

path = File.join(File.expand_path(File.dirname(__FILE__)), 'survey_gizmo')
Dir["#{path}/*.rb"].each { |f| require f }
Dir["#{path}/**/*.rb"].each { |f| require f }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we need to keep the **/*.rb otherwise the SurveyGizmo::API won't be required and you wont be able to use thing like: SurveyGizmo::API::Survey.all(all_pages: true)

You can give it a try with an new rails project:

rails new -d sqlite3 test-survey-gizmo-rails

and in the Gemfile point at your local version of the gem

gem 'survey-gizmo-ruby' #, path: '../survey-gizmo-ruby'

Then if you do bundle exec rails c you'll have the following error:

ylecuyer@inwin:~/Projects/test-survey-gizmo-rails$ bundle exec rails c
Running via Spring preloader in process 7319
Loading development environment (Rails 6.1.5)
irb(main):001:0> SurveyGizmo::API::Survey.all(all_pages: true)
Traceback (most recent call last):
        1: from (irb):1
NameError (uninitialized constant SurveyGizmo::API)
irb(main):002:0> 

Instead of:

ylecuyer@inwin:~/Projects/test-survey-gizmo-rails$ bundle exec rails c
Running via Spring preloader in process 7463
Loading development environment (Rails 6.1.5)
irb(main):001:0> SurveyGizmo::API::Survey.all(all_pages: true)
Traceback (most recent call last):
        1: from (irb):1
RuntimeError (Not configured!)

which is expected

Dir["#{path}/v4/*.rb"].each { |f| require f }
Dir["#{path}/v5/*.rb"].each { |f| require f }
7 changes: 7 additions & 0 deletions lib/survey_gizmo/api/api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module SurveyGizmo::API
if SurveyGizmo.configuration.v5?
include SurveyGizmo::V5
else
include SurveyGizmo::V4
end
end
10 changes: 9 additions & 1 deletion lib/survey_gizmo/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ def configure
configuration.retriable_params = Configuration::DEFAULT_RETRIABLE_PARAMS.merge(configuration.retriable_params)

@global_config = configuration

path = File.expand_path(File.dirname(__FILE__))
require File.join(path, 'api/api')
configuration
end

def reset!
Expand All @@ -41,7 +45,7 @@ def reset!
end

class Configuration
DEFAULT_API_VERSION = 'v4'
DEFAULT_API_VERSION = 'v5'
DEFAULT_RESULTS_PER_PAGE = 50
DEFAULT_TIMEOUT_SECONDS = 300
DEFAULT_REGION = :us
Expand Down Expand Up @@ -113,5 +117,9 @@ def region=(region)
@api_url = region_infos[:url]
@api_time_zone = region_infos[:locale]
end

def v5?
api_version == 'v5'
end
end
end
3 changes: 3 additions & 0 deletions lib/survey_gizmo/faraday_middleware/parse_survey_gizmo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class ParseSurveyGizmo < FaradayMiddleware::ResponseMiddleware
'datecreated',
'datemodified',
'datesubmitted',
'date_created',
'date_modified',
'date_submitted',
'modified_on'
]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This REST endpoint is only available to accounts with admin privileges
# This code is untested.

module SurveyGizmo::API
module SurveyGizmo::V4
class AccountTeams
include SurveyGizmo::Resource

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module SurveyGizmo::API
module SurveyGizmo::V4
class Answer
include Virtus.model

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module SurveyGizmo::API
module SurveyGizmo::V4
class Campaign
include SurveyGizmo::Resource

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module SurveyGizmo::API
module SurveyGizmo::V4
class Contact
include SurveyGizmo::Resource

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module SurveyGizmo::API
module SurveyGizmo::V4
class EmailMessage
include SurveyGizmo::Resource

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module SurveyGizmo::API
module SurveyGizmo::V4
class Option
include SurveyGizmo::Resource
include SurveyGizmo::MultilingualTitle
Expand Down
4 changes: 2 additions & 2 deletions lib/survey_gizmo/api/page.rb → lib/survey_gizmo/v4/page.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'survey_gizmo/api/question'
require 'survey_gizmo/v4/question'

module SurveyGizmo::API
module SurveyGizmo::V4
class Page
include SurveyGizmo::Resource
include SurveyGizmo::MultilingualTitle
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'survey_gizmo/api/option'
require 'survey_gizmo/v4/option'

module SurveyGizmo::API
module SurveyGizmo::V4
class Question
include SurveyGizmo::Resource
include SurveyGizmo::MultilingualTitle
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module SurveyGizmo::API
module SurveyGizmo::V4
class Response
include SurveyGizmo::Resource

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'survey_gizmo/api/page'
require 'survey_gizmo/v4/page'

module SurveyGizmo::API
module SurveyGizmo::V4
class Survey
include SurveyGizmo::Resource

Expand Down
18 changes: 18 additions & 0 deletions lib/survey_gizmo/v5/account_teams.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# This REST endpoint is only available to accounts with admin privileges
# This code is untested.

module SurveyGizmo::V5
class AccountTeams
include SurveyGizmo::Resource

attribute :id, Integer
attribute :default_role, String
attribute :status, String

# v5 fields
attribute :team_name, String
attribute :description, String

@route = '/accountteams'
end
end
72 changes: 72 additions & 0 deletions lib/survey_gizmo/v5/answer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
require 'survey_gizmo/v5/option'

module SurveyGizmo::V5
class Answer
include Virtus.model

attribute :key, String
attribute :value, String
attribute :survey_id, Integer
attribute :response_id, Integer
attribute :question_id, Integer
attribute :question_text, String
attribute :question_type, String
attribute :options, Array[Option]
attribute :submitted_at, DateTime
attribute :answer_text, String
attribute :other_text, String
attribute :question_pipe, String

def initialize(attrs = {})
self.attributes = attrs
self.question_id = value['id']
self.question_text = value['question']
self.question_type = value['type']

if value['options']
self.answer_text = selected_options_texts.join(', ')
else
self.answer_text = value['answer']
end
end

def single_option
[
Option.new(attributes.merge(
id: value['answer_id'],
value: value['answer'],
title: value['original_answer'] || value['answer']
))
]
end

def selected_options_texts
selected_options.map do |opt|
opt['answer']
end
end

def selected_options
value['options'].values.reject { |opt| opt['answer'].nil? }.map do |opt|
Option.new(attributes.merge(
id: opt['id'],
value: opt['answer'],
title: opt['option']
))
end
end

# Strips out the answer_text when there is a valid option_id
def to_hash
{
response_id: response_id,
question_id: question_id,
question_pipe: question_pipe,
submitted_at: submitted_at,
survey_id: survey_id,
other_text: other_text,
answer_text: other_text ? nil : answer_text
}.reject { |k, v| v.nil? }
end
end
end
32 changes: 32 additions & 0 deletions lib/survey_gizmo/v5/campaign.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module SurveyGizmo::V5
class Campaign
include SurveyGizmo::Resource

attribute :id, Integer
attribute :name, String
attribute :type, String
attribute :status, String
attribute :uri, String
attribute :SSL, Boolean
attribute :language, String
attribute :close_message, String
attribute :limit_responses, String
attribute :survey_id, Integer

# v5 fields
attribute :token_variables, Array
attribute :invite_id, Integer
attribute :subtype, String
attribute :link_type, String
attribute :date_created, DateTime
attribute :date_modified, DateTime
attribute :link_open_date, DateTime
attribute :link_close_date, DateTime

@route = '/survey/:survey_id/surveycampaign'

def contacts(conditions = {})
Contact.all(conditions.merge(children_params).merge(all_pages: !conditions[:page]))
end
end
end
46 changes: 46 additions & 0 deletions lib/survey_gizmo/v5/contact.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module SurveyGizmo::V5
class Contact
include SurveyGizmo::Resource

attribute :id, Integer
attribute :survey_id, Integer
attribute :campaign_id, Integer

# v5 fields
attribute :date_last_sent, DateTime
attribute :division, String
attribute :team, String
attribute :group, String
attribute :role, String
attribute :status, String
attribute :subscriber_status, String
attribute :email_address, String
attribute :first_name, String
attribute :last_name, String
attribute :organization, String
attribute :department, String
attribute :business_phone, String
attribute :home_phone, String
attribute :fax_phone, String
attribute :mailing_address, String
attribute :mailing_address2, String
attribute :mailing_address_city, String
attribute :mailing_address_state, String
attribute :mailing_address_country, String
attribute :mailing_address_postal, String
attribute :title, String
attribute :url, String
attribute :customfield1, String
attribute :customfield2, String
attribute :customfield3, String
attribute :customfield4, String
attribute :customfield5, String
attribute :customfield6, String
attribute :customfield7, String
attribute :customfield8, String
attribute :customfield9, String
attribute :customfield10, String

@route = '/survey/:survey_id/surveycampaign/:campaign_id/contact'
end
end
29 changes: 29 additions & 0 deletions lib/survey_gizmo/v5/email_message.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module SurveyGizmo::V5
class EmailMessage
include SurveyGizmo::Resource

attribute :id, Integer
attribute :survey_id, Integer
attribute :campaign_id, Integer
attribute :invite_identity, Integer
attribute :subject, String
attribute :replies, String
attribute :medium, String
attribute :status, String
attribute :from, Hash
attribute :body, Hash
attribute :send, Boolean

# v5 fields
attribute :type, String
attribute :subtype, String
attribute :message_type, String
attribute :footer, String
attribute :embed_question, Boolean
attribute :disable_styles, Boolean
attribute :date_created, DateTime
attribute :date_modified, DateTime

@route = '/survey/:survey_id/surveycampaign/:campaign_id/emailmessage'
end
end
18 changes: 18 additions & 0 deletions lib/survey_gizmo/v5/option.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module SurveyGizmo::V5
class Option
include SurveyGizmo::Resource
include SurveyGizmo::MultilingualTitle

attribute :id, Integer
attribute :survey_id, Integer
attribute :page_id, Integer
attribute :question_id, Integer
attribute :value, String
attribute :properties, Hash

# v5 fields
attribute :title, String

@route = '/survey/:survey_id/surveypage/:page_id/surveyquestion/:question_id/surveyoption'
end
end
27 changes: 27 additions & 0 deletions lib/survey_gizmo/v5/page.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'survey_gizmo/v5/question'

module SurveyGizmo::V5
class Page
include SurveyGizmo::Resource
include SurveyGizmo::MultilingualTitle

attribute :id, Integer
attribute :description, String
attribute :properties, Hash
attribute :survey_id, Integer
attribute :questions, Array[Question]

# v5 fields
attribute :title, String

@route = '/survey/:survey_id/surveypage'

def survey
@survey ||= Survey.first(id: survey_id)
end

def questions
@questions.each { |q| q.attributes = children_params }
end
end
end
Loading