wolf_core 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9b284a7f298a9af0d3d5bba012296ae0c3097561fc68152d3c9c172dc4f51d83
4
+ data.tar.gz: 9836ec6f17456d430c81f91fdb753454b6fe4dcd14399c64026295f608fa4a23
5
+ SHA512:
6
+ metadata.gz: b56f79dec0d5ff8b128dd2bee0dada31b54c8df9b660923bb4274fd89d28c44a9099dcdff5776a666b0edf92146d69db63998f15b0527e6f0832b0be332543e7
7
+ data.tar.gz: e531e17cb5befd062079f8dd3a165ec8705a973039a080dfc0c7133766bb1f6e5a32f8f4fa6e714df9ab5a8e163033f2b8488f5962b2722477eca3152eac6914
@@ -0,0 +1,130 @@
1
+ module WolfCore
2
+ class ApplicationService
3
+ def call
4
+ process
5
+ rescue => e
6
+ if e.instance_of?(WolfCore::ServiceException)
7
+ return Result.failure(
8
+ error: e.error.to_h.merge({ backtrace: e.backtrace })
9
+ )
10
+ end
11
+
12
+ # Honeybadger.notify(e)
13
+ return Result.failure(error: { message: e.message, backtrace: e.backtrace })
14
+ end
15
+
16
+ def process
17
+ raise NotImplementedError
18
+ end
19
+
20
+ def raise_failed_result(result)
21
+ return if result.success?
22
+
23
+ raise_service_error(result.error.to_h)
24
+ end
25
+
26
+ def validate_presence(object, message)
27
+ return if object.present?
28
+
29
+ raise_service_error({ message: message })
30
+ end
31
+
32
+ def raise_service_error(error_data)
33
+ raise WolfCore::ServiceException, error_data
34
+ end
35
+
36
+ def remove_non_permitted_parameters(params, allowed_params)
37
+ permitted = params.slice(*allowed_params)
38
+ if permitted.blank?
39
+ raise_service_error({ message: "There are not permitted parameters"})
40
+ end
41
+ return permitted
42
+ end
43
+
44
+ def http_get(url:, headers: {}, query: nil)
45
+ WolfCore::HttpDataSource.http_get(url: url, headers: headers, query: query)
46
+ end
47
+
48
+ def http_post(url:, headers: {}, body: nil, query: nil)
49
+ WolfCore::HttpDataSource.http_post(url: url, headers: headers, query: query, body: body)
50
+ end
51
+
52
+ def barton_integration_http_post(path:, body:, error_message:)
53
+ domain_url = ENV['CURRENT_SAM_URL']
54
+ response = http_post(url: "#{domain_url}/#{path}", body: body)
55
+ validate_http_response(
56
+ response: response, message: error_message, error_data: { url: path }
57
+ )
58
+ response
59
+ end
60
+
61
+ def validate_http_response(response:, message:, error_data: nil)
62
+ unless response.code == 200
63
+ error_data = {
64
+ message: message,
65
+ response: parse_http_response(response)
66
+ }.merge(error_data || {})
67
+ raise_service_error(error_data)
68
+ end
69
+ end
70
+
71
+ def parse_http_response(response)
72
+ body = JSON.parse(response.body) rescue response.body
73
+ {
74
+ code: response.code,
75
+ body: body,
76
+ message: response.message,
77
+ }
78
+ end
79
+
80
+ def get_salesforce_access_token
81
+ result = WolfCore::SalesforceOauthService.new.call
82
+ raise_failed_result(result)
83
+ result.data.access_token
84
+ end
85
+
86
+ def get_salesforce_foreign_object(record_id:)
87
+ data = saleforce_http_get(
88
+ query: { calltype: 'getRecord', RecordId: record_id }
89
+ )
90
+ foreign_object = data.first
91
+ puts "foreign object is"
92
+ pp foreign_object
93
+ foreign_object
94
+ end
95
+
96
+ def saleforce_http_get(query: nil)
97
+ response = http_get(
98
+ url: ENV['SALESFORCE_URL'],
99
+ headers: { 'Authorization' => "Bearer #{@salesforce_access_token}" },
100
+ query: query
101
+ )
102
+ validate_salesforce_response(response)
103
+ JSON.parse(response.parsed_response)
104
+ end
105
+
106
+ def validate_salesforce_response(response)
107
+ return if response.code == 200
108
+ message = JSON.parse(response.body).first['message']
109
+ raise_service_error({ message: message })
110
+ end
111
+
112
+ def get_wolf_token
113
+ response = http_post(
114
+ query: { tenant: ENV['TENANT'] },
115
+ url: "#{ENV['WOLF_PLATFORM_URL']}/api/v1/sign_in",
116
+ body: {
117
+ email: ENV['WOLF_ADMIN_EMAIL'],
118
+ password: ENV['WOLF_ADMIN_PASSWORD']
119
+ }
120
+ )
121
+ validate_http_response(
122
+ response: response, message: 'Failed to get wolf token'
123
+ )
124
+ response_body = JSON.parse(response.body)
125
+ wolf_token = response_body.dig('user', 'authentication_token')
126
+ puts "wolf token is #{wolf_token}"
127
+ wolf_token
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,24 @@
1
+ module WolfCore
2
+ class SalesforceOauthService < WolfCore::ApplicationService
3
+ def process
4
+ response = http_post(
5
+ url: ENV['SALESFORCE_OAUTH_URL'],
6
+ headers: { 'Content-Type' => 'application/x-www-form-urlencoded' },
7
+ body: {
8
+ grant_type: ENV['SALESFORCE_OAUTH_GRANT_TYPE'],
9
+ username: ENV['SALESFORCE_OAUTH_USERNAME'],
10
+ password: ENV['SALESFORCE_OAUTH_PASSWORD'],
11
+ client_id: ENV['SALESFORCE_OAUTH_CLIENT_ID'],
12
+ client_secret: ENV['SALESFORCE_OAUTH_CLIENT_SECRET'],
13
+ }
14
+ )
15
+ if response.code == 200
16
+ access_token = JSON.parse(response.body)['access_token']
17
+ puts "salesforce access token is #{access_token}"
18
+ Result.success(data: { access_token: access_token })
19
+ else
20
+ Result.failure(error: { message: response.message, code: response.code, body: response.body })
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,12 @@
1
+ module WolfCore
2
+ class ServiceException < StandardError
3
+ attr_reader :error
4
+
5
+ def initialize(error)
6
+ @error = OpenStruct.new(
7
+ { message: 'error' }.merge(error)
8
+ )
9
+ super(@error[:message])
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,15 @@
1
+ module WolfCore
2
+ module HttpDataSource
3
+ module_function
4
+
5
+ def http_get(url:, headers: {}, query: nil)
6
+ HTTParty.get(url, headers: headers, query: query)
7
+ end
8
+
9
+ def http_post(url:, headers: {}, query: nil, body: nil)
10
+ headers['Content-Type'] ||= 'application/json'
11
+ body = body&.to_json if headers['Content-Type'] == 'application/json'
12
+ HTTParty.post(url, headers: headers, query: query, body: body)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,26 @@
1
+ class Hash
2
+ def with_indifferent_access
3
+ hash = self.dup
4
+ hash.extend IndifferentAccess
5
+ hash
6
+ end
7
+
8
+ module IndifferentAccess
9
+ def [](key)
10
+ fetch(key.to_s) { fetch(key.to_sym) { nil } }
11
+ end
12
+
13
+ def fetch(key, *extras)
14
+ super(key.to_s, *extras)
15
+ rescue KeyError
16
+ super(key.to_sym, *extras)
17
+ end
18
+
19
+ def has_key?(key)
20
+ key?(key.to_s) || key?(key.to_sym)
21
+ end
22
+ alias_method :include?, :has_key?
23
+ alias_method :key?, :has_key?
24
+ alias_method :member?, :has_key?
25
+ end
26
+ end
@@ -0,0 +1,9 @@
1
+ class Object
2
+ def present?
3
+ !blank?
4
+ end
5
+
6
+ def blank?
7
+ nil? || (respond_to?(:empty?) && empty?)
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ module WolfCore
2
+ module RequireUtils
3
+ module_function
4
+
5
+ def require_relative_files(*folders)
6
+ folder_files = File.join(*folders, '**', '*.rb')
7
+ Dir[folder_files].sort.each { |file| require_relative file }
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,27 @@
1
+ module WolfCore
2
+ class Result
3
+ attr_accessor :data, :error
4
+
5
+ def initialize(success:, data: nil, error: nil)
6
+ @success = success
7
+ @data = data
8
+ @error = error
9
+ end
10
+
11
+ def self.success(data: {})
12
+ Result.new(success: true, data: OpenStruct.new(data))
13
+ end
14
+
15
+ def self.failure(error: {})
16
+ Result.new(success: false, error: OpenStruct.new(error))
17
+ end
18
+
19
+ def success?
20
+ @success
21
+ end
22
+
23
+ def failure?
24
+ !@success
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WolfCore
4
+ VERSION = "0.1.0"
5
+ end
data/lib/wolf_core.rb ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ostruct'
4
+ require 'httparty'
5
+
6
+ module WolfCore; end
7
+
8
+ require 'wolf_core/utils/require_utils'
9
+
10
+ WolfCore::RequireUtils.require_relative_files(__dir__, 'wolf_core')
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wolf_core
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Javier Roncallo
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-07-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Repository to store shared code among Ruby projects.
28
+ email:
29
+ - jroncallo96@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - lib/wolf_core.rb
35
+ - lib/wolf_core/application/application_service.rb
36
+ - lib/wolf_core/application/salesforce_oauth_service.rb
37
+ - lib/wolf_core/application/service_exception.rb
38
+ - lib/wolf_core/infrastructure/http_data_source.rb
39
+ - lib/wolf_core/utils/hash_extension.rb
40
+ - lib/wolf_core/utils/object_extension.rb
41
+ - lib/wolf_core/utils/require_utils.rb
42
+ - lib/wolf_core/utils/result.rb
43
+ - lib/wolf_core/version.rb
44
+ homepage: https://github.com/onewolfxyz/wolf_core
45
+ licenses: []
46
+ metadata:
47
+ allowed_push_host: https://rubygems.org
48
+ homepage_uri: https://github.com/onewolfxyz/wolf_core
49
+ source_code_uri: https://github.com/onewolfxyz/wolf_core
50
+ changelog_uri: https://github.com/onewolfxyz/wolf_core
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: 2.7.4
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubygems_version: 3.5.14
67
+ signing_key:
68
+ specification_version: 4
69
+ summary: Repository to store shared code among Ruby projects.
70
+ test_files: []