Class: WolfCore::ApplicationService
Instance Method Summary
collapse
#log_object
#deep_parse_json, #get_event_params, #invoke_lambda, #result_to_response
#async_http_get, #async_http_post, #async_http_put, #http_get, #http_post, #http_put, #parse_http_response, #validate_http_response
Methods included from AsyncUtils
#run_async
#raise_service_error
Instance Method Details
#call ⇒ Object
8
9
10
11
12
13
14
15
16
17
18
19
|
# File 'lib/wolf_core/application/application_service.rb', line 8
def call
process
rescue => e
if e.instance_of?(WolfCore::ServiceException)
return Result.failure(
error: e.error.to_h.merge({ backtrace: e.backtrace })
)
end
return Result.failure(error: { message: e.message, backtrace: e.backtrace })
end
|
#get_salesforce_access_token ⇒ Object
45
46
47
48
49
|
# File 'lib/wolf_core/application/application_service.rb', line 45
def get_salesforce_access_token
result = WolfCore::SalesforceOauthService.new.call
raise_failed_result(result)
result.data.access_token
end
|
#get_salesforce_foreign_object(salesforce_access_token:, record_id:) ⇒ Object
51
52
53
54
55
56
57
58
59
|
# File 'lib/wolf_core/application/application_service.rb', line 51
def get_salesforce_foreign_object(salesforce_access_token:, record_id:)
foreign_object = salesforce_http_get(
salesforce_access_token: salesforce_access_token,
query: { calltype: 'getRecord', RecordId: record_id }
)
log_object "foreign object is"
log_object foreign_object
foreign_object
end
|
#get_wolf_token ⇒ Object
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/wolf_core/application/application_service.rb', line 77
def get_wolf_token
response = http_post(
query: { tenant: ENV['TENANT'] },
url: "#{ENV['WOLF_PLATFORM_URL']}/api/v1/sign_in",
body: {
email: ENV['WOLF_ADMIN_EMAIL'],
password: ENV['WOLF_ADMIN_PASSWORD']
}
)
validate_http_response(
response: response, message: 'Failed to get wolf token'
)
response_body = JSON.parse(response.body)
wolf_token = response_body.dig('user', 'authentication_token')
puts "wolf token is #{wolf_token}"
wolf_token
end
|
#process ⇒ Object
21
22
23
|
# File 'lib/wolf_core/application/application_service.rb', line 21
def process
raise NotImplementedError
end
|
#raise_failed_result(result) ⇒ Object
25
26
27
28
29
|
# File 'lib/wolf_core/application/application_service.rb', line 25
def raise_failed_result(result)
return if result.success?
raise_service_error(result.error.to_h)
end
|
#remove_non_permitted_parameters(params, allowed_params) ⇒ Object
37
38
39
40
41
42
43
|
# File 'lib/wolf_core/application/application_service.rb', line 37
def remove_non_permitted_parameters(params, allowed_params)
permitted = params.slice(*allowed_params)
if permitted.blank?
raise_service_error({ message: "There are not permitted parameters"})
end
return permitted
end
|
#salesforce_http_get(salesforce_access_token:, query: nil) ⇒ Object
61
62
63
64
65
66
67
68
69
|
# File 'lib/wolf_core/application/application_service.rb', line 61
def salesforce_http_get(salesforce_access_token:, query: nil)
response = http_get(
url: ENV['SALESFORCE_URL'],
headers: { 'Authorization' => "Bearer #{salesforce_access_token}" },
query: query
)
validate_salesforce_response(response)
JSON.parse(response.parsed_response)
end
|
#validate_presence(object, message) ⇒ Object
31
32
33
34
35
|
# File 'lib/wolf_core/application/application_service.rb', line 31
def validate_presence(object, message)
return if object.present?
raise_service_error({ message: message })
end
|
#validate_salesforce_response(response) ⇒ Object
71
72
73
74
75
|
# File 'lib/wolf_core/application/application_service.rb', line 71
def validate_salesforce_response(response)
return if response.code == 200
message = JSON.parse(response.body).first['message']
raise_service_error({ message: message })
end
|