Class: WolfCore::ApplicationService

Inherits:
Object
  • Object
show all
Includes:
ExceptionOperations, HttpOperations, LambdaFunctionOperations, LoggingUtils
Defined in:
lib/wolf_core/application/application_service.rb

Direct Known Subclasses

SalesforceOauthService

Instance Method Summary collapse

Methods included from LoggingUtils

#log_object

Methods included from LambdaFunctionOperations

#get_event_params, #invoke_lambda, #parse_lambda_response, #result_to_response, #validate_lambda_response

Methods included from StringUtils

#camelcase_to_spaces, #clean_phone_number, #deep_parse_json, #hash_str_to_json, #remove_blank_spaces

Methods included from ExceptionOperations

#raise_service_error

Methods included from HttpOperations

#async_http_get, #async_http_patch, #async_http_post, #async_http_put, #http_get, #http_patch, #http_post, #http_put, #parse_http_response, #parsed_http_get, #parsed_http_patch, #parsed_http_post, #parsed_http_put, #safe_http_get, #safe_http_patch, #safe_http_post, #safe_http_put, #validate_http_response

Methods included from AsyncUtils

#run_async

Instance Method Details

#callObject



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

  # Honeybadger.notify(e)
  return Result.failure(error: { message: e.message, backtrace: e.backtrace })
end

#get_salesforce_access_tokenObject



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
# 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, title: 'foreign object is'
  foreign_object
end

#get_wolf_tokenObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/wolf_core/application/application_service.rb', line 76

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

#processObject

Raises:

  • (NotImplementedError)


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



60
61
62
63
64
65
66
67
68
# File 'lib/wolf_core/application/application_service.rb', line 60

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



70
71
72
73
74
# File 'lib/wolf_core/application/application_service.rb', line 70

def validate_salesforce_response(response)
  return if response.code == 200
  message = JSON.parse(response.body).first['message']
  raise_service_error({ message: message, response: response })
end