-
Notifications
You must be signed in to change notification settings - Fork 255
/
properties.rb
123 lines (104 loc) · 4.14 KB
/
properties.rb
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
module Hubspot
class Properties
PROPERTY_SPECS = {
group_field_names: %w(name displayName displayOrder properties),
field_names: %w(name groupName description fieldType formField type displayOrder label options showCurrencySymbol),
valid_field_types: %w(textarea select text date file number radio checkbox booleancheckbox),
valid_types: %w(string number bool date datetime enumeration),
options: %w(description value label hidden displayOrder)
}
DEFAULT_PROPERTY = 'email'
class << self
# TODO: properties can be set as configuration
# TODO: find the way how to set a list of Properties + merge same property key if present from opts
def add_default_parameters(opts={})
if opts.keys.map(&:to_s).include? 'property'
opts
else
opts.merge(property: DEFAULT_PROPERTY)
end
end
def all(path, opts={}, filter={})
response = Hubspot::Connection.get_json(path, opts)
filter_results(response, :groupName, filter[:include], filter[:exclude])
end
def groups(path, opts={}, filter={})
response = Hubspot::Connection.get_json(path, opts)
filter_results(response, :name, filter[:include], filter[:exclude])
end
def create!(path, params={})
post_data = valid_property_params(params)
return nil if post_data.blank?
Hubspot::Connection.post_json(path, params: {}, body: post_data)
end
def update!(path, property_name, params={})
post_data = valid_property_params(params)
return nil if post_data.blank?
Hubspot::Connection.put_json(path, params: { property_name: property_name }, body: post_data)
end
def delete!(path, property_name)
response = Hubspot::Connection.delete_json(path, property_name: property_name)
response.parsed_response
end
def create_group!(path, params={})
post_data = valid_group_params(params)
return nil if post_data.blank?
Hubspot::Connection.post_json(path, params: {}, body: post_data)
end
def update_group!(path, group_name, params={})
post_data = valid_group_params(params)
return nil if post_data.blank?
Hubspot::Connection.put_json(path, params: { group_name: group_name }, body: post_data)
end
def delete_group!(path, group_name)
response = Hubspot::Connection.delete_json(path, group_name: group_name)
response.parsed_response
end
def same?(src, dst)
src_params = valid_params(src)
dst_params = valid_params(dst)
src_params.eql?(dst_params)
# hash_same?(src_params, dst_params)
end
def valid_params(params={})
valid_property_params(params)
end
private
def filter_results(results, key, include, exclude)
key = key.to_s
results.select { |result|
(include.blank? || include.include?(result[key])) &&
(exclude.blank? || !exclude.include?(result[key]))
}
end
def valid_property_params(params)
return {} if params.blank?
result = params.slice(*PROPERTY_SPECS[:field_names])
result.delete('fieldType') unless check_field_type(result['fieldType'])
result.delete('type') unless check_type(result['type'])
result['options'] = valid_option_params(result['options'])
result
end
def valid_group_params(params)
return {} if params.blank?
result = params.slice(*PROPERTY_SPECS[:group_field_names])
result['properties'] = valid_property_params(result['properties']) unless result['properties'].blank?
result
end
def check_field_type(val)
return true if PROPERTY_SPECS[:valid_field_types].include?(val)
puts "Invalid field type: #{val}"
false
end
def check_type(val)
return true if PROPERTY_SPECS[:valid_types].include?(val)
puts "Invalid type: #{val}"
false
end
def valid_option_params(options)
return [] if options.blank?
options.map { |o| o.slice(*PROPERTY_SPECS[:options]) }
end
end
end
end