Module: SfdoAPI
- Defined in:
- lib/SFDO/API.rb
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_called, *args, &block) ⇒ Object
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
# File 'lib/SFDO/API.rb', line 146
def method_missing(method_called, *args, &block)
breakdown = method_called.to_s.split('_')
obj_type = breakdown.last.capitalize
case method_called.to_s
when /^delete_all_/
delete_all *args
when /^delete_one/
delete obj_type, *args
when /^create_/
create obj_type, *args
when /^select_api/
select_api *args
else
super.method_missing
end
end
|
Instance Method Details
#api_client ⇒ Object
6
7
8
9
10
11
12
|
# File 'lib/SFDO/API.rb', line 6
def api_client
@api_client ||= Restforce.new api_version: '32.0',
refresh_token: ENV['SF_REFRESH_TOKEN'],
client_id: ENV['SF_CLIENT_KEY'],
client_secret: ENV['SF_CLIENT_SECRET']
yield
end
|
#create(type, obj_hash) ⇒ Object
14
15
16
17
18
19
20
21
22
|
# File 'lib/SFDO/API.rb', line 14
def create(type, obj_hash)
type = true_object_name(type)
if is_valid_obj_hash?(type, obj_hash, @fields_acceptibly_nil)
obj_id = api_client do
@api_client.create! type, obj_hash
end
end
obj_id
end
|
#delete(type, obj_id) ⇒ Object
124
125
126
|
# File 'lib/SFDO/API.rb', line 124
def delete(type, obj_id)
delete_by_id(obj_id)
end
|
#delete_all(id) ⇒ Object
134
135
136
137
138
|
# File 'lib/SFDO/API.rb', line 134
def delete_all(id)
api_client do
id.each(&:destroy)
end
end
|
#delete_by_id(obj_id) ⇒ Object
128
129
130
131
132
|
# File 'lib/SFDO/API.rb', line 128
def delete_by_id(obj_id)
api_client do
@api_client.destroy(true_object_name(obj_id), obj_id)
end
end
|
#get_object_describe(object_name) ⇒ Object
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/SFDO/API.rb', line 108
def get_object_describe(object_name)
api_client do
@description = @api_client.get("/services/data/v35.0/sobjects/#{object_name}/describe")
describeobject = Hashie::Mash.new(@description.body)
required = describeobject.fields.map do |x|
Hashie::Mash.new(
fieldName: x.name,
required: (!x.nillable && !x.defaultedOnCreate),
default: x.defaultValue)
end
required.select(&:required)
end
end
|
#is_valid_obj_hash?(object_name, obj_hash, fields_acceptibly_nil) ⇒ Boolean
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/SFDO/API.rb', line 41
def is_valid_obj_hash?(object_name, obj_hash, fields_acceptibly_nil)
required_fields = get_object_describe(object_name).map(&:fieldName)
valid = true
required_fields.each do |f|
valid = false if (!obj_hash.key? f.to_sym) && (begin
!fields_acceptibly_nil[object_name].contains? f
puts 'This field must be populated in order to create this object in this environment: ' + f.inspect
rescue
false
end)
end
valid
end
|
#obj_names_without_custom_additions ⇒ Object
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/SFDO/API.rb', line 88
def obj_names_without_custom_additions
if @obj_names_without_custom_additions.nil? || !@obj_names_without_custom_additions.respond_to?(:contains)
@obj_names_without_custom_additions = {}
org_describe.each do |z|
tmp_var = z.name.split "__"
save = ""
case tmp_var.size
when 2
save = tmp_var.first
when 3
save = tmp_var[1]
else
save = tmp_var.last
end
@obj_names_without_custom_additions.store(save, z.name)
end
end
@obj_names_without_custom_additions
end
|
#org_describe ⇒ Object
58
59
60
61
62
63
64
65
|
# File 'lib/SFDO/API.rb', line 58
def org_describe()
if @org_description.nil? || !@org_description.respond_to?(:contains)
@org_description = api_client do
@api_client.describe
end
end
return @org_description
end
|
#prefix_to_name ⇒ Object
67
68
69
70
71
72
73
74
75
|
# File 'lib/SFDO/API.rb', line 67
def prefix_to_name
if @prefix_to_name.nil? || !@prefix_to_name.respond_to?(:contains)
@prefix_to_name = {}
org_describe.each do |z|
@prefix_to_name.store(z.keyPrefix, z.name)
end
end
return @prefix_to_name
end
|
#select_api(query) ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/SFDO/API.rb', line 24
def select_api(query)
if query.match /where/i
obj_name = query[/(?<=from )(.*)(?= where)|$/i]
else
obj_name = query[/(?<=from )(.*)$/i]
end
real_obj_name = true_object_name(obj_name)
query = query.gsub(obj_name, real_obj_name)
results = api_client do
@api_client.query query
end
results
end
|
#true_object_name(handle) ⇒ Object
either an ID or a string name
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/SFDO/API.rb', line 77
def true_object_name(handle) handle = (handle.end_with?("__c") || handle.end_with?("__r")) ? handle[0...-3] : handle
from_id = prefix_to_name[handle[0..2]]
from_name = obj_names_without_custom_additions[handle]
if !from_name.nil? || !from_id.nil?
return from_name if from_id.nil?
return from_id if from_name.nil?
end
return 'Unable to find object. Be sure to call SFDO-API without preceding namespace or following __c or __r'
end
|
#update_api(obj) ⇒ Object
140
141
142
143
144
|
# File 'lib/SFDO/API.rb', line 140
def update_api(obj)
api_client do
@api_client.update(obj.attributes.type, obj)
end
end
|