Class: WolfCore::ApplicationService

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

Direct Known Subclasses

SalesforceOauthService

Instance Method Summary collapse

Methods included from LambdaFunctionOperations

#deep_parse_json, #get_event_params, #invoke_lambda, #result_to_response

Methods included from HttpOperations

#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

Methods included from ExceptionOperations

#raise_service_error

Instance Method Details

#callObject



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/wolf_core/application/application_service.rb', line 7

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



44
45
46
47
48
# File 'lib/wolf_core/application/application_service.rb', line 44

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



50
51
52
53
54
55
56
57
58
# File 'lib/wolf_core/application/application_service.rb', line 50

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 }
  )
  puts "foreign object is"
  pp foreign_object
  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)


20
21
22
# File 'lib/wolf_core/application/application_service.rb', line 20

def process
  raise NotImplementedError
end

#raise_failed_result(result) ⇒ Object



24
25
26
27
28
# File 'lib/wolf_core/application/application_service.rb', line 24

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



36
37
38
39
40
41
42
# File 'lib/wolf_core/application/application_service.rb', line 36

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



30
31
32
33
34
# File 'lib/wolf_core/application/application_service.rb', line 30

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 })
end