Class: WolfCore::ApplicationService

Inherits:
Object
  • Object
show all
Defined in:
lib/wolf_core/application/application_service.rb

Direct Known Subclasses

SalesforceOauthService

Instance Method Summary collapse

Instance Method Details

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



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

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



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/wolf_core/application/application_service.rb', line 3

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



80
81
82
83
84
# File 'lib/wolf_core/application/application_service.rb', line 80

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



86
87
88
89
90
91
92
93
94
# File 'lib/wolf_core/application/application_service.rb', line 86

def get_salesforce_foreign_object(record_id:)
  data = saleforce_http_get(
    query: { calltype: 'getRecord', RecordId: record_id }
  )
  foreign_object = data.first
  puts "foreign object is"
  pp foreign_object
  foreign_object
end

#get_wolf_tokenObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/wolf_core/application/application_service.rb', line 112

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

#http_get(url:, headers: {}, query: nil) ⇒ Object



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

def http_get(url:, headers: {}, query: nil)
WolfCore::HttpDataSource.http_get(url: url, headers: headers, query: query)
end

#http_post(url:, headers: {}, body: nil, query: nil) ⇒ Object



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

def http_post(url:, headers: {}, body: nil, query: nil)
WolfCore::HttpDataSource.http_post(url: url, headers: headers, query: query, body: body)
end

#parse_http_response(response) ⇒ Object



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

def parse_http_response(response)
  body = JSON.parse(response.body) rescue response.body
  {
    code: response.code,
    body: body,
    message: response.message,
  }
end

#processObject

Raises:

  • (NotImplementedError)


16
17
18
# File 'lib/wolf_core/application/application_service.rb', line 16

def process
  raise NotImplementedError
end

#raise_failed_result(result) ⇒ Object



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

def raise_failed_result(result)
  return if result.success?

  raise_service_error(result.error.to_h)
end

#raise_service_error(error_data) ⇒ Object



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

def raise_service_error(error_data)
  raise WolfCore::ServiceException, error_data
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

#saleforce_http_get(query: nil) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/wolf_core/application/application_service.rb', line 96

def saleforce_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_http_response(response:, message:, error_data: nil) ⇒ Object



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

def validate_http_response(response:, message:, error_data: nil)
  unless response.code == 200
    error_data = {
      message: message,
      response: parse_http_response(response)
    }.merge(error_data || {})
    raise_service_error(error_data)
  end
end

#validate_presence(object, message) ⇒ Object



26
27
28
29
30
# File 'lib/wolf_core/application/application_service.rb', line 26

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

  raise_service_error({ message: message })
end

#validate_salesforce_response(response) ⇒ Object



106
107
108
109
110
# File 'lib/wolf_core/application/application_service.rb', line 106

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