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

#http_get, #http_post, #parse_http_response, #validate_http_response

Methods included from ExceptionOperations

#raise_service_error

Instance Method Details

#barton_integration_http_post(path:, body:, error_message:) ⇒ Object



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

def barton_integration_http_post(path:, body:, error_message:)
  domain_url = ENV['CURRENT_SAM_URL']
  response = http_post(url: "#{domain_url}/#{path}", body: body)
  validate_http_response(
    response: response, message: error_message, error_data: { url: path }
  )
  response
end

#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



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

def get_salesforce_access_token
  result = WolfCore::SalesforceOauthService.new.call
  raise_failed_result(result)
  result.data.access_token
end

#get_salesforce_foreign_object(record_id:) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/wolf_core/application/application_service.rb', line 58

def get_salesforce_foreign_object(record_id:)
  foreign_object = salesforce_http_get(
    query: { calltype: 'getRecord', RecordId: record_id }
  )
  # puts "foreign object is"
  # pp foreign_object
  foreign_object
end

#get_wolf_tokenObject



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

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

#salesforce_http_get(query: nil) ⇒ Object



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

def salesforce_http_get(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



77
78
79
80
81
# File 'lib/wolf_core/application/application_service.rb', line 77

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