forked from Typeform/create-api-gem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.rb
More file actions
106 lines (97 loc) · 5.46 KB
/
settings.rb
File metadata and controls
106 lines (97 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
class Settings
attr_accessor :redirect_after_submit_url, :show_typeform_branding, :progress_bar,
:show_progress_bar, :description, :allow_indexing, :image, :language,
:is_public, :is_trial, :google_analytics, :facebook_pixel, :google_tag_manager,
:notifications
def initialize(redirect_after_submit_url: nil, show_typeform_branding: nil, progress_bar: nil,
show_progress_bar: nil, description: nil, allow_indexing: nil, image: nil, language: nil,
is_public: nil, is_trial: nil, google_analytics: nil, facebook_pixel: nil,
google_tag_manager: nil, notifications: nil, are_uploads_public: nil,
show_time_to_complete: nil)
@redirect_after_submit_url = redirect_after_submit_url
@show_typeform_branding = show_typeform_branding
@progress_bar = progress_bar
@show_progress_bar = show_progress_bar
@description = description
@allow_indexing = allow_indexing
@language = language
@is_public = is_public
@is_trial = is_trial
@image = image
@google_analytics = google_analytics
@facebook_pixel = facebook_pixel
@google_tag_manager = google_tag_manager
@notifications = notifications
end
def self.from_response(response)
meta = response[:meta]
settings_params = response.keep_if { |k, _| k != :meta }
params = meta.merge(settings_params)
params[:notifications] = Notifications.from_response(response[:notifications]) unless response[:notifications].nil?
Settings.new(params)
end
def payload
payload = {}
payload[:redirect_after_submit_url] = redirect_after_submit_url unless redirect_after_submit_url.nil?
payload[:show_typeform_branding] = show_typeform_branding unless show_typeform_branding.nil?
payload[:progress_bar] = progress_bar unless progress_bar.nil?
payload[:show_progress_bar] = show_progress_bar unless show_progress_bar.nil?
payload[:language] = language unless language.nil?
payload[:is_public] = is_public unless is_public.nil?
payload[:google_analytics] = google_analytics unless google_analytics.nil?
payload[:facebook_pixel] = facebook_pixel unless facebook_pixel.nil?
payload[:google_tag_manager] = google_tag_manager unless google_tag_manager.nil?
payload[:notifications] = notifications.payload unless notifications.nil?
unless description.nil? && allow_indexing.nil?
payload[:meta] = {}
payload[:meta][:description] = description unless description.nil?
payload[:meta][:allow_indexing] = allow_indexing unless allow_indexing.nil?
payload[:meta][:image] = image unless image.nil?
end
payload
end
def same?(actual)
(redirect_after_submit_url.nil? || redirect_after_submit_url == actual.redirect_after_submit_url) &&
(google_analytics.nil? || google_analytics == actual.google_analytics) &&
(facebook_pixel.nil? || facebook_pixel == actual.facebook_pixel) &&
(google_tag_manager.nil? || google_tag_manager == actual.google_tag_manager) &&
(notifications.nil? || notifications.same?(actual.notifications)) &&
(description.nil? || description == actual.description) &&
(show_typeform_branding.nil? ? Settings.default.show_typeform_branding : show_typeform_branding) == actual.show_typeform_branding &&
(progress_bar.nil? ? Settings.default.progress_bar : progress_bar) == actual.progress_bar &&
(language.nil? ? Settings.default.language : language) == actual.language &&
(is_public.nil? ? Settings.default.is_public : is_public) == actual.is_public &&
(allow_indexing.nil? ? Settings.default.allow_indexing : allow_indexing) == actual.allow_indexing &&
same_image?(actual)
end
def same_image?(actual)
image.nil? || image[:href].start_with?("#{APIConfig.image_api_request_url}/images/") && actual.image[:href].start_with?("#{APIConfig.image_api_request_url}/images/")
end
def self.default
Settings.new(show_typeform_branding: true, progress_bar: 'proportion', show_progress_bar: true,
allow_indexing: false, language: 'en', is_public: true, is_trial: false)
end
def self.full_example(email_block_for_notifications_ref)
image = { href: APIConfig.image_api_request_url + '/images/default' }
Settings.new(redirect_after_submit_url: 'http://google.com', show_typeform_branding: false, progress_bar: 'percentage',
show_progress_bar: false, description: 'some meta description', allow_indexing: true, image: image,
language: 'fr', is_public: true, google_analytics: 'UA-1234-12', facebook_pixel: '12345678901234567',
google_tag_manager: 'GTM-PLWP6TS', notifications: Notifications.full_example(email_block_for_notifications_ref))
end
end