Class: WolfCore::ApplicationService

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

Direct Known Subclasses

SalesforceOauthService

Instance Method Summary collapse

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



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

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



48
49
50
51
52
# File 'lib/wolf_core/application/application_service.rb', line 48

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



54
55
56
57
58
59
60
61
62
# File 'lib/wolf_core/application/application_service.rb', line 54

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/wolf_core/application/application_service.rb', line 80

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)


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

def process
  raise NotImplementedError
end

#raise_failed_result(result) ⇒ Object



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

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



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

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

#route_event_request(path:, body:) ⇒ Object



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

def route_event_request(path:, body:)
  domain_url = ENV['CURRENT_SAM_URL']
  async_http_post(url: "#{domain_url}/#{path}", body: body)
end

#salesforce_http_get(salesforce_access_token:, query: nil) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/wolf_core/application/application_service.rb', line 64

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



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

def validate_presence(object, message)
  return if object.present?

  raise_service_error({ message: message })
end

#validate_salesforce_response(response) ⇒ Object



74
75
76
77
78
# File 'lib/wolf_core/application/application_service.rb', line 74

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